62

I have searched around SO as well as the rest of the web for a good answer but I have't found one that I really understand. I am going to present this in a different way and hopefully the answers will help others as well.

As far as I understand, the two concepts have the same rules except an abstract class is more flexible due to the method implementation ability. Also, I am aware you can implement multiple interfaces and only extend a single class but I'm sure there are more differences than the two I mentioned.

Please look at the two snippets of code and give me an example what I can do with each of my examples that would make me want or not want to use the other.

Abstract Class

abstract class Foo {
    abstract public function getValue();
    abstract public function setValue($value); 
}


class myObj extends Foo {
    function getValue() {

    }
    function setValue($value) {

    }
}

Interface

interface Foo {
    public function getValue();
    public function setValue($value);
}

class myObj implements Foo {
    function getValue() {

    }
    function setValue($value) {

    }
}
1
  • Let's suppose you already have a class Bar inherits Baz and would like to pass it to some method someMethod(Foo $foo). Which solution (abstract or interface) could you use?
    – zerkms
    Apr 14, 2013 at 22:10

4 Answers 4

154

Abstract

Abstract Classes focus on a kind of things similarity.

People are considered of type mammal and as such would not be considered of type vehicle.

Interface

Interfaces focus on collation of similar function.

For example: You are a human being and are of type mammal. If you want to fly then you will need to implement a flying Interface. If you want to shoot while flying, then you also need to implement the gun Interface.

See the examples below:

abstract class Mammal {
      protected $age_;
      //below are functions I think all mammals will have,including people
      abstract public function setAge($age);
      abstract public function getAge();
      abstract public function eat($food);
}
class Person extends Mammal {
      protected $job_; //Person's feature
      public function setAge($age){
        $this->age_ = $age;
      }

      public function getAge(){
        return $this->age_;
      }

      public function eat($food){
        echo 'I eat ' ,$food ,'today';
      }

      //People only attribute
      public function setJob($job){
         $this->job_ = $job;
      }
      public function getJob(){
         echo 'My job is ' , $this->job_;
      }

}

//Now a person wants to fly, but they are typically not able to do so.
//So we implement an interface
interface Plane{
  public function Fly(); 
}

//I also want shoot enemy
interface Gun{
  public function shoot();
}

class Person2 extends Mammal implements Plane,Gun{

      protected $job_;//Person feature
      public function setAge($age){
        $this->age_ = $age;
      }
      public function getAge(){
        return $this->age_;
      }
      public function eat($food){
        echo '<br/>I eat ' ,$food ,' today<br/>';
      }
      //Only a person has this feature.
      public function setJob($job){
         $this->job_ = $job;
      }
      public function getJob(){
         echo 'My job is ' , $this->job_;
      }

      //-----------------------------------------
      //below implementations from interfaces function. (features that humans do not have).
      //Person implements from other class
      public function fly(){
        echo '<br/>I use plane,so I can fly<br/>';
      }
      public function shoot(){
        echo 'I use gun,so I can shoot<br/>';
      }
}

$People = new Person();
echo '<pre>';
print_r( get_class_methods('People'));
echo '</pre>';

echo '<pre>';
print_r( get_class_methods('People2'));
echo '</pre>';

$People2 = new Person2();
$People2->setAge(24);
echo $People2->getAge();
$People2->eat('egg');
$People2->setJob('PHP devepop');
echo $People2->getJob();

$People2->fly();
$People2->shoot();
4
  • 1
    @LiuqingHu @ Super way of explaining things, can you please help me your interface class uses, with little more example.
    – jsduniya
    Feb 7, 2014 at 7:09
  • 5
    class People2 extends Mammalian implements Plane,Gun -- perhaps it's merely a matter of language barrier, but that sounds insane.
    – Dan Lugg
    Feb 18, 2014 at 15:21
  • 1
    @Vladd that's why it doesn't implement interfaces for that. Great explanation Liu! Jan 4, 2015 at 18:55
  • I know this is an old post but I'd like to understand what is the purpose or usefulness of creating 'abstract Mammal'? I know your example is a simple one, but wouldn't it be fine to just have 'class Person' without 'abstract Mammal'? Could you provide an example of when/why one should create an abstract? Thanks in advance :)
    – S.A
    Dec 1, 2015 at 6:30
31

To resume the idea (globally, not in detail):

inheritance

is the notion to extend from something, and optionally add some new feature or override some existing feature (to do differently). But using inheritance, you share a big part of code with the parent. You are a parent + some other things.

interface

is representing some abilities (we says a class is implementing an interface to says that it has these abilities). An interface can be implemented by 2 classes which are completely different and do not share their code (except for methods they implements). When A and B are implementing interface C, A is not a B and B is not a A.

And one of the reason for interface is indeed to allow programmer to do the same as they could do with multi-inheritance, but without multi-inheritance problems.

This notion is used in some programming languages like JAVA, PHP...

3
  • 1
    So really, for the most part these two ideas focus on principles rather than actual implementation details. In my example code the two ways of doing it would be the exact same thing since the code means nothing and is too ambiguous to determine which implementation would be a better fit. Am I correct in my thinking? Apr 12, 2013 at 5:09
  • 1
    The implementation wich will be a better fit depend on your needs. If you create abstract function, doing nothing on the abstract class, just to inherit in a 1-level, then the interface will be better, because you can implement an interface from any class (any type of object). But, if you have multiple level of inheritance in your classes, and need to re-use a parent version, then, its better to use the abstract class.
    – MatRt
    Apr 14, 2013 at 22:13
  • For the best analogy ever, see the answer below.
    – Styphon
    Feb 26, 2014 at 17:26
26

Briefly speaking, interface is to standardize a set of functions,while abstract class is to define a basic skeleton for classes to derive from.

3
  • Would it be correct to say that abstract classes with abstract methods requires descendant classes to define those methods (or at least for all abstract methods to be implemented somewhere in itself or its ancestors)?
    – ahnbizcad
    Aug 3, 2015 at 20:51
  • Hmm in conjunction with Liuqing Hu's answer, it all makes sense now 8)
    – Andrew
    Feb 10, 2016 at 18:24
  • I like this answer. More simply, both interfaces and abstract classes enforce QUALITIES or ATTRIBUTES on a class. The difference is interfaces are 100% abstract and so some refer to them as contracts. (Think about it for a second, you enforce how a class works.) Mar 29, 2016 at 12:02
3

I have thought about this before, and the best I could conclude was that interfaces are a logically convenient abstraction of a pure abstract class (c++).

As for why you would choose interfaces over abstract classes, I quote (a c++ source but the concepts are the same):

Note that there is a great temptation to add concrete member functions and data to pure abstract base classes. This must be resisted, in general it is a sign that the interface is not well factored. Data and concrete member functions tend to imply a particular implementation and as such can inherit from the interface but should not be that interface. Instead if there is some commonality between concrete classes, creation of abstract class which inherits its interface from the pure abstract class and defines the common data and member functions of the concrete classes works well.

The thing is, when using interfaces, the first thing that comes to mind is decoupling. When using an interface, the user and the implementing-class are totally decoupled. The same applies for when you're using a pure abstract class which is basically an interface.

Not the answer you're looking for? Browse other questions tagged or ask your own question.