Java | Inheritance | Question 5

Output of following Java program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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



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.