Java | Inheritance | Question 1

Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Base {
    public void show() {
       System.out.println("Base::show() called");
    }
}
   
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
   
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}

chevron_right


(A) Derived::show() called
(B) Base::show() called


Answer: (A)

Explanation: In the above program, b is a reference of Base type and refers to an abject of Derived class.

In Java, functions are virtual by default. So the run time polymorphism happens and derived fun() is called.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.