What will be the following program's output?

package tutorial.base;

public class TypesTutorial {

    public static void main(String... args) {
        A alpha = new B();
    }
}

class A {
    A() {
        System.out.print("A");
        a();
    }
    
    void a() {
        System.out.print("a");
    }
}

class B extends A {
    B() {
        System.out.print("B");
        a();
    }
    
    void a() {
        System.out.print("b");
    }
}
Explanation
Class A constructor will be called first. Overridden method from child class B is called from it, according to the type of the object created. Next, the constructor of the class B is called, which, in turn, calls the same method again.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes