Page 1 of 1

interface vs abstract

Posted: Tue Sep 22, 2009 4:35 am
by kickeru
hi,

i am new to php programming, i am learning php from php.net

can someone tell me the difference between an interface and an abstract class? it seems that you need to implement functions from either the interface or from the abstract class, they can be defined or you need to define and can redefine them.
what advantages would one have over the other?
when would you use one and not the other?

Re: interface vs abstract

Posted: Tue Sep 22, 2009 4:47 am
by Eran
Two main differences:
1. Classes can only extend one other class (there's no multiple inheritance). On the other hand, multiple interfaces can be implemented per class
2. Abstract class can contain concrete methods, an interface contains only method structure

Re: interface vs abstract

Posted: Tue Sep 22, 2009 1:23 pm
by Christopher
If you are new to PHP programming, I would recommend just using classes until you get to the point where you have a need for abstract or interface.

Re: interface vs abstract

Posted: Tue Sep 22, 2009 1:57 pm
by requinix
Interfaces are for functionality without really caring about how it functions. A "Growable" interface is for when something needs to grow: while you don't know what that "something" is so you can't say exactly how growing should work (thus can't have code in an interface), you do know that the "something" has to be able to grow. You'll leave it up to that class to decide what to do.

Code: Select all

interface Growable {
 
    public function grow();
 
}
Abstract classes are just like classes except this specific class is more of a concept than a real thing. There is no such thing as a Tree but there are OakTrees and PineTrees and MapleTrees. Each of those are different in little ways, but they do have a lot in common: that's where a Tree comes in. A Tree is the idea of a tall plant with roots, branches, leaves, some fruit, and so on. This abstract Tree class can define a lot so that you don't have to repeat the same code in each OakTree/PineTree/MapleTree/etc class.
But since each sub-Tree has little differences there are some things a Tree can't do: these are the abstract methods. "produceFruit" should be an abstract method because each type of tree has differently-shaped leaves.

Code: Select all

abstract class Tree implements Growable {
 
    public function grow() { // required by Growable
        // ...
    }
 
    public function growNewBranch() {
        // ...
    }
 
    // all Trees can drop their fruit
    public function dropFruit() {
        // ...
    }
 
    // however the exact fruit for each Tree varies
    public abstract function produceFruit();
 
}

Re: interface vs abstract

Posted: Tue Sep 22, 2009 2:12 pm
by Darhazer
:offtopic:
The think that I don't like in an interface is that it does not provide any default implementation. And I don't see the point, why 1 interface can extend multiple interfaces without any conflict resolving (if 2 of the interfaces have a method with the same name it rises a fatal error), but 1 class can't extend multiple classes.
:offtopic:

Re: interface vs abstract

Posted: Tue Sep 22, 2009 2:37 pm
by Eran
Exactly because it has no implementation that it's easy to resolve multiple interface inheritance. Multiple class inheritance is much more problematic - if you have the same method in two classes you inherit from, which one is used?

Interface are there more for semantics and type safety checks. It's not for everyone, but some like those kind of things.

Re: interface vs abstract

Posted: Tue Sep 22, 2009 2:40 pm
by requinix
Darhazer wrote:The think that I don't like in an interface is that it does not provide any default implementation. And I don't see the point, why 1 interface can extend multiple interfaces without any conflict resolving (if 2 of the interfaces have a method with the same name it rises a fatal error), but 1 class can't extend multiple classes.
There are times when a class has a legitimate need to extend multiple classes, but most times what you want to do should be done with interfaces or mixins.

(There's nothing in OOD that says one class can only extend from one other class but most languages do it that way. Eiffel is one that doesn't.)

Re: interface vs abstract

Posted: Tue Sep 22, 2009 3:40 pm
by Darhazer
pytrin wrote:Exactly because it has no implementation that it's easy to resolve multiple interface inheritance. Multiple class inheritance is much more problematic - if you have the same method in two classes you inherit from, which one is used?

Interface are there more for semantics and type safety checks. It's not for everyone, but some like those kind of things.
And when the same method exists in two interfaces you inherit from, which one is used?

Code: Select all

 
<?php
interface A {
   function save();
}
 
interface B {
   function save();
}
 
interface C extends A, B {
}
?>
Fatal error: Can't inherit abstract function B::save() (previously declared abstract in A) on line 10
So the question is not why we have no multiple inheritance in PHP, but why we have multiple inheritance for interfaces without conflict-resolving, but have no such for classes?

Re: interface vs abstract

Posted: Tue Sep 22, 2009 4:10 pm
by Eran
I guess they didn't allow that as well since you could have different declarations to the same method.. but that is somewhat stupid in my opinion.