Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces and abstract class inheritance, implementation in extended classes

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?

like image 428
Dan Lugg Avatar asked Jun 16 '11 08:06

Dan Lugg


People also ask

Can abstract class and interface be extended and implemented together?

Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract class can be extended using the keyword “extends”.

What is the abstract class to be extended by the implementation program?

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.

Can a class extend a class and implement an interface?

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.

How inheritance is incorporated through interfaces and abstract classes?

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.


2 Answers

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!

like image 79
Emil Ivanov Avatar answered Oct 11 '22 23:10

Emil Ivanov


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.

like image 24
Shef Avatar answered Oct 12 '22 01:10

Shef