What will happen as a result of compiling and running the following code?

class A {
    int i = 0;
    public int increment() {
        return ++i;
    }
}

public class B extends A {
    int i = 1;                                        // 1
    public int increment() {
        return ++i;
    }
    
    public static void main(String[] args) {
        B b = (B) new A();                            // 2
        System.out.println(b.increment());
    }
}

Follow CodeGalaxy

Mobile Beta

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