Polymorphism in OOP abstract class and interface

polymorphism
Spread the love

Polymorphism is a Greek word. Polymorphism means many forms. In Object-Oriented Programming (OOP) polymorphism is slightly similar to inheritance. There are two types of polymorphism. Method overloading and method overriding. Method overriding is known as runtime polymorphism. To implement polymorphism we have to know the abstract class and the interface. In this tutorial, We are going to discuss abstract class and interface. In Object-Oriented Programming, the term polymorphism refers to the ability of an object to adapt the code to the type of data it is processing. An object may provide different implementations of one of its methods depending on the type of the input parameters.

Read More: Inheritance in OOP example with PHP

Abstract Classes

An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared but not implemented in the code. To define a class in an abstract class you have to give abstract keywords before the class. To define an abstract method you have to give abstract keywords before the method. An abstract method doesn’t contain implementation, it is just a method signature. An abstract class can have a non-abstract method. You can’t create an instance of the abstract class you just inherit this class. When you inherit an abstract class, then you have to implement or override all abstract methods in the sub-class. Let’s have an example of an abstract class.

<?php

abstract class AbstractClass
{
public function nonAbstractMethod()
{
// TODO : any task
}
abstract public function abstractMethodOne();
abstract public function abstractMethodTwo();
}

Well, in this example you can see an abstract class that contains one non-abstract method and two abstract methods. Before the abstract method, I define abstract keyword which means these methods are abstract. The abstract method doesn’t have a definition. When a child class extends this abstract class then it has to override those abstract methods.

So, when a child class is inherited from an abstract class, we have the following rules:

  • The child class method must be defined with the same name and it redeclares the parent abstract method
  • The child class method must be defined with the same or a less restricted access modifier
  • The number of required arguments must be the same. However, the child class may have optional arguments in addition
class ChildClass extends AbstractClass
{
public function abstractMethodOne()
{
// TODO : Any task 
}
public function abstractMethodTwo()
{
// TODO : Any task 
}
}

Well, in this example you can see that I extend the abstract class in this child class. Then I override those abstract methods. If I don’t do that then I will get an error.

Interface

The interface is a collection of abstract methods. Interfaces allow you to specify what methods a class should implement. Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as “polymorphism”. Interfaces are declared with the interface keyword. In the interface class, you just write only the abstract method and all methods should be public access modifiers. Let’s have an example of an interface.

interface AnyInterface
{
public function abstractMethodOne();
public function abstractMethodTwo($data);
}

Well, you can see that the define an interface I used the interface keyword instead of the class keyword. In this interface I have defined two public methods these are abstract methods. When you want to use this interface in a class you have to use the implements keyword. You can’t create an instance of any interface. Let’s see this example how to use the interface of a class.

class AnyClass implements AnyInterface
{
public function abstractMethodOne()
{
// TODO : any task
}
public function abstractMethodTwo($data)
{
// TODO : any task
}
}

Well, in this example you can see that the anyClass class implements the anyInterface. The class must override the interface’s all method. If you don’t override that then you will get an error.

Interface vs Abstract class

The interface is similar to abstract classes. The difference between interfaces and abstract classes are:

  • Interfaces don’t contain any properties, while abstract classes can have properties
  • All interface methods must be public, while abstract class methods is public or protected
  • All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary
  • Classes can implement an interface while inheriting from another class at the same time

Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.