Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Class vs. Interface [duplicate]

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) {      } } 
like image 658
nathanjosiah Avatar asked Apr 11 '13 23:04

nathanjosiah


People also ask

Which is faster abstract or interface?

The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class. It is used to implement the core identity of class.

Why abstract class is faster than interface?

4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java.

Can abstract class replace interface?

To answer your question, yes you could use an abstract class (providing no implementation) instead of an interface but I'd consider this bad practice: You've used up your "one-shot" at inheritance (without gaining any benefit). You cannot inherit from multiple abstract classes but you can implement multiple interfaces.

Is abstract class and interface same?

Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables. Implementation: Abstract class can provide the implementation of the interface. Interface can't provide the implementation of an abstract class.


1 Answers

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(); 
like image 77
Liuqing Hu Avatar answered Sep 19 '22 08:09

Liuqing Hu