I want to use/call a method inside EdumateSuperClass
that I created in Navigation
class. Is that possible? Anything I tried didn't work.
public abstract class EdumateSuperClass extends ScriptSuperClass
public abstract class Navigation extends EdumateSuperClass
super.clickNewButton();
--> The method clickNewButton() is undefined for the type ScriptSuperClass PS Please update the title if necessary. Not sure if I used the right terminology.
You have to declare the method as abstract in the using class (or its parent classes), and implement it in the child class:
public abstract class EdumateSuperClass {
protected abstract void childMethod(String message);
public void callChildMethod() {
childMethod("hello");
}
}
public abstract class Navigation extends EdumateSuperClass {
protected void childMethod(String message) {
System.out.println(message);
}
}
You can't use super
to call a method of Navigation
from inside EdumateSuperClass
.
EdumateSuperClass
is the superclass of Navigation
.
Your hierarchy is: ScriptSuperClass
-> EdumateSuperClass
-> Navigation
, where -> indicates "base class of" or "super class of".
Navigation
can use super.X()
to call concrete methods of EdumateSuperClass
, not vice versa.
When you're doing super.clickNewButton()
from inside EdumateSuperClass
, it's trying to call the method clickNewButton
from inside of the base class ScriptSuperClass
. This method is undefined for the class ScriptSuperClass
and therefore that is why you get that specific error.
EDIT: If you wanted, you could have EdumateSuperClass
remain an abstract class which declares an abstract method abstractMethod()
and Navigation
be a concrete class that defines a concrete implementation of this method.
You still can't use super
from EdumateSuperClass
to call this method inside Navigation
, however this will allow you to use EdumateSuperClass
as basically an interface when grouping objects.
ie. You could have an array of EdumateSuperClass
, which means that this array can hold references of concrete class objects derived/extended from EdumateSuperClass
. For these classes to be concrete they must define abstractMethod()
. From there you can store Navigation
objects in that array and call abstractMethod()
from any element in that array.
However, that doesn't mean you're calling abstractMethod()
from inside EdumateSuperClass
, it'll still be a Navigation
object that will contain the definition of abstractMethod()
and be calling that from itself. I don't know if that makes it any clearer or not.
Read up on polymorphism .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.