Inheritance and overriding methods

Inheritance and overriding methods

In PHP programming, inheritance features allow programmers to inherit parameter properties and methods from a parent or root class or a subclass. This concept helps programmers improve program source code reusability and maintain large amounts of source code. This feature allows programmers to develop a parent class that inherits from another class. The class inheriting from these features is known as a subclass or derived class, and the main class it inherits from is known as the parent class or base class.

Inheritance and overriding methods

When a derived class or subclass inherits a method or parameter from a parent class, it can override the class method to provide a specific program implementation. This process is known as method overriding.

So, let’s get to know the concepts of inheritance and method overriding better in PHP programming.

Inheritance Class Concept in PHP.

The inheritance concept in PHP allows a derived class or subclass to inherit the properties and methods of a parent or root class. Here, the programmer can use and modify the inherited properties and class methods of the derived class or subclass based on the needs in the program.

Inheritance syntax in PHP programming.

class ParentClass {

// create program source code for the parent class

}

class ChildClass extends ParentClass {

// create program source code for the child class

}

Here, in the inheritance concept, the extends keyword is used to indicate that the subclass in the program is inheriting from the ChildClass or the parent class.

Example of inheritance in PHP programming.

So, let’s create a program demonstrating the inheritance concept in PHP programming, in which properties and methods are inherited from a child class, root class, or parent class.

<?php

// here declares employee as parent class or root class

class employee {

public $emp_name;

// here declare Constructor class method

public function __construct($emp_name) {

$this->emp_name = $emp_name;

}

// here create Parent class method

public function emp_info() {

echo $this->emp_name . ” Employee info\n”;

}

}

// here child class inherits from employee class

class hr extends employee {

// here invokes child class method

public function emp_info() {

echo $this->emp_name . ” Siddhi Deora\n”;

}

}

// Here we create an instance of the parent employee class

$employee = new employee(“instance of employee”);

$employee->emp_info(); // Result – instance of employee Employee info.

/ Here we create an instance of the child class

$hr = new hr(“Hr”);

$hr->emp_info(); // Result – Hr Siddhi Deora

?>

Explanation of inheritance in PHP.

  • In the program, hr is declared as a subclass, inheriting from the root Employee class.
  • Here, the hr subclass declares a method emp_info() in the hr class, which overrides the emp_info() method of the Employee class to provide specific behavior.

Method overriding concept in PHP programming.

In PHP programming, method overriding occurs when a subclass inherits from a root or parent class and redefines a method. This allows the subclass to provide its own implementation of the method.

Key points about method overriding in PHP classes.

  • In the method overriding concept, the name and signature parameters of the subclass’s method must be equal to or compatible with those of the root or parent class’s method.
  • In the method overriding concept, the class access modifiers (public, private, or protected) must be equal to those of the parent class’s method.

Example of method overriding in PHP programming.

<?php

// here computer Parent class created

class computer {

public function select_choice() {

echo “select your computer \n”;

}

}

// Here, the laptop child class inherits from the computer parent class.

class laptop extends computer {

/ Here, we override the select_choice method.

public function select_choice() {

echo “You select a laptop computer\n”;

}

}

// Here, we create an instance of the parent class.

$computer = new computer();

$computer->select_choice(); // Result – select your computer.

// Here, we create an instance of the child class.

$laptop = new laptop();

$laptop->select_choice(); // Result – you select a laptop computer.

?>

Explanation of PHP method overriding.

In this program, the laptop class overrides the computer class’s select_choice() method. The parent class’s select_choice() prints the message “Select your computer” to the console. However, the overridden class method in the laptop class prints the message “You select a laptop computer.”

Access modifiers with method overriding in PHP programming.

When overriding a user-defined class method in PHP programming, the class access modifier must be equal to or more compatible in the child subclass. Here, a class’s public method can be overridden by a public or protected method. The protected keyword can only be overridden by the protected or public keywords. Remember, a private keyword method in a class cannot be overridden.

Example of overriding with the correct access modifier in PHP.

<?php

// Here we create a computer as the parent class.

class computer {

// Here the private class method was created.

private function your_selection() {

echo “Select your computer device \n”;

}

// Here the public class method was created.

public function select_choice() {

$this->your_selection();

}

}

// Here, the laptop child class inherits from Computer

class laptop extends Computer {

/ Here, it tries to override the private class method (error)

/ Here, we use public function your_selection() {

/ echo “you select laptop \n”;

/ }

/ Here, it uses the correct way to override the public class method

public function select_choice() {

echo “you select laptop \n”;

}

}

$laptop = new laptop();

$laptop->select_choice(); // Result – you select laptop

?>

Explanation of overriding with the correct access modifier.

In this program, the parent class Computer declares the method your_selection() as a private method, which cannot be directly overridden by the child class Laptop. However, the child class can override the public select_choice() method to provide its own implementation.

parent keyword in PHP programming.

The parent keyword in PHP programming allows the programmer to call methods or access class properties from the parent class, even if they are overridden in the child class.

Example of using parent to call parent methods in a PHP class.

<?php

// here computer Parent class created

class computer {

public function select_choice() {

echo “You can select your desired computer \n”;

}

}

// here laptop Child class inherits from computer

class laptop extends computer {

public function select_choice() {

// here it calls the parent class’s select_choice method

parent::select_choice(); // here it calls the computer select_choice method

echo “You select hp laptop \n”; // now it adds its own class behavior

}

}

$laptop = new Laptop();

$laptop->select_choice();

// Result –

// you can select your desired computer

// you select your HP laptop

?>

Explanation of how to call parent methods in PHP.

In this program, the Laptop class overrides the select_choice() method. Here, we use the parent::select_choice() method to call the parent class’s select_choice() class method before adding laptop-specific behavior to the overridden class method.

Final methods and classes in PHP.

In PHP programming, final is a reserved keyword. The final keyword can be used to prevent inheritance or method overriding in a PHP class. A final method declared in a PHP class cannot be overridden by a subclass. Remember, a final class cannot be extended. This means that no subclasses in the current class program can inherit from it.

Example of using final in a PHP program.

<?php

// Here we create a final class that cannot be extended as inherited.

final class Computer {

public function select_choice() {

echo “Select your desired computer \n”;

}

}

// Here we try to attempt to extend a final class, which will generate an error.

// Here class Laptop extends to Computer { } // Displays a fatal error – Cannot inherit from final class Computer.

$computer = new Computer();

$computer->select_choice(); // Result – Select your desired computer.

?>

Explanation of using final in PHP.

In this program, Computer is a final keyword-declared class, hence it cannot be extended by any other class.

A summary of inheritance and method overriding in PHP programming.

  • The inheritance concept in PHP programming allows a subclass to inherit properties and class methods from a parent class.
  • Method overriding in PHP programming allows a subclass to redefine a class method inherited from a parent class.
  • In PHP programming, the parent keyword is used to call class methods from the parent class within an overridden class method.
  • Class access modifiers in PHP programming control the visibility of public, protected, and private methods and properties. When overriding class methods, the child class’s access level must be equal or more compatible.
  • Remember, final methods in a class cannot be overridden, and therefore, final classes cannot be extended in a program.

Leave a Reply