I want to understand the role interface plays in inheritance between two classes.
My understanding is that you can create superclass and subclass with the use of extends.
class Parent {
}
class Child extends Parent {
}
This is already sufficient in creating superclass and subclass.
When do we need interface? Do we need implements for Child or Parent?
class Parent {
}
class Child extends Parent implements MyInterface {
}
interface MyInterface {
}
Inheritance and interface implementation defines a is-a relationship. This means that is the subclass A extends a superclass B, A is-a B. If the superclass B implements the interface I, B is-a I. And since A is-a A, A is also a I.
For example, a Car (ssuperclass) is a Vehicle (interface). Since Audi extends Car, Audi is also a Vehicle.
A subclass can not be less than its parent, but it can be more. So a child may implement interfaces that its parent class doesn’t implement.
For example, all cars are not hybrid (interface). But this doesn’t prevent a Prius, which is a Car, to be a hybrid. A Prius is more than just a Car. It’s also a hybrid.