Reputation: 27
I'm currently developing a project which requires me to control robots.
I keep them in an array of RobotInterfaces, thing is, I got a super class named RobotMovement since all movement is equal for all robots.
The robot classes that implement the RobotInterface also extend the Super Class
How do I call the method move() from the super class in the array of interfaces?
Upvotes: 1
Views: 63
Reputation: 601
class RobotMovement {
public void move() {
System.out.println("moving...");
}
}
interface RobotInterface {
public void move(); // add this
}
class Robot extends RobotMovement implements RobotInterface {
}
class Main {
public static void main(String[] args) {
List<RobotInterface> list = new ArrayList<RobotInterface>();
list.add(new Robot());
list.add(new Robot());
for (RobotInterface ri: list) {
ri.move();
}
}
}
Upvotes: 1
Reputation: 183270
The robot classes that implement the RobotInterface also extend the Super Class
How do I call the method move() from the super class in the array of interfaces?
You can declare the move()
method in RobotInterface
. That way Java will let you call move()
on any expression of type RobotInterface
, and Java will enforce the requirement that all instances of RobotInterface
have an implementation of move()
.
Upvotes: 1