Interview questions on Interfaces in php

1. What is an interface in php?

An interface contains all abstract methods by default. An interface doesn't have any common methods. Methods of interface do not have any implementation part. Methods of interface are only declared.

Interfaces are declared using the interface keyword. Interfaces must be implemented by using implements keyword.

A class can implement multiple interfaces

Syntax:

<?php
interface InterfaceName {
  public function abstractMethod1();
  public function abstractMethod2($param1, $param2);
 
}

class SubClass implements InterfaceName
{  
    //All methods of interface must be implemeted
}
?>

2. What is the difference between abstract and interface?

Abstract classes can have both abstract and concrete methods. A class can extend only one abstract class.
Interfaces can only have abstract methods by default. A class can implement multiple interfaces.


3. What is the difference between inheritance and interface?

In inheritance, only one class can inherits another class. In interface, one or more than one class can implements interface.

Inheritance can be implemented by using 'extends' keyword. Interface can be implemented by using 'implements' keyword.

Comments

Leave a Reply

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

84769