Updated on 17 Feb 2021 | 3 Min Read
By
 

Abstract Class

Abstract classes are when the parent class contains a name method, but that needs its child class to run those tasks. In Abstract class there is at least one abstract method. Abstract methods can only be declared in an abstract class but cannot be implemented in the code. Abstract classes can be declared with the help of abstract keywords.

The syntax for an abstract class-

abstract class First {
   abstract function printdata();  
}

class Second extends First{
    public function printdata() {
        echo 'Best Interview Question';
    }
}

class Third extends First{
    public function printdata() {
        echo 'Hello Best Interview Question Team';
    }
}
$second = new Second();
$third = new Third();
$second->printdata() // Best Interview Question
$third->printdata() // Hello Best Interview Question Team

Characteristics of an abstract class

  • Abstract class can’t be instantiated directly means we can not create objects of an abstract class, they can only be inherited.
  • Both abstract and non-abstract members could be part of the abstract class.
  • One abstract method must be declared in the abstract class.
  • Abstract classes always remain public.
Related Article: How to Use Traits in PHP?

Difference between Abstract Class and Interface

Abstract Class Interface
Abstract Class doesn't support multiple inheritances. Interfaces support multiple inheritances.
They contain Data Members They don't contain data members.
An abstract class contains both abstract and non-abstract members. The interface contains only abstract members.
Abstract class contains constructors. These don't contain constructors.
Complete member of an abstract class can be static Members of the interface can not be static.
It contains access modifiers for the subs, functions, and properties. It doesn't access modifiers, by default everything is public.

Interfaces in PHP

With the help of the interface, users can create programs and help in specifying public methods that are implemented by a class, by hiding the complexity and details of how the particular methods are implemented. This is commonly referred to as the next level of abstraction. We can define the interface just like a class, the only difference is the class keyword is replaced by the interface keyword.

Syntax of an interface-

interface MyInterfaceName {
   public function methodA();
   public function methodB();
}
class MyFirstClass implements MyInterfaceName{
    public function methodA() {
       echo 'Best Interview Question';
    }
    public function methodB() {
       echo 'Best Interview Question Team';
    }
}

Characteristics of an Interface

  • It consists of methods that have no implementations means they are abstract methods.
  • All the methods contain public visibility scope.
  • They are different from class as the class can inherit from one class on the other hand class can implement one or more interfaces.

Conclusion

Interfaces and abstract classes are completely different from each other, one can not interchange or use alternatives of one another. Interfaces are more like empty shells that expect child classes to implement all things for them while abstract class doesn’t contain only common pieces of information but also expects child classes to fill remaining gaps in between them. In short- An abstract class allows the user to create functionality that subclasses can implement or override and the interface only allows the user to define functionality but we can implement it.