Open In App

Java | Inheritance | Question 5

Like Article
Like
Save
Share
Report

Output of following Java program?




class Base {
    public void Print() {
        System.out.println("Base");
    }         
}
  
class Derived extends Base {    
    public void Print() {
        System.out.println("Derived");
    }
}
  
class Main{
    public static void DoPrint( Base o ) {
        o.Print();   
    }
    public static void main(String[] args) {
        Base x = new Base();
        Base y = new Derived();
        Derived z = new Derived();
        DoPrint(x);
        DoPrint(y);
        DoPrint(z);
    }
}


(A)

Base
Derived
Derived

(B)

Base
Base
Derived

(C)

Base
Derived
Base

(D) Compiler Error


Answer: (A)

Explanation: See question 1 of https://www.geeksforgeeks.org/output-of-java-program-set-2/

Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads