Search code examples
javainheritancesuper

how to access grandparent attribute in the child class with same name as some attribute in child class


I was tinkering with classes and objects, then I had a doubt, since all the non-private methods and variables/attributes are available to the child class, and if there is any attribute in the parent class with the same name as some attribute in the child class, the it can be accessed using super.attrName. But what if this is the situation in case of multi-level-inheritence, and you want to access the grandparent attribute in the child class, how can it be done. using super.super.attrName gives error.

my sample code

public class Main {
    public static void main(String[] args) {
        Abc sahil = new Abc();
        System.out.println(sahil.length);
    }
}

class A {
    int length;

    A() {
        this.length = 1;
    }
}

class Ab extends A {
    int length;
    
    Ab() {
        this.length = 2;
    }
}

class Abc extends Ab {
    int length;

    Abc() {
        this.length = 3;
    }
}

Solution

  • You cannot write super.super in Java, that's why you are getting a compilation error.

    The keyword super is used to get the attributes and methods from the immediate parent given that they are accessible from the child. So it expects only methods and attributes and not a keyword.

    Now, if you want to access an attribute from the topmost parent, the best thing you can do is create an object and call the attribute. The other way of doing this is by creating a chain of methods in the intermediate parent classes. You can create a method in Ab that returns the super.length (A.length) and call that method from Abc using the super keyword. I won't recommend that because as the number of classes increases in the hierarchy you'll have to add this method in all the intermediate classes.

    But, in a normal scenario, I've to ask a question. Consider the Car (a class) which has a gasPedal (an attribute), now there are Audi, Bently, and Honda which inherit from the Car. My question is that why would Audi be requiring the gasPedal from the Car when it has its own custom-designed gasPedal?

    There is nothing wrong with accessing the superclass attribute, it is fine, but what I was trying to convey in the above question is a basic OOPs concept.

    Update regarding the comment

    When a person needs to have data regarding his grandparent's age, you'll need a new attribute in that person, grandFatherAge. Now this new attribute will be having the same value for every grandchildren he has, so you can declare it as static.

    class GrandFather {
        
        int age = 88;
    }
    
    class Father extends GrandFather {
        
        int age = 50;
    }
    
    class Son1 extends Father {
        
        int age = 18;
        static int grandFatherAge;
        
        static {
            
            grandFatherAge = new GrandFather().age;
        }
    }
    
    class Son2 extends Father {
        
        int age = 15;
        static int grandFatherAge;
        
        static {
            
            grandFatherAge = new GrandFather().age;
        }
    }
    
    class Daughter1 extends Father {
        
        int age = 21;
        static int grandFatherAge;
        
        static {
            
            grandFatherAge = new GrandFather().age;
        }
    }
    
    class HierarchyManager {
        
        public static void main(String[] args) {
            
            System.out.println(Son1.grandFatherAge);
            System.out.println(Son2.grandFatherAge);
            System.out.println(Daughter1.grandFatherAge);
        }
    }
    

    I applied your "brainstorming" logic, a person can know his grandparent's age directly from his Grandparent, it is not necessary that it needs to come from his direct parent.