Do Subclasses Inherit Private Fields

Do subclasses inherit private fields?

Most of the confusion in the question/answers here surrounds the definition of Inheritance.

Obviously, as @DigitalRoss explains an OBJECT of a subclass must contain its superclass's private fields. As he states, having no access to a private member doesn't mean its not there.

However. This is different than the notion of inheritance for a class. As is the case in the java world, where there is a question of semantics the arbiter is the Java Language Specification (currently 3rd edition).

As the JLS states (https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2):

Members of a class that are declared
private are not inherited by
subclasses of that class. Only members
of a class that are declared protected
or public are inherited by subclasses
declared in a package other than the
one in which the class is declared.

This addresses the exact question posed by the interviewer: "do subCLASSES inherit private fields". (emphasis added by me)

The answer is No. They do not. OBJECTS of subclasses contain private fields of their superclasses. The subclass itself has NO NOTION of private fields of its superclass.

Is it semantics of a pedantic nature? Yes. Is it a useful interview question? Probably not. But the JLS establishes the definition for the Java world, and it does so (in this case) unambiguously.

EDITED (removed a parallel quote from Bjarne Stroustrup which due to the differences between java and c++ probably only add to the confusion. I'll let my answer rest on the JLS :)

Why subclasses inherit private fields?

Generally, a class has a default constructor, taking no arguments, IF no constructor has been provided by you.

When you subclass Vehicle with your LandVehicle, your LandVehicle is a type of Vehicle. This means that it inherits methods and field from its superclass, even if they are private. For the class LandVehicle these members are just not visible, but they are still present - otherwise it couldn't function properly. The private keyword is an access modifier, that changes visibility to the caller.

As a result, to instantiate a LandVehicle, you also must provide the required attributes of its superclass Vehicle (since there is no default, no-arg constructor in Vehicle). In your example, a LandVehicle without a name (from Vehicle) wouldn't make sense, since a LandVehicle is a Vehicle, which requires a name.

Private members in Java inheritance

No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited.

From the Java Documentation,

Private Members in a Superclass

A subclass does not inherit the
private members
of its parent class.
However, if the superclass has public
or protected methods for accessing its
private fields, these can also be used
by the subclass. A nested class has
access to all the private members of
its enclosing class—both fields and
methods. Therefore, a public or
protected nested class inherited by a
subclass has indirect access to all of
the private members of the superclass.

From the JLS,

Members of a class that are declared
private are not inherited
by
subclasses of that class. Only members
of a class that are declared protected
or public are inherited by subclasses
declared in a package other than the
one in which the class is declared.

A useful link : Does subclasses inherit private fields?

Are private fields inherited by the subclass?

The subclass 'has' the fields of its superclass, but does not have access to them directly. Similarly, the subclass 'has' the private methods, but you cannot call or override them from the subclass directly.

In the Java documentation on inheritance, it says that

A subclass does not inherit the private members of its parent class.

However, I find it more useful to think of it as

A subclass inherits the private members of its parent class but does not have access to them

but this boils down to sematics.

Do Subclasses Inherit Private Instance Variables From Superclasses

Private members are inherited too. To test this, you can do the following in the superclass:

//...
private String myText = "I'm in the superclass";

private void setMyText(String myTextValue)
{
this.myText = myTextValue;
}

public void setMyTextPublic(String myTextValue)
{
setMyText(myTextValue);
}

public String getMyText()
{
return myText;
}
//...

Create a method in the inherited class:

//...
public void setMyTextInTheSuperClass(String myTextValue)
{
System.out.println(getMyText());
setMyTextPublic(myTextValue);
System.out.println(getMyText());
}

public void setConstantValueToMyText()
{
setMyTextInTheSuperClass("I am in the child class");
}
//...

And call setConstantValueToMyText somewhere.



Related Topics



Leave a reply



Submit