Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are private fields inherited by the subclass?

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.

like image 940
harish Avatar asked Nov 16 '11 13:11

harish


People also ask

What is inherited by a subclass?

Classes in Java exist in a hierarchy. A class in Java can be declared as a subclass of another class using the extends keyword. 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 ; ...

Are private variables visible to subclasses?

As we mentioned earlier, methods and variables declared as private are accessible only within their class. At the other end of the spectrum, members declared as public are accessible from any class in any package, provided the class itself can be seen.

Are private methods inherited?

Private methods are inherited in sub class ,which means private methods are available in child class but they are not accessible from child class,because here we have to remember the concept of availability and accessibility.

Is private accessible in subclass?

With private inheritance, all members from the base class are inherited as private. This means private members are inaccessible, and protected and public members become private.


2 Answers

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.

like image 79
Michael Berry Avatar answered Sep 28 '22 07:09

Michael Berry


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.

like image 39
Garrett Hall Avatar answered Sep 28 '22 09:09

Garrett Hall