Acessing grandparent's method in java

Viewed 658

i am working on practice problems simple OOP concepts using java the question gives a uml diagram and asks to implement . i went through a problem it asks me to access a method in the grandparent class from the child class.

see illustration :

    
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(); // Trying to access Grandparent's Print() 
        System.out.println("Child's Print()"); 
    } 
} 

public class Main { 
    public static void main(String[] args) { 
        Child c = new Child(); 
        c.Print(); 
    } 
} 
 

i have done my reasearch everywhere i found that java doesn't allow that due to apllying Encpculation however it is allowed in C++ , my question is can i do something to get that method from grandparent class beside the not allowed ( super.super.method() ) statement in java .

i mean can i change the structure in such a way i kep the inhertiance as it is and can access that method with missing the UML .

4 Answers
  1. There is no way to call super.super.foo() in java.

  2. You can do it implicitly by:

    public class Grandparent {
        public void Print() { 
            System.out.println("Grandparent's Print()"); 
        } 
    }
    class Parent extends Grandparent { 
        public void Print() { 
            super.Print(); // Here we can add a call to Print() from Grandparent.
            System.out.println("Parent's Print()"); 
        } 
    } 
    
    class Child extends Parent { 
        public void Print() { 
            super.Print(); 
            System.out.println("Child's Print()"); 
        } 
    } 
    
    public class Main { 
        public static void main(String[] args) { 
            Child c = new Child(); 
            c.Print(); 
        } 
    } 
    
  • Note: Try to use naming conventions for methods. Use lowerCamelCase for Print() -> print()

You can create the reference variable of GrandParent class and object of Child class. So the code becomes like this:

    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(); // Trying to access Grandparent's Print() 
        System.out.println("Child's Print()"); 
    } 
} 

public class Main { 
    public static void main(String[] args) { 
        GrandParent p = new Child(); 
        p.Print(); 
    } 
}

Here what happens is, the reference variable used is of one of the parent class so even though the object is created for the child class the reference variable only knows the method which is present inside class "GrandParent" and the output from the same method is displayed in the screen.

This is allowed for all the parent classes, that being said means all the parent class reference variable can be referred using the object of child class, but the vice versa of the same is not true.

I think you should write the Parent's print() method like this :

public void Print() {   
  super.print()
    System.out.println("Parent's Print()"); 
}

and also the child's print() method.
by doing this it will work.

You can get the expected result using reflection, but for this you need to know exactly how many levels up the super class is.

Child c = new Child();
Constructor<?> constructor = c.getClass().getSuperclass()
               .getSuperclass().getDeclaredConstructor();
constructor.setAccessible(true);
((Grandparent)constructor.newInstance()).Print();
Related