Private methods in Inheritance

后端 未结 9 1643
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 04:19

Here\'s an interesting code snippet:

public class Superclass {

    public static void main (String[] args){
        Superclass obj = new Subclass();
                


        
相关标签:
9条回答
  • 2020-11-29 04:38
    Superclass obj = new Subclass();
    

    At this point, obj is both things, a Subclass, and a Superclass object. The fact that you use Superclass in the declaration of the variable is just a matter of casting it.

    When you do: obj.doSomething(), you are telling the compiler to call the private method doSomething() of obj. Because you are doing it from the main static method inside Superclass, the compiler can call it.

    If you would use the main method of Subclass rather than the one in Superclass, you would not be able to access that method because, as you said, it's neither inherited nor a part of your definition of Subclass.

    So basically you understood inheritance correctly. The problem was related to the visibility of private methods.

    0 讨论(0)
  • 2020-11-29 04:39

    You answered it yourself. As the private methods are not inherited, a superclass reference calls its own private method.

    0 讨论(0)
  • 2020-11-29 04:46

    Private methods are only for the owner.

    Not even for the kids, relatives or friends of the owner.

    0 讨论(0)
  • 2020-11-29 04:51

    To understand this question you can relate private method to member variable of super class and sub class.

    So we know member variable is not going to be overridden in sub class.

    For example:

    Class A{
     int i = 10;
    }
    
    Class B extends A{
      int i = 11;
    }
    
    Class C extends A {
       int i = 12;
    }
    
    A a1 = new B();
    print(a1.i) // Will print 10
    
    A a2 = new B();
    print(a2.i) // Will print 10 
    

    Similar way when there is no inheritance reference variable super class is going to be considered.

    0 讨论(0)
  • 2020-11-29 04:53

    why it's possible to invoke doSomething() in the first place?

    Why not? obj is an instance both of Subclass and Superclass, and as doSomething() is declared in Superclass and obj is used in it, so you've access to Superclass.doSomething(), you may try to rename your method (to e.g.: doAnotherThing()) and you'll still have access to it.

    how does obj have access to a private member of its parent?

    There is no parent/child for a private method, and as obj is also a type of Superclass, so it has access to all private methods/fields declared within it, because obj is used in this class. You will lose this access privilege if you're outside of Superclass or of a class that has Superclass as a member (nested class).

    So What?

    There is no relation/inheritance between SuperClass's private methods and SubClass's private methods, even they have the same name and signature, from Java Language Specification, Java SE 8 Edition:

    A private method and all methods declared immediately within a final class (§8.1.1.2) behave as if they are final, since it is impossible to override them.

    0 讨论(0)
  • 2020-11-29 04:54

    When we define a private method with the same name in the derived class, it becomes a new method as derived class don't inherit the private members.

    Since the private method is not even visible outside the class, we can never call a base class private method from a derived class, it will throw a compilation error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method aPrivateMethod() from the type Base is not visible

    We can use down casting to the parent class reference to call the derived class private method, which can only be accessed in that derived class.

    0 讨论(0)
提交回复
热议问题