PHP Abstract And Interface Class- Tutorialpath

PHP Abstract And Interface Class
PHP Abstract And Interface Class

Abstract class

  • In abstraction there should be at least one method which must be declared but not defined.
  • The class that inherit this abstract class need to define that method.
  • There must be a abstract keyword that must be return before this class for it to be abstract class.
  • This class cannot be instantiated . only the class that implement the methods of abstract class can be instantiated.

Example:

abstract class example
abstract class example

Output:

abstract class output
abstract class output

Interface Class :

The class that is fully abstract is called interface.

Any class that implement this interface class must use implements keyword and all the methods that are declared in the class must be defined here. otherwise this class also need to be defined as abstract.

Multiple inheritance is possible only in the case of interface.

Example:

interface example
interface example

Output:

interface output
interface output

Difference Between Abstract class and Interface Class:

Abstract class :-

  • Abstract class does not support multiple inheritance.
  • Abstract contains data member.
  • Abstract class contains constructors.
  • Only complete member of abstract class can be static.

Interface Class :-

  • Interface support multiple inheritance.
  • Interface doesn’t contains data member.
  • Interface class doesn’t  contains constructors.
  • Member of Interface can not be static.

Leave a Reply

Your email address will not be published. Required fields are marked *