Overriding Examples

Java Inheritance and Polymorphism MCQs with Output (2026)

Java Inheritance and Polymorphism MCQs with Output

This set of 14 output-based Java questions tests your understanding of inheritance, polymorphism, method overriding, method overloading, access modifiers, and dynamic method dispatch. Try to answer each question before checking the result.

Question 1

What is the output?

class Animal {
    void show() {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    void show() {
        System.out.println("dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Animal();
        a.show();
    }
}

Question 2

What is the output?

class Animal {
    void show() {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    void show() {
        System.out.println("dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.show();
    }
}

Question 3

What is the output?

class Animal {
    void show() {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    private void show() {
        System.out.println("dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.show();
    }
}

Question 4

What is the output?

class Animal {
    void show() {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    protected void show() {
        System.out.println("dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.show();
    }
}

Question 5

What is the output?

class Animal {
    void show(int i) {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    void show(Integer i) {
        System.out.println("dog");
    }

    void show(int i) {
        System.out.println("hello");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.show(1);
    }
}

Question 6

What is the output?

class Animal {
    void show(int i) {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    void show(Integer i) {
        System.out.println("dog");
    }

    void show(int i) {
        System.out.println("hello");
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
        a.show(1);
    }
}

Question 7

What is the output?

class Animal {
    float show(int i) {
        System.out.println("animal");
        return 1;
    }
}

class Dog extends Animal {
    int show(int i) {
        System.out.println("dog");
        return 1;
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
        a.show(1);
    }
}

Question 8

What is the output?

class Animal {
    private void show(int i) {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    void show(int i) {
        System.out.println("dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.show(1);
    }
}

Question 9

What is the output?

class Animal {
    private void show(int i) {
        System.out.println("animal");
    }
}

class Dog extends Animal {
    void show(int i) {
        System.out.println("dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
        a.show(1);
    }
}

Question 10

What is the output?

class Animal {
    void show(int i) {
        System.out.println("animal " + i);
    }

    public Animal() {
        show(6);
    }
}

class Dog extends Animal {
    void show(int i) {
        System.out.println("dog " + i);
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
    }
}

Question 11

What is the output?

class Animal {
    int i = 100;

    void show() {
        System.out.println("animal " + i);
    }

    public Animal() {
        show();
    }
}

class Dog extends Animal {
    int i = 1000;

    void show(int i) {
        System.out.println("dog " + i);
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
    }
}

Question 12

What is the output?

class Animal {
    int i = 100;

    void show() {
        System.out.println("animal " + i);
    }

    public Animal() {
        show(6);
    }
}

class Dog extends Animal {
    int i = 1000;

    void show(int i) {
        System.out.println("dog " + i);
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
    }
}

Question 13

What is the output?

class Animal {
    int i = 100;

    void show() {
        System.out.println("animal " + i);
    }

    public Animal() {
        show();
    }
}

class Dog extends Animal {
    int i = 1000;

    void show() {
        System.out.println("dog " + i);
    }
}

public class Test {
    public static void main(String[] args) {
        Dog a = new Dog();
    }
}

Question 14

What is the output?

class Animal {
    int i = 100;

    void show() {
        System.out.println("animal " + i);
    }
}

class Dog extends Animal {
    int i = 1000;

    void show() {
        System.out.println("dog " + i);
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.show();
    }
}

Frequently Asked Questions

What is method overriding in Java?

Method overriding happens when a subclass provides a specific implementation of a method that is already defined in its superclass. The method signature (name, parameters, return type) must match, and the access modifier cannot be more restrictive.

What is the difference between method overloading and method overriding?

Overloading means multiple methods with the same name but different parameter lists in the same class. Overriding means a subclass redefines a superclass method with the same signature. Overloading is resolved at compile time, while overriding is resolved at runtime.

Can a private method be overridden in Java?

No. Private methods are not inherited, so they cannot be overridden. If a subclass declares a method with the same signature, it is a new method, not an override.

What is dynamic method dispatch in Java?

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime based on the actual object type, not the reference type. It is the foundation of runtime polymorphism in Java.

Why is calling an overridable method from a constructor dangerous?

If a constructor calls a method that is overridden in a subclass, the subclass version runs before the subclass fields are initialized. This can lead to unexpected output or NullPointerException.

Conclusion

These 14 questions cover essential Java inheritance and polymorphism concepts: reference type vs object type, method overriding rules, access modifier restrictions, method overloading resolution, and constructor dispatch. Practicing them helps you prepare for Java interviews and certifications.

Related topics: Java Inheritance, Java Polymorphism, Method Overriding, Method Overloading, Java Access Modifiers, Dynamic Method Dispatch, Java Interview Questions

No comments: