Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with same method name in parent as extended class

I have a parent class and extended class, both contain a toString() method.

How would I go about calling the parent class's toString() method from the Test app?

Right now to call the extended class's toString method it's objectname.toString(), but what about the parent class?

Thanks in advance for the help.

like image 339
Eric Avatar asked Dec 04 '22 09:12

Eric


1 Answers

You can't. This is called polymorphism, and that's what OOP is all about. The subclass toString redefines (overrides) the parent toString method.

If you want to be able to call the parent one, you need to add another method, with another name:

@Override
public String toString() {
    // redefine the toString method
}

public String parentToString() {
    return super.toString();
}
like image 188
JB Nizet Avatar answered Dec 19 '22 12:12

JB Nizet