简体   繁体   English

公共final或受保护的final方法不会在基类中覆盖,而当我们在父类中将方法“ PRIVATE FINAL”用作其覆盖时

[英]public final or protected final method does not override in base class and when we make a method “PRIVATE FINAL” in parent class its override

public final or protected final method does not override in base class is simple and when we make a method private final in parent class it is overridden even if you make a parent class private final and extends this in child class with method protected final it overrides. 在基类中不覆盖public finalprotected final方法很简单,当我们在父类中将方法private final ,即使您将父类private final并在其子类中使用protected final扩展,它也会被覆盖。 Can someone explain this behaviour? 有人可以解释这种行为吗?

class A {

    private final void show() {
        System.out.println("Show method from A");
    }
}

class B extends A {

    protected final void show() {
        System.out.println("Show method from B");
    }

    public static void main(String... s) {
       new B.show();
    }
}

Hi as other people said you misunderstood about overriding. 嗨,就像其他人说的那样,您误解了覆盖。 First of all the show method in class A is private. 首先,类A中的show方法是私有的。 So private members are not accessible from out side of that class.The below example will explain that: 因此不能从该类的外部访问私有成员。下面的示例将说明这一点:

class A {
    private final void show() {
        System.out.println("Show method from A");
    }
}

public class B extends A {
    // protected final void show() {
    //     System.out.println("Show method from B");
    // }

    public static void main(String... s) {
        new B().show();
    }
}

If you compile this it will gives the below error: 如果您对此进行编译,则会出现以下错误:

B.java:12: error: cannot find symbol
        new B().show();
             ^
symbol:   method show()
location: class B
1 error

Here in your question it's calling the method "show()" which is defined in the class B not in Ai hope this will clarify your doubt. 在您的问题中,它正在调用在类B中定义的方法“ show()”,而不是Ai希望这样可以澄清您的疑问。

You can declare some or all of a class's methods final. 您可以将某些或所有类的方法声明为final。 You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. 您可以在方法声明中使用final关键字,以指示该方法不能被子类覆盖。 The Object class does this—a number of its methods are final. Object类执行此操作-它的许多方法都是最终的。

You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. 如果方法的实现不应该更改并且对于对象的一致状态至关重要,则可能希望将其定型。

Statement From java official Documents . 来自Java官方文档的声明。

If your code was 如果您的代码是

A a = new B();
a.show();

Then you would be attempting to call the method from the parent class which cannot be overridden. 然后,您将尝试从父类调用无法覆盖的方法。 As that's private in your example, you'll not get it to work. 由于在您的示例中这是私有的,因此您将无法使用它。 Try it with the methods as public. 尝试使用公共方法。

So what's happening is that your B.show() method is masking A.show() when the type of object is B - that's always the way things work. 所以发生的事情是,当对象的类型为B时,您的B.show()方法将掩盖A.show()-这始终是事情的工作方式。 Overriding methods works when your reference is of a type of the parent class. 当您的引用是父类的类型时,覆盖方法有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM