In every example I've seen, extended classes implement the interfaces of their parents. For reference, the following example:
interface MyInterface{ public function foo(); public function bar(); } abstract class MyAbstract implements MyInterface{ public function foo(){ /* stuff */ } public function bar(){ /* stuff */ } } // what i usually see class MyClass extends MyAbstract implements MyInterface{} // what i'm curious about class MyOtherClass extends MyAbstract{}
Is failure to implement an interface in a child, which is implemented by a parent, considered bad practice or something? Are there any technical drawbacks to omitting the implementation in the child?
Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract class can be extended using the keyword “extends”.
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.
Interfaces can also be considered an abstract class which group similar methods without any implementation. To use interface in the java code, 'implements' keyword is used. A class can implement several interfaces, thus providing similar features that are provided by multiple inheritance.
I would consider that you are on the right path. There is no need to declare that you are implementing the interface, when extending a class that already implements
it. For me it's just another piece of code to maintain if change is needed. So, yes, you are correct!
Is failure to implement an interface in a child, which is implemented by a parent, considered bad practice or something? Are there any technical drawbacks to omitting the implementation in the child?
I just can't answer your question better than this guy has:
By their nature, although sometimes they may look quite similar, abstract classes and class interfaces serve very distinct purposes.
The interface of a class is meant as a tool for the "user" of that class. An interface is a public presentation for the class, and it should advertise, to anyone considering to use it, what methods and constants are available and accessible from the outside. So, as it name suggests, it always sits between the user and the class implementing it.
On the other hand, an abstract class is a tool aimed at helping the "implementor" of the classes that extend it. It is an infrastructure that can impose restrictions and guidelines about what the concrete classes should look like. From a class design perspective, abstract classes are more architecturally important than interfaces. In this case, the implementor sits between the abstract class and the concrete one, building the latter on top of the former.
Reference
Thus, it's up to you to decide, based on who is going to use (instantiate) your classes, and who is going to write them. If you are the sole user and writer of your classes, then, maybe, just maybe, you don't need them both. But, if you want to give everyone a stripped down to core bits blueprint for the class writer(s) and class user(s), then you should consider using both abstracting and implementing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With