0

How do I invoke a Parent class method from Child class object in Java?

From what I know, Child class object should invoke it's own implementation of any Parent class method that it has overridden, but what if I need to call the Parent's method implementation using the Child object reference? Does Java even support this?

Sample code for explanation:

public class Main
{
    public static void main(String[] args) {
        Human obj = new Aman(21);
        Aman obj2 = new Aman(21);
        System.out.println(obj.getAge()); //prints 20
        System.out.println(obj2.getAge()); //also prints 20
        System.out.println(obj.super.getAge()); //error here
        //how to invoke Human's getAge() method?
    }
}

class Human {
    int age;
    public Human(int age) {
        this.age = age;
    }
    
    public String getAge() {
        return new String("Human's age is "+(this.age));
    }
}

class Aman extends Human {
    public Aman(int age) {
        super(age);
    }
    
    public String getAge() {
        return new String("Aman's age is "+(this.age-1));
    }
}

How do I get my code to print: "Human's age is 21"?

  • 1
    Your code is broken: a getter method should do just that, get a property result ***and return it***. So your `getAge()` method should simply return the age value and do nothing more. Then the calling code can print the result. Human's `getAge()` could be, `return age;` and Aman's `getAge()` could simply be: `return super.getAge() - 1;` – Hovercraft Full Of Eels Sep 22 '22 at 21:02
  • 1
    BTW you mostly never need to use `new String(String)` - for example, `return "Human's age is "+(this.age);` will already create the string – user16320675 Sep 22 '22 at 21:10
  • @HovercraftFullOfEels thanks for your suggestion. I made the changes as suggested by you, but sorry to say, my code still prints the same age values for both Human and Aman. Am stuck here. – Aman Malakar Sep 22 '22 at 21:13
  • @user16320675 sure. Will try to avoid `new String(String)` . Just another question, doesn't the Java Compiler internally convert all `String s = "abcd"` to `String s = "abcd".intern()` ? If yes, isn't there any performance loss with using the above syntax? – Aman Malakar Sep 22 '22 at 21:17
  • @user16320675 thanks for clarifying my doubts. I really appreciate your help. Closing this thread now. – Aman Malakar Sep 22 '22 at 21:21

1 Answers1

0

The super.getAge() syntax is only available within subclasses. So you could do the following:

class Aman extends Human {
    public Aman(int age) {
        super(age);
    }
    
    public String getAge() {
        return new String("Aman's age is "+(this.age-1));
    }
    
    public String getHumanAge() {
        return super.getAge();
    }
}

The reason for this is that it would break encapsulation if you could just overgo the implementation of the child class by using something like obj.super.getAge() from outside the class of the obj instance.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99