Search code examples
javainheritancesubclasssuperclass

Why are subclasses' private variables kept when stored in a superclass type variable?


I must have missed something basic in inheritance since it doesn't make sense to me.

Given these two classes:

class Person
{
   private String name;
}

class Student extends Person
{
   private String degree;
}

Why is it possible to create a new Student object, store it in a Person type variable and still be able to keep the variable String degree and have access to it?

Person p = new Student("John", "Master's");

I thought that this is possible to use the above initialization because p will not have any variable other than name, since a Person variable can't contain a degree variable, and so it's legal - but when you do that, degree doesn't exist in p.

That's what I thought the reason was behind the fact that the opposite is illegal
(Student s = new Person("John");) - because then s will not have its other variable (degree) initialized.


Solution

  • A variable with a type of reference to a supertype can hold a reference to an instance of a subtype. Assigning the subtype instance to the variable does not change the instance in any way; it just broadens the type of reference. All the instance data is still there, and can be retrieved by a down-cast. That is,

    Student s = new Student("John", "Master's");
    Person p = s; // no cast needed for a widening conversion
    

    is exactly the same as:

    Person p = new Student("John", "Master's");
    Student s = (Student) p; // cast required for a narrowing conversion
    

    EDIT Perhaps this will help clarify things. Suppose there was a second subclass of Person:

    class Teacher extends Person
    {
        private String department;
    }
    

    Now you can do this:

    Person p = new Teacher("Fred", "Physics");
    Teacher t = (Teacher) p;
    

    But if you later tried to do this:

    p = new Teacher("Fred", "Physics");
    Student s = (Student) p; // Error!
    

    you'll get a ClassCastException because at that point in the program, p is now a reference to an object (an instance of Teacher) that is not a Student.

    Here's an analogy. In English, you might say, "I have a pet animal". This says nothing about the type of animal. (You cannot know whether the pet barks, for instance.) On the other hand, if you say, "I have a pet dog", then it makes sense to ask whether it barks at strangers. The key point is: calling your pet an animal doesn't change the fact that it is (or is not) a dog. Exactly the same thing is going on with Java references—using a more general (e.g., base class) reference type to store the object reference does not change the nature of the object itself. You lose knowledge of the details of the object, but those details are still there.

    To repeat the point I made in my comment: you never assign objects to variables in Java; you only assign references to objects. Or, to put it another way, none of your variables are objects.