简体   繁体   中英

can a local override final method?

public class Test  {  

    public static void main(String args[]){
        Test t = new Test();
        t.inner();

    }
    public final void print() { 
        System.out.print("main"); 
    }      

    public void inner() {       
        class TestInner {           
            void print(){
                System.out.print("sub");                
            }

        }
        TestInner inner =new TestInner();
        inner.print();
        print();
    }
}

Out put : submain

Question : the method print() in class Test is final and is accessible to local class , but still local class is a able to define print() method again how?

If we declare private final x() in super class, it is not accessible in sub class so we can define x() in sub class.

If we declare public final x() in super class, it is accessible in sub class so we can not define x() in sub class.

Can anybody explain ?

The inner class is not overriding the final method.

The inner class would have to extend the outer class for it to be able to override a method from the outer class.

The two classes are separate and unrelated to each other, other than the outer class contains the inner one.

Because the TestInner class doesn't extend the Test class, so it has its own namespace that is separate.

There is no trick to it, it isn't overwriting the Test classes print method.

The inner class is not overriding the final method.

The inner class would have to extend the outer class for it to be able to override a method from the outer class.

The two classes are separate and unrelated to each other, other than the outer class contains the inner one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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