0
Can i inherit like the following?
I want to inherit from a class in Java . But it would only inherit its methods and not attributes.Can somebody show me the way?
5 Answers
+ 4
The answer to this question is a bit confusing... According to the official Java documentation https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html private members of the parent class are not inherited by the subclass. However, technically speaking, a subclass inherits the private members of its parent class, but cannot access them directly. That is only possible through public or protected methods like getters and setters.To sun up, even though private fields exist in the subclass, they are not really inherited by the parent class so you just have to declare the parent's class attributes as private
+ 3
Check out this example https://stackoverflow.com/questions/6543328/private-members-in-java-inheritance
0
Hmmm
0
Can you show me a code
0
It would be easier for me to understand. I am learning java for about only 12 hours