OnlineCBT Logo OnlineCBT
PHP · LESSON 18

Inheritance and Method Overriding

By Ratan Updated: 16 Oct 2026

Extend parent classes using extends keyword and override parent methods using parent:: method calls.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. Understanding Inheritance

Inheritance allows a child class to inherit properties and methods from a parent class using the extends keyword. This promotes DRY (Don't Repeat Yourself) architecture.

name}, Email: {$this->email}";
    }
}

// Derived Class (Child)
class Teacher extends Member {
    public function __construct(
        string $name,
        string $email,
        private string $subject
    ) {
        // Call parent constructor
        parent::__construct($name, $email);
    }

    // Method Overriding
    public function getProfile(): string {
        return parent::getProfile() . ", Subject: {$this->subject}";
    }
}

$teacher = new Teacher("Dr. Rajesh Gupta", "rajesh@cbt.com", "Mathematics");
echo $teacher->getProfile();

2. The parent:: Keyword

The parent:: scope resolution operator enables a child class method to invoke the implementation of the overridden parent method, extending rather than replacing the parent's logic.

3. Preventing Overriding: final Keyword

Prepending final to a class or method prevents subclasses from extending the class or overriding the method:

4. Abstract Classes and Interfaces

  • Abstract Class: A class containing at least one abstract method (declared without implementation) that all subclasses must implement.
  • Interface: A pure contract specifying which methods a class must implement using implements.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat