Override “private” method in java

后端 未结 5 1059
小蘑菇
小蘑菇 2020-11-27 16:18

There something ambiguous about this idea and I need some clarifications.

My problem is when using this code:

public class B {

    private void don(         


        
相关标签:
5条回答
  • 2020-11-27 16:45

    private members aren't visible to any other classes, even children

    You can't override a private method, but then again, you can't call it either. You can create an identical method with the same name in the child however.

    public class A
    {
        private int calculate() {return 1;}
        public void visibleMethod()
        {
            System.out.println(calculate());
        };
    }
    
    public class B extends A
    {
        private int calculate() {return 2;}
        public void visibleMethod()
        {
            System.out.println(calculate());
        };
    }
    

    If you call A.visibleMethod() it prints out 1.

    If you call B.visibleMethod() it prints 2.

    If you don't implement the private calculate() method in B, it won't compile because the public method that calls it can't see the private method in A.

    0 讨论(0)
  • 2020-11-27 16:50

    You can't override a private method, but you can introduce one in a derived class without a problem. The derive class can not access the private method on the ancestor.

    Since t is a on object of type B, calling don() method will invoque the method defined at B. It doesn't even know that there is a method named also don() at class A

    0 讨论(0)
  • 2020-11-27 17:00

    You cannot override a private method. It isn't visible if you cast A to B. You can override a protected method, but that isn't what you're doing here (and yes, here if you move your main to A then you would get the other method. I would recommend the @Override annotation when you intend to override,

    class A extends B {
        @Override
        public void don() { // <-- will not compile if don is private in B.
            System.out.println("hoho public");
        }
    }
    

    In this case why didn't compiler provide an error for using t.don() which is private?

    The Java Tutorials: Predefined Annotation Types says (in part)

    While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

    0 讨论(0)
  • 2020-11-27 17:00

    is this because the main function is in the same class as the method "don"

    No, it's because A's don() is unrelated to B's don() method, in spite of having the same name and argument list. private methods are hidden inside their class. They cannot be invoked directly by outside callers, such as main method in your case, because they are encapsulated inside the class. They do not participate in method overrides.

    0 讨论(0)
  • 2020-11-27 17:00

    No, a private method cannot be overridden since it is not visible from any other class. You have declared a new method for your subclass that has no relation to the superclass method. One way to look at it is to ask yourself whether it would be legal to write super.func() in the Derived class.

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