Abstract Class Vs Interface :: Code In Php

Abstract Class vs Interface

Sarfraz Ahmed April 18, 2015 08:26 PM

I found this picture that tells the difference between abstract class and interface:


Let"s see each comparison practically so we know what it means.

Bạn đang xem: Abstract class vs interface :: code in php

Interface support multiple inheritance | Abstract class does not support multiple inheritanceIt means you can extend an interface with one or more (hence multiple inheritance) interfaces like:

interface Interface_A interface Interface_B interface Interface_C interface MyInterface extends Interface_A, Interface_B, Interface_C As can be seen, we are extending MyInterface with three other interfaces Interface_A, Interface_A and Interface_C.

Let"s now try khổng lồ extend an abstract class:

class Class_A abstract class MyAbstractClass extends Class_A No problem there, you CAN extkết thúc an abstract class with exactly one class but if you try lớn add one more:

class Class_A class Class_B abstract class MyAbstractClass extends Class_A, Class_B This time PHPhường would give sầu you strange error without telling you what you are doing wrong:

Parse error: syntax error, unexpected ",", expecting "{"

I wish PHPhường would have given message somewhat lượt thích (hope PHPhường gives smarter error messages in future versions):

Fatal Error: You cannot extover an abstract class with more than one classes

Interface does"n Contains Data Member | Abstract class contains Data MemberBy data members, it means class properties or variables. So you cannot add data members lớn an interface:

interface MyInterface public $foo = null;So in an interface, only method stubs can be provided.

You can add data members lớn an abstract class though, this is valid code of course:

abstract class MyAbstractClass public $foo = null;Interface does"n contains Constructors | Abstract class contains ConstructorsIt sounds lượt thích this point applies khổng lồ may be other languages but in PHP. an interface CAN have sầu an empty constructor shell:

interface MyInterface public function __construct();Like other languages, PHPhường shouldn"t have allowed having a constructor inside an interface. But anyways, it doesn"t make much sense here và should be avoided anyway.

On the other h&, an abstract class can contain constructor method:

abstract class MyAbstractClass abstract public function __construct();Here constructor is said to be abstract & therefore expends child classes khổng lồ complement for it. However, you can also have sầu common initialization code in constructor of an abstract class too in which case, you would need lớn remove the abstract từ khóa và provide the body toàn thân for it:

abstract class MyAbstractClass public function __construct() // initialization code ;An interface Contains only incomplete thành viên (signature of member) | An abstract class Contains both incomplete (abstract) và complete memberThis simply means an interface can only contain method stubs not their implementation. This is pretty same as second point in the image above sầu. This is why in methods of an interface, they don"t have bodies marked with braces. So an interface is completely empty shell that enforces some rules that child classes must implement và that"s it.

Xem thêm: Chứng Quyền Có Đảm Bảo ( Cw Là Gì, Câu Hỏi Thường Gặp

Abstract classes can have sầu both; empty method definitions as well as full method implementation. Generally empty method stubs are prefixed with abstract từ khóa so that child classes must provide their implementation details. But an abstract class can also contain full method implementation inside it which are generally used for common piece of functionality that each child class may need. For example:

abstract class Animal // child classes must implement this abstract function prey(); public function run() eđến "I am running!"; class Dog extends Animal public function prey() echo "I killed the mèo !"; class Cat extends Animal public function prey() eđến "I killed the rat !"; $dog = new Dog();$cát = new Cat();$dog->prey(); // I killed the cát !$cat->prey(); // I killed the rat !$dog->run(); // I am running!$cat->run(); // I am running!In above sầu code, we can see that Animal class has one abstract method called prey because each child class (animal) have sầu their own ways of finding prey và a full implemented method called run because all animals can run, this doesn"t need khổng lồ be defined in each of child classes. So here run method is fully implemented and represents common data that needs lớn be shared across child classes.

Notice that other than common fully implemented methods inside an abstract class, you can also have common data members eg variables that need khổng lồ be shared across child classes.

An interface cannot have sầu access modifiers by mặc định everything is assumed as public | An abstract class can contain access modifiers for the subs, functions, propertiesBy access modifiers, we mean ability to lớn change scoping by using từ khóa like public, private and protected. We cannot use these for interfaces but we can use them for everything in abstract classes.

Members of interface can not be Static | Only Complete Member of abstract class can be StaticThis is again possible in PHPhường (as of now with PHP 5.4) unlike other languages, so you CAN do:

interface MyInterface static function foo();Notice the addition of keyword static lớn mark it as static method.

For abstract class, you can have sầu static members as well as those methods that are implemented in the abstract class:

abstract class MyAbstractClass public static $foo = null; public static function foo() But you cannot have a method to lớn be both static và abstract, so you cannot vì this, which is common across the languages:

abstract class MyAbstractClass public static $foo = null; abstract static function foo() // errorIn conclusion, interface and abstract classes are completely different from each other, you cannot interchange them or use one as alternative over the other. Interfaces are completely empty shells that expect child classes lớn implement everything for them. On the other hand, abstract class can not only contain common piece of information by implementing inside them but also expect child classes lớn fill in the remaining gaps.