Interfaces and abstract class inheritance, implementation in extended classes

21,439

Solution 1

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!

Solution 2

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.

Solution 3

Maybe a little late to the table but I see the above comments do not clarify the main misunderstanding underlying the OP's question.

So the underlying questions are:

  • Why we use both an Abstract class and an Interface on the same line?
  • Should both an Abstract method and an Interface declare the same methods at all?

But before some clarifications why to use either of the two above:

  • Either of them are used by one programmer to define the contract (requirements, obligations, limitations) the other programmers have to obey when they create the concrete classes (and eventually entire software application) based on Abstract classes / Interfaces developed by that programmer.
  • An Abstract class, in turn, is used to provide the later created concrete class with methods & data structures blueprint via:

    1. data structures declarations (optional),
    2. base implementation of methods (and their signatures, optional)
    3. just methods declarations (similar to an Interface usage, optional).
  • An Interface is used to provide a concrete class with a methods blueprint via

    1. just methods (and their signatures, optional) declarations.

Here is an example for an Abstract and concrete classes.

abstract class MyAbstractClass {

    public function foo() {
        // Base implementation of the method here.
    }

    public function bar() {
        // Base implementation of the method here.
    }

    // Effectively similar to baz() declaration within some interface:
    public abstract function baz($value);

}

class MyConcreteClass extends MyAbstractClass {

    // foo() and bar() are inherited here from MyAbstractClass.

    // baz() must be implemented or declared abstract again.
    public function baz($value) {
        // implementation.
    }

}

Then the questions come:

  1. Why we need an Interface here?
  2. Do we need an Interface to duplicate same method declarations?

The answers:

  1. Due to the fact that PHP allows only single inheritance for each subclass (you cannot write class MyConcreteClass extends MyAbstractClass, MyAnotherClass {}), when we need to expand the concrete class functionality beyond the already used Abstract class we have to declare this additional functionality via one or more Interfaces.

    Like this:

    class MyConcreteClass 
        extends MyAbstractClass 
        implements MyInterface, MyAnotherInterface {
        // Methods and data implementations go here.
    }
    
  2. As the result from the answer 1, an Interface better not to duplicate an Abstract class methods' declarations (this is basically useless). An Interface(s) should decalre the methods that may help to enhance the concrete (or another Abstract class, why not) functionality to provide the programmer that will use these with the firm contract for each object built on top of these classes and interfaces.

Finally, answer to the the OP question whether to use an Interface for an Abstract class or for the concrete class is:

  • use for either or both (or as needed) as long as an Interface enhances a class contract with new methods' declarations.
Share:
21,439
Dan Lugg
Author by

Dan Lugg

… 60% of the time, it works every time.

Updated on April 11, 2020

Comments

  • Dan Lugg
    Dan Lugg about 4 years

    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?

  • Dan Lugg
    Dan Lugg almost 13 years
    Thanks Emil Ivanov; That's what I assumed. I haven't worked much with interfaces (beyond SPL, et al.) and am beginning to incorporate them into my projects, as more and more of my code will be touched by client programmers. My main concern was if any changes to inheritance could occur due to omission.