Mehedi Hasan's BLog

Tuesday, October 1, 2019

OOP : Abstract Class vs Interface

I found this picture that tells the difference between abstract class and interface:
enter image description here

Let's see each comparison practically so we know what it means.
Interface support multiple inheritance | Abstract class does not support multiple inheritance
It 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 to extend an abstract class:
class Class_A { }

abstract class MyAbstractClass extends Class_A { }
No problem there, you CAN extend an abstract class with exactly one class but if you try to add one more:
class Class_A { }
class Class_B { }

abstract class MyAbstractClass extends Class_A, Class_B { }
This time PHP would give you strange error without telling you what you are doing wrong:
Parse error: syntax error, unexpected ',', expecting '{'
I wish PHP would have given message somewhat like (hope PHP gives smarter error messages in future versions):
Fatal Error: You cannot extend an abstract class with more than one classes

Interface does'n Contains Data Member | Abstract class contains Data Member
By data members, it means class properties or variables. So you cannot add data members to an interface:
interface MyInterface {
    public $foo = null;
}
So in an interface, only method stubs can be provided.
You can add data members to an abstract class though, this is valid code of course:
abstract class MyAbstractClass {
    public $foo = null;
}

Interface does'n contains Constructors | Abstract class contains Constructors
It sounds like this point applies to may be other languages but in PHP an interface CAN have an empty constructor shell:
interface MyInterface {
    public function __construct();
}
Like other languages, PHP shouldn't have allowed having a constructor inside an interface. But anyways, it doesn't make much sense here and should be avoided anyway.
On the other hand, an abstract class can contain constructor method:
abstract class MyAbstractClass {
    abstract public function __construct();
}
Here constructor is said to be abstract and therefore expends child classes to complement for it. However, you can also have common initialization code in constructor of an abstract class too in which case, you would need to remove the abstract keyword and provide the body for it:
abstract class MyAbstractClass {
    public function __construct() {
         // initialization code
    };
}

An interface Contains only incomplete member (signature of member) | An abstract class Contains both incomplete (abstract) and complete member
This simply means an interface can only contain method stubs not their implementation. This is pretty same as second point in the image above. 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 and that's it.
Abstract classes can have both; empty method definitions as well as full method implementation. Generally empty method stubs are prefixed with abstract keyword 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() {
        echo 'I am running!';
    }
}

class Dog extends Animal {
    public function prey() {
        echo 'I killed the cat !';
    }
}

class Cat extends Animal {
    public function prey() {
        echo 'I killed the rat !';
    }
}

$dog = new Dog();
$cat = new Cat();

$dog->prey(); // I killed the cat !
$cat->prey(); // I killed the rat !

$dog->run(); // I am running!
$cat->run(); // I am running!
In above code, we can see that Animal class has one abstract method called prey because each child class (animal) have their own ways of finding prey and a full implemented method called run because all animals can run, this doesn't need to be defined in each of child classes. So here run method is fully implemented and represents common data that needs to 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 to be shared across child classes.

An interface cannot have access modifiers by default everything is assumed as public | An abstract class can contain access modifiers for the subs, functions, properties
By access modifiers, we mean ability to change scoping by using keywords 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 Static
This is again possible in PHP (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 to mark it as static method.
For abstract class, you can have 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 be both static and abstract, so you cannot do this, which is common across the languages:
abstract class MyAbstractClass {
    public static $foo = null;

    abstract static function foo() {} // error
}

In 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 to 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 to fill in the remaining gaps.
at October 01, 2019
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

PostgreSQL (postgre) Setup on macOS 12 (MacBook Pro 2016) Without Homebrew setup process

  • Vuetify — Color and Date Pickers
    Vuetify is a popular UI framework for Vue apps. In this article, we’ll look at how to work with the Vuetify framework. Color Picker Inputs W...
  • Laravel API Integration Best Practices for Clean and Maintainable Code
    In today’s software development landscape, APIs (Application Programming Interfaces) are essential for enabling communication between differ...
  • Algorithms Behind JavaScript Array Methods
    concat() join() fill() includes() indexOf() reverse() sort() splice() at() copyWithin() flat() Array.from() findLastIndex() forEach() every(...

About Me

My photo
Shariful Islam (Mehedi)
Bangladesh
Hi I am shariful Islam, A Full Stack Laravel Vue js and React JS developer. I am a software engineer having 7+ years of experience. You cantact me at: +8801679174124 Email: sharifcse57@gmail.com
View my complete profile

Post Updates

  • ►  2025 (6)
    • ►  June 2025 (1)
      • ►  Jun 24 (1)
    • ►  May 2025 (1)
      • ►  May 06 (1)
    • ►  March 2025 (1)
      • ►  Mar 15 (1)
    • ►  January 2025 (3)
      • ►  Jan 21 (1)
      • ►  Jan 17 (1)
      • ►  Jan 01 (1)
  • ►  2024 (6)
    • ►  November 2024 (1)
      • ►  Nov 04 (1)
    • ►  October 2024 (1)
      • ►  Oct 05 (1)
    • ►  September 2024 (1)
      • ►  Sep 25 (1)
    • ►  June 2024 (1)
      • ►  Jun 05 (1)
    • ►  March 2024 (1)
      • ►  Mar 01 (1)
    • ►  February 2024 (1)
      • ►  Feb 12 (1)
  • ►  2023 (16)
    • ►  November 2023 (1)
      • ►  Nov 30 (1)
    • ►  October 2023 (3)
      • ►  Oct 31 (2)
      • ►  Oct 09 (1)
    • ►  August 2023 (1)
      • ►  Aug 02 (1)
    • ►  July 2023 (1)
      • ►  Jul 02 (1)
    • ►  June 2023 (1)
      • ►  Jun 03 (1)
    • ►  March 2023 (3)
      • ►  Mar 04 (3)
    • ►  February 2023 (1)
      • ►  Feb 11 (1)
    • ►  January 2023 (5)
      • ►  Jan 22 (3)
      • ►  Jan 17 (1)
      • ►  Jan 16 (1)
  • ►  2022 (67)
    • ►  December 2022 (5)
      • ►  Dec 28 (1)
      • ►  Dec 25 (1)
      • ►  Dec 24 (1)
      • ►  Dec 22 (2)
    • ►  November 2022 (2)
      • ►  Nov 10 (2)
    • ►  October 2022 (1)
      • ►  Oct 05 (1)
    • ►  August 2022 (2)
      • ►  Aug 28 (1)
      • ►  Aug 27 (1)
    • ►  June 2022 (2)
      • ►  Jun 27 (1)
      • ►  Jun 26 (1)
    • ►  May 2022 (1)
      • ►  May 08 (1)
    • ►  April 2022 (2)
      • ►  Apr 19 (1)
      • ►  Apr 11 (1)
    • ►  March 2022 (30)
      • ►  Mar 09 (6)
      • ►  Mar 08 (22)
      • ►  Mar 07 (2)
    • ►  February 2022 (5)
      • ►  Feb 16 (3)
      • ►  Feb 15 (1)
      • ►  Feb 12 (1)
    • ►  January 2022 (17)
      • ►  Jan 31 (1)
      • ►  Jan 30 (2)
      • ►  Jan 25 (1)
      • ►  Jan 12 (1)
      • ►  Jan 11 (1)
      • ►  Jan 07 (3)
      • ►  Jan 03 (6)
      • ►  Jan 02 (2)
  • ►  2021 (111)
    • ►  December 2021 (63)
      • ►  Dec 26 (2)
      • ►  Dec 23 (1)
      • ►  Dec 21 (1)
      • ►  Dec 20 (1)
      • ►  Dec 14 (1)
      • ►  Dec 13 (2)
      • ►  Dec 12 (49)
      • ►  Dec 11 (1)
      • ►  Dec 07 (3)
      • ►  Dec 06 (2)
    • ►  November 2021 (6)
      • ►  Nov 26 (2)
      • ►  Nov 09 (1)
      • ►  Nov 07 (1)
      • ►  Nov 06 (1)
      • ►  Nov 01 (1)
    • ►  October 2021 (3)
      • ►  Oct 19 (1)
      • ►  Oct 14 (1)
      • ►  Oct 06 (1)
    • ►  September 2021 (10)
      • ►  Sep 28 (2)
      • ►  Sep 19 (4)
      • ►  Sep 07 (2)
      • ►  Sep 02 (1)
      • ►  Sep 01 (1)
    • ►  August 2021 (4)
      • ►  Aug 27 (1)
      • ►  Aug 24 (1)
      • ►  Aug 23 (1)
      • ►  Aug 16 (1)
    • ►  July 2021 (6)
      • ►  Jul 28 (1)
      • ►  Jul 17 (3)
      • ►  Jul 07 (1)
      • ►  Jul 03 (1)
    • ►  June 2021 (1)
      • ►  Jun 30 (1)
    • ►  May 2021 (2)
      • ►  May 20 (1)
      • ►  May 19 (1)
    • ►  April 2021 (4)
      • ►  Apr 06 (3)
      • ►  Apr 05 (1)
    • ►  March 2021 (9)
      • ►  Mar 30 (2)
      • ►  Mar 26 (1)
      • ►  Mar 15 (1)
      • ►  Mar 14 (2)
      • ►  Mar 11 (1)
      • ►  Mar 10 (1)
      • ►  Mar 09 (1)
    • ►  January 2021 (3)
      • ►  Jan 13 (3)
  • ►  2020 (107)
    • ►  December 2020 (8)
      • ►  Dec 28 (2)
      • ►  Dec 27 (1)
      • ►  Dec 23 (1)
      • ►  Dec 22 (1)
      • ►  Dec 15 (1)
      • ►  Dec 07 (1)
      • ►  Dec 06 (1)
    • ►  November 2020 (17)
      • ►  Nov 28 (1)
      • ►  Nov 25 (1)
      • ►  Nov 24 (1)
      • ►  Nov 22 (2)
      • ►  Nov 17 (5)
      • ►  Nov 14 (2)
      • ►  Nov 12 (1)
      • ►  Nov 10 (1)
      • ►  Nov 08 (1)
      • ►  Nov 03 (2)
    • ►  October 2020 (16)
      • ►  Oct 31 (1)
      • ►  Oct 28 (1)
      • ►  Oct 27 (1)
      • ►  Oct 18 (6)
      • ►  Oct 13 (2)
      • ►  Oct 11 (1)
      • ►  Oct 05 (2)
      • ►  Oct 04 (1)
      • ►  Oct 03 (1)
    • ►  September 2020 (23)
      • ►  Sep 29 (9)
      • ►  Sep 28 (2)
      • ►  Sep 27 (4)
      • ►  Sep 08 (1)
      • ►  Sep 06 (3)
      • ►  Sep 03 (3)
      • ►  Sep 01 (1)
    • ►  August 2020 (1)
      • ►  Aug 26 (1)
    • ►  June 2020 (1)
      • ►  Jun 13 (1)
    • ►  May 2020 (3)
      • ►  May 19 (1)
      • ►  May 14 (1)
      • ►  May 07 (1)
    • ►  April 2020 (2)
      • ►  Apr 23 (1)
      • ►  Apr 22 (1)
    • ►  March 2020 (13)
      • ►  Mar 20 (1)
      • ►  Mar 19 (2)
      • ►  Mar 17 (1)
      • ►  Mar 10 (5)
      • ►  Mar 09 (1)
      • ►  Mar 04 (1)
      • ►  Mar 01 (2)
    • ►  February 2020 (18)
      • ►  Feb 29 (1)
      • ►  Feb 26 (1)
      • ►  Feb 25 (2)
      • ►  Feb 22 (1)
      • ►  Feb 16 (4)
      • ►  Feb 12 (3)
      • ►  Feb 10 (1)
      • ►  Feb 08 (1)
      • ►  Feb 07 (3)
      • ►  Feb 04 (1)
    • ►  January 2020 (5)
      • ►  Jan 20 (1)
      • ►  Jan 19 (2)
      • ►  Jan 13 (1)
      • ►  Jan 08 (1)
  • ▼  2019 (121)
    • ►  December 2019 (10)
      • ►  Dec 27 (2)
      • ►  Dec 22 (1)
      • ►  Dec 21 (1)
      • ►  Dec 15 (1)
      • ►  Dec 14 (1)
      • ►  Dec 07 (1)
      • ►  Dec 06 (1)
      • ►  Dec 05 (1)
      • ►  Dec 01 (1)
    • ►  November 2019 (45)
      • ►  Nov 25 (3)
      • ►  Nov 24 (1)
      • ►  Nov 20 (10)
      • ►  Nov 18 (12)
      • ►  Nov 17 (4)
      • ►  Nov 16 (2)
      • ►  Nov 12 (2)
      • ►  Nov 11 (1)
      • ►  Nov 06 (2)
      • ►  Nov 05 (1)
      • ►  Nov 04 (4)
      • ►  Nov 03 (2)
      • ►  Nov 02 (1)
    • ▼  October 2019 (4)
      • ►  Oct 28 (1)
      • ►  Oct 25 (1)
      • ▼  Oct 01 (2)
        • OOP : Dependency Injection in PHP
        • OOP : Abstract Class vs Interface
    • ►  September 2019 (12)
      • ►  Sep 30 (1)
      • ►  Sep 26 (2)
      • ►  Sep 18 (1)
      • ►  Sep 17 (3)
      • ►  Sep 11 (2)
      • ►  Sep 09 (1)
      • ►  Sep 03 (2)
    • ►  August 2019 (1)
      • ►  Aug 30 (1)
    • ►  July 2019 (12)
      • ►  Jul 29 (2)
      • ►  Jul 16 (2)
      • ►  Jul 11 (1)
      • ►  Jul 10 (2)
      • ►  Jul 06 (2)
      • ►  Jul 03 (2)
      • ►  Jul 02 (1)
    • ►  May 2019 (22)
      • ►  May 28 (2)
      • ►  May 27 (2)
      • ►  May 24 (1)
      • ►  May 23 (1)
      • ►  May 22 (1)
      • ►  May 19 (2)
      • ►  May 18 (4)
      • ►  May 16 (1)
      • ►  May 15 (1)
      • ►  May 13 (1)
      • ►  May 05 (2)
      • ►  May 03 (4)
    • ►  April 2019 (9)
      • ►  Apr 29 (1)
      • ►  Apr 21 (5)
      • ►  Apr 08 (2)
      • ►  Apr 03 (1)
    • ►  March 2019 (1)
      • ►  Mar 07 (1)
    • ►  February 2019 (2)
      • ►  Feb 17 (1)
      • ►  Feb 10 (1)
    • ►  January 2019 (3)
      • ►  Jan 16 (1)
      • ►  Jan 12 (1)
      • ►  Jan 07 (1)
  • ►  2018 (66)
    • ►  December 2018 (9)
      • ►  Dec 24 (1)
      • ►  Dec 19 (1)
      • ►  Dec 17 (3)
      • ►  Dec 16 (1)
      • ►  Dec 12 (1)
      • ►  Dec 05 (1)
      • ►  Dec 04 (1)
    • ►  November 2018 (20)
      • ►  Nov 28 (1)
      • ►  Nov 26 (1)
      • ►  Nov 21 (2)
      • ►  Nov 19 (1)
      • ►  Nov 17 (1)
      • ►  Nov 14 (3)
      • ►  Nov 12 (1)
      • ►  Nov 08 (7)
      • ►  Nov 07 (3)
    • ►  October 2018 (37)
      • ►  Oct 31 (3)
      • ►  Oct 30 (3)
      • ►  Oct 29 (1)
      • ►  Oct 28 (6)
      • ►  Oct 24 (2)
      • ►  Oct 23 (15)
      • ►  Oct 21 (1)
      • ►  Oct 18 (2)
      • ►  Oct 03 (4)
  • ►  2017 (11)
    • ►  December 2017 (1)
      • ►  Dec 20 (1)
    • ►  November 2017 (1)
      • ►  Nov 29 (1)
    • ►  September 2017 (3)
      • ►  Sep 24 (1)
      • ►  Sep 21 (2)
    • ►  August 2017 (6)
      • ►  Aug 27 (2)
      • ►  Aug 22 (2)
      • ►  Aug 21 (1)
      • ►  Aug 20 (1)

Report Abuse

Search This Blog

About Me

  • হোম
  • My Website
Simple theme. Theme images by sndr. Powered by Blogger.