Open In App
Related Articles

Java | Inheritance | Question 1

Improve Article
Improve
Save Article
Save
Like Article
Like

Output of following Java Program?




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();
    }
}


(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 object 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

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 09 Jul, 2021
Like Article
Save Article
Previous
Next
Similar Reads