Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Java | Inheritance | Question 8

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Predict the output of following Java Program




// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
   
class Parent extends Grandparent {
    public void Print() {
        System.out.println("Parent's Print()");
    }
}
   
class Child extends Parent {
    public void Print() {
        super.super.Print(); 
        System.out.println("Child's Print()");
    }
}
   
public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}


(A) Compiler Error in super.super.Print()
(B)

Grandparent's Print()
Parent's Print()
Child's Print()

(C) Runtime Error


Answer: (A)

Explanation: In Java, it is not allowed to do super.super. We can only access Grandparent’s members using Parent. For example, the following program works fine.

// Guess the output
// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
 
class Parent extends Grandparent {
    public void Print() {
        super.Print();  
        System.out.println("Parent's Print()");
    }
}
 
class Child extends Parent {
    public void Print() {
        super.Print();  
        System.out.println("Child's Print()");
    }
}
 
class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}


Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads