Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 222
Rajaneesh Avatar asked Mar 07 '26 07:03

Rajaneesh


1 Answers

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.

like image 66
Bohemian Avatar answered Mar 09 '26 19:03

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!