Computer Science, asked by jhumasen09135, 10 months ago

Can we use private keyword to change variable into class variable


Anonymous: No we use static keyword
jhumasen09135: Can we use private keyword
Anonymous: No to the extent of my knowledge we cant

Answers

Answered by manya5198
0

This is a question I was asked in an interview: I have class A with private members and Class B extends A. I know private members of a class cannot be accessed, but the question is: I need to access private members of class A from class B, rather than create variables with the same value in class BThe interviewer was either testing your knowledge of access modifiers, or your approach to changing existing classes, or both.

I would have listed them (public, private, protected, package private) with an explanation of each. Then gone on to say that class A would need to be modified to allow access to those members from class B, either by adding setters and getters, or by changing the access modifiers of the members. Or class B could use reflection. Finally, talk about the pros and cons of each approach.


jhumasen09135: Can we use private keyword also
gauravdeshmukh: hii
Answered by sabhyata09
1

You would use the static keyword to change an instance variable into a class variable. Both instance and class variables are declared at the class level, not within methods.

Similar questions