PHP Abstract Classes

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.
When can define a class abstract using the
abstract keyword. An class which is defined as abstract cannot be instantiated.

Following are some important points about abstract class and method:

  •  An abstract class can have methods and properties just like any other normal class.
  •  An abstract class cannot be instantiated, hence we need to create a child class which extends it, then we can create object of the child class.
  •  If a class has even a single abstract method then the class should also be abstract.
  • An abstract method is just the declaration, where we provide name of the method and argument, while the body part is empty.

Example :let's simply understand abstract class given below :

Output : 30

Example 2 : Cannot be directly instantiate.

Output: