Are private fields inherited by the subclass?

12,260

Solution 1

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.

Solution 2

You're inheriting and using the getn() method, which is package-private and available from the subclass (since both are inherently in the same package in this case.) You can't access n directly because it's private. It's the getn() method that has access to n because it's in the same class as n, and you have access to the getn() method because it's not private.

If you did:

System.out.println("n= "+e.n+"");

...in place of your current line, then it wouldn't compile for the above reason.

It's perfectly normal behaviour to expose private variables through setter / getter methods, which is essentially what you're doing here. The difference is with this approach you have the potential to check / restrict / alter / log / anything the value of the variable when you get it or set it, and you can do so without making breaking changes when your code compiles. You can't do the same if you just make a field public and let people access it directly.

Solution 3

This is a topic worth of discussion. The confusion arises because, technically, the subclass inherits the private fields, because the private fields exist in the subclass, so that when you call getN(), it returns the value of n. So the field n exists in the subclass. If it didn't exist, then when you called getN(), it would issue an error, since the field n doesn't exist. The thing is, it does exist, and since it were declared in the superclass, it technically was inherited by the subclass.

However, we (Java programmers and the java official documentation about inheritance ) don't consider this inheritance. By our convention, this is not considered inheritance, because you cannot access the value of those fields directly. It's almost as if they weren't yours, since the only way to access them is using what everybody else(classes that are not subclasses of that superclass) use (getters/setters).

So, conceptually speaking, private fields are not inherited (although they exist in the subclass).

I think teachers should make this point a lot more clear than they do. After deeply looking at it, it really is confusing.

Share:
12,260
harish
Author by

harish

Updated on June 04, 2022

Comments

  • harish
    harish about 2 years

    I have read that a subclass cannot inherit private fields or methods. However, in this example

    class SuperClass {
        private int n=3;
        int getN() {
            return n;
        }
    }
    
    class SubClass extends SuperClass {
        public static void main(String[] args) {
            SubClass e = new SubClass();
            System.out.println("n= " + e.getN());
        }
    }
    

    When I run main I get the output as n=3. Which seems that SubClass is inheriting the private attribute n from SuperClass.

    So, please explain what's going on here. Thank you.

  • harish
    harish over 12 years
    ok..but have a little doubt as 'A subclass inherits the private members of its parent class but does not have access to them' why we use INHERITANCE while just we can instantiate an object some where in the same package and can access the attributes..
  • Garrett Hall
    Garrett Hall over 12 years
    In many cases, delegation (accessing the attributes via public methods) is preferable. However, for polymorphism especially if using the Template pattern, you need to subclass (although using interfaces is often better).