PHP OOP Inheritance: Code Reuse, Method Overriding & Final Classes

One of the greatest benefits of Object-Oriented Programming is the elimination of code duplication. Instead of creating distinct classes from scratch that share 80% of the same properties and methods, Inheritance allows a child class to inherit all functionality from an existing parent class using the extends keyword. You can then add new specialized features or override existing behaviors.

PHP OOP Inheritance Hierarchy

Figure 8: Class hierarchy showing Parent Base Class, Child Derived Classes, and Trait horizontal inclusion.

Basic Inheritance with extends

<?php
    // Parent Base Class
    class Person {
        public function __construct(
            public string $name,
            public string $email
        ) {}

        public function getGeneralInfo(): string {
            return "Name: {$this->name} | Email: {$this->email}";
        }
    }

    // Child Derived Class inheriting Person
    class Student extends Person {
        public string $rollNumber;

        public function setRollNumber(string $roll): void {
            $this->rollNumber = $roll;
        }

        public function study(): void {
            echo "{$this->name} is currently preparing for BCA Semester Exams.<br>";
        }
    }

    $student = new Student("Aakash Verma", "aakash@veewom.com");
    $student->setRollNumber("BCA-2026-05");

    // Inherited from Person:
    echo $student->getGeneralInfo() . "<br>";
    // Specific to Student:
    $student->study();
?>

Method Overriding & Calling parent::__construct()

When a child class defines a method with the exact same name as a parent class method, it overrides the parent's implementation. If the child defines its own constructor, it should call parent::__construct() to ensure base properties are initialized:

<?php
    class Teacher extends Person {
        public string $department;

        public function __construct(string $name, string $email, string $department) {
            // Call parent constructor to initialize $name and $email
            parent::__construct($name, $email);
            $this->department = $department;
        }

        // Method Overriding: Customizing getGeneralInfo() for teachers
        public function getGeneralInfo(): string {
            $baseInfo = parent::getGeneralInfo(); // Calls parent method
            return $baseInfo . " | Department: {$this->department}";
        }
    }

    $prof = new Teacher("Dr. Sharma", "sharma@veewom.com", "Computer Applications");
    echo $prof->getGeneralInfo();
?>

The final Keyword: Preventing Overriding & Extension

When building secure software, you may want to prevent subclasses from altering critical security logic:

<?php
    // Preventing class extension: No other class can extend FinalPaymentGateway
    final class FinalPaymentGateway {
        public function charge(): void {
            echo "Processing secure payment.";
        }
    }

    // class HackedGateway extends FinalPaymentGateway {} 
    // Fatal Error: Class HackedGateway cannot extend final class FinalPaymentGateway

    class SecurityVault {
        // Preventing method overriding
        final public function getMasterEncryptionKey(): string {
            return "secret-token-10294";
        }
    }
?>

BCA / CS University Exam Corner

High-Frequency Exam Question:

Q: What is the Scope Resolution Operator in PHP?
A: The double colon (::) is the Scope Resolution Operator (Paamayim Nekudotayim). It is used to access parent:: methods, static properties/methods (ClassName::method()), and class constants (ClassName::CONSTANT).

Interactive Student Practice Session

Exercise 36.1: Create a base Vehicle class with properties brand and speed. Extend it with a Car class that adds numberOfDoors and overrides a drive() method.

Exercise 36.2: Create an application where a child class calls its parent's constructor using parent::__construct().

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!