Do subclasses inherit private fields?
This question addresses the same problem but I don't quite understand how that satisfies the (seemingly) contradictory situations below.
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Says that "A subclass does not inherit the private members of its parent class."
This means that it neither inherits private instance variables nor private methods right?
However, how does this work if it inherits a public accessor method from its parent? It returns an instance variable that it doesn't know exists?
Also, my computer science book (Baron's AP Computer Science A) has the correct answer to a question that says that "The (Subclass) inherits all the private instance variables and public accessor methods from the (Superclass)."
Isn't this in contraction to oracle's tutorial?
Thanks for your help
A subclass is a class derived from the superclass. It inherits the properties of the superclass and also contains attributes of its own. An example is: Car, Truck and Motorcycle are all subclasses of the superclass Vehicle.
A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree.
A subclass inherits variables and methods from its superclass and can use them as if they were declared within the subclass itself: class Animal { float weight ; ...
A subclass inherits variables and methods and methods from its superclass. Inheritance is one of object-oriented programming's most powerful paradigms. Through inheritance you can reuse code many times and build a hierarchy of increasingly specialized classes.
The accessor will work fine. Remember that the accessor runs in the "context" of the superclass and so the accessor will be able to see the member that's hidden from the subclasses.
As for the textbook, it depends on your point of view. The subclass inherits the private members in the sense that they are actually there inside instances of the subclass (so they'll take up memory, etc.), but the subclass will be unable to directly access them.
Think of it like an onion. Every level of hierarchy is a layer within the onion. For example, If class C extends Class B, which extends class A then an object of class C would look like:
Object of Class C
-------------------------------------------------------------
| |
| C and it's members |
| |
| ------------------------------------------------ |
| | | |
| | B and it's members | |
| | ------------------------------------ | |
| | | A and it's members | | |
| | | | | |
| | ------------------------------------ | |
| ------------------------------------------------ |
| |
-------------------------------------------------------------
So, an object of class C does have members of B and A. But it cannot access private members of B and A.
It can however access public and protected members of B and A.
So a public accessor function of B or A would allow this object of class C to access a private instance variable of the class B or class A "part" of the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With