Java: Inheritance Challenges

|

This article includes several questions about inheritance, with answers provided at the end.

Inheritance Example

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

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound();
    }
}

What will be the output?

See the answer

Bark


Explanation:
The sound method in the Dog class overrides the one in Animal. Due to polymorphism, the overridden sound method in Dog is called.

Method Overriding with super

class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    void display() {
        super.display();
        System.out.println("Child display");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.display();
    }
}

What will be the output?

See the answer

Parent display
Child display


Explanation:
The display method in Child calls super.display() first, invoking the Parent class’s display method, followed by the Child class’s display.

Method Overloading vs. Overriding

class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(5, 3));
        System.out.println(calc.add(5.5, 3.3));
    }
}

What will be the output?

See the answer

8

8.8


Explanation:
Java uses method overloading here, selecting the correct add method based on the argument types provided.

Polymorphism and Dynamic Method Dispatch

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

class B extends A {
    void print() {
        System.out.println("Class B");
    }
}

class C extends B {
    void print() {
        System.out.println("Class C");
    }
}

public class Main {
    public static void main(String[] args) {
        A obj = new C();
        obj.print();
    }
}

What will be the output?

See the answer

Class C


Explanation:
The print method in C overrides those in A and B. With dynamic method dispatch, the print method in C is called, even though the reference type is A.

Final Methods and Inheritance

class Parent {
    final void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("Child show");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}

Will this code compile? If not, why?

See the answer

Compilation Error


Explanation:
The show method in Parent is marked as final, meaning it cannot be overridden in Child. Attempting to override it in Child will result in a compilation error.

Leave a Reply

Your email address will not be published. Required fields are marked *