Interview questions on Inheritance in php

1.What is Inheritance in php?

When one class can inherit the properties(instance variables) and behavior(methods) of another class, it is called as Inheritance.

Super Class : The class from which the properties(instance variables) and behavior(methods) are inherited, It is called as super class. Also It is  called as base class or parent class

Sub Class : If class inherits the properties and behavior from another class, Then It is called as sub class. Also It is called as derived class or child class. The sub class can access all the methods and variables except the private members of super class. It also can have its own instance variables and methods.

Inheritance can be implemented by using 'extends' keyword.

Mulltiple Inheritance can not be supported by PHP. child class can only extend one parent class.

Syntax :

class Game{

}

class Cricket extends Game{

}

2. What type of inheritance does php support?

PHP supports simple, multilevel and hierarchical inheritance.

Simple: This is one level of inheritance. One class inherits from another class. In this case, class Cricket inherits from class Game.

Example,
class Game{
 
}
class Cricket extends Game{
 
}

Multilevel : This type of inheritance goes down for multiple levels.
Example,
class Game{
 
}
class Cricket extends Game{
 
}
class Team extends Cricket{
 
}

class Team inherits from Cricket, class Cricket inherits from class Game
class Game is the super class of  class Cricket.  So, automatically class Team can access the properties of Game class.

Hierarchical : Multiple classes can inherit from a single class or A class can have multiple sub classes.

Example,
class Game{
 
}
class Cricket extends Game{
 
}
class Hockey extends Game{
 
}
class Football extends Game{
 
}
In this case, Hockey ,Football ,Cricket are sub classes of Game.

Note : Mulltiple Inheritance can not be supported by PHP.

3. Why do you need inheritance?

We can use Inheritance for code reusability.

4. What is the use of super keyword in php?

The super keyword refers to the parent class. It is used to call the constructor of the parent class and to access the parent's properties and methods.

5. Can a class extend more than one class in php?

No, A single class can not extend more than one class. PHP does not support multiple inheritance.

6. What is the use of The final Keyword?

The final keyword can be used to prevent class inheritance or to prevent method overriding.

7. What is method overriding?

If both parent and child classes have same function name with number of arguments, then it is called Method Overriding in php object oriented programming.
The methods of Parent class can be overridden by the methods in the child class.

8. What is Overloading in PHP?

If method have same name and different quantities and types of parameters, then it is called as Method Overloading in most object oriented languages. But Magic methods are used to achieve method overloading in case of php. Magic methods are __set(), __get(), __isset(), __unset() etc.

Comments

Leave a Reply

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

40014