interface in php

In the last lesson we learnt about abstract classes and today we will learn about an important topic of object oriented programming i.e Interface.

Interface is similar to abstract classes while there are some difference in them.

Declaration and implementation of Interface.

Syntax :  we use keyword interface before the class name.

  • we use interface keyword before the class name, while in abstraction we uses abstract keyword.

How to use interface ?

To extend the interface we use keyword implement as illustrated below.

  • In Interface the name and number of methods must be same in its implementing class.

 

  • In interface we don’t declare any properties or variable while in abstract class we do.
  • In interface we declare the method without interface keyword and access modifiers must not be non-public, while in abstract class we used to write keyword abstract before the method name and we can use public , private and protected keyword before method and properties.

 

Can we implement more than one interface in a class ?

Yes, we can implement more than one interface in a class. While in inheritance a class can not extend more than one class.

 

Leave a Reply

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