What will be the result when the following code is compiled and executed?

final public class Parent {
    final public void test() {            
        System.out.println("hello");
    }
}

class Child extends Parent {         //1
    public void test() {             //2
        System.out.println("world");
    }    
}

class Main {            
    public static void main(String args[]) {
        Child c = new Child();
        c.test();
    }    
}
Explanation
In this case, the final keyword is used to override the parent class inheritance and non-override method test ()

javac Parent.java

Parent.java:7: cannot inherit from final Parent
class Child extends Parent { //1
^
Parent.java:8: test() in Child cannot override test() in Parent; overridden method is final
public void test() { //2
^
//2 errors

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback