Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Inheritance and Method Overriding | OOP Principles and Advanced Features
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Object-Oriented PHP

bookInheritance and Method Overriding

Understanding Inheritance and Method Overriding in PHP

Inheritance is a core concept in object-oriented programming that lets you create a new class based on an existing class. In PHP, you use inheritance to build a child class that automatically receives all the public and protected properties and methods from a parent class. This helps you avoid repeating code and makes your programs easier to maintain.

class Animal {
}

// Define a child class that inherits from Animal
class Dog extends Animal {
}

When you create a child class using the extends keyword, the child class gains access to everything the parent class has, except for private properties and methods. You can then add new features to the child class or change how some methods work.

Method overriding happens when a child class defines a method with the same name as one in its parent class. In this case, PHP will use the child class's version of the method when you call it on an object of the child class. This allows you to customize or completely change how certain actions work in different parts of your program.

class Animal {
    // Method to be overridden
    public function makeSound() {
        echo "The animal makes a sound.\n";
    }
}

// Define a child class that inherits from Animal
class Dog extends Animal {
    // Override the makeSound method
    public function makeSound() {
        echo "The dog barks.\n";
    }
}

Inheritance and method overriding help you write flexible, organized, and reusable code. By understanding these ideas, you can build complex PHP applications that are easier to manage and expand.

Code Example

Below, you will see an example of class inheritance in PHP, where a child class overrides a method from its parent class.

inheritance_example.php

inheritance_example.php

copy
123456789101112131415161718192021222324
<?php // Define a parent class class Animal { // Method to be overridden public function makeSound() { echo "The animal makes a sound.\n"; } } // Define a child class that inherits from Animal class Dog extends Animal { // Override the makeSound method public function makeSound() { echo "The dog barks.\n"; } } // Create an instance of the parent class $genericAnimal = new Animal(); $genericAnimal->makeSound(); // Outputs: The animal makes a sound. // Create an instance of the child class $dog = new Dog(); $dog->makeSound(); // Outputs: The dog barks.

This example demonstrates how a Dog class can inherit from an Animal class. The parent class Animal has a makeSound() method, which is overridden in the Dog class to provide a more specific behavior.

When we create an Animal object, calling makeSound() executes the parent method. When we create a Dog object, calling makeSound() executes the overridden method in the child class. This shows how inheritance allows child classes to reuse and customize functionality from their parent classes.

question mark

Which keyword is used in PHP to create a subclass that inherits from a parent class?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookInheritance and Method Overriding

Свайпніть щоб показати меню

Understanding Inheritance and Method Overriding in PHP

Inheritance is a core concept in object-oriented programming that lets you create a new class based on an existing class. In PHP, you use inheritance to build a child class that automatically receives all the public and protected properties and methods from a parent class. This helps you avoid repeating code and makes your programs easier to maintain.

class Animal {
}

// Define a child class that inherits from Animal
class Dog extends Animal {
}

When you create a child class using the extends keyword, the child class gains access to everything the parent class has, except for private properties and methods. You can then add new features to the child class or change how some methods work.

Method overriding happens when a child class defines a method with the same name as one in its parent class. In this case, PHP will use the child class's version of the method when you call it on an object of the child class. This allows you to customize or completely change how certain actions work in different parts of your program.

class Animal {
    // Method to be overridden
    public function makeSound() {
        echo "The animal makes a sound.\n";
    }
}

// Define a child class that inherits from Animal
class Dog extends Animal {
    // Override the makeSound method
    public function makeSound() {
        echo "The dog barks.\n";
    }
}

Inheritance and method overriding help you write flexible, organized, and reusable code. By understanding these ideas, you can build complex PHP applications that are easier to manage and expand.

Code Example

Below, you will see an example of class inheritance in PHP, where a child class overrides a method from its parent class.

inheritance_example.php

inheritance_example.php

copy
123456789101112131415161718192021222324
<?php // Define a parent class class Animal { // Method to be overridden public function makeSound() { echo "The animal makes a sound.\n"; } } // Define a child class that inherits from Animal class Dog extends Animal { // Override the makeSound method public function makeSound() { echo "The dog barks.\n"; } } // Create an instance of the parent class $genericAnimal = new Animal(); $genericAnimal->makeSound(); // Outputs: The animal makes a sound. // Create an instance of the child class $dog = new Dog(); $dog->makeSound(); // Outputs: The dog barks.

This example demonstrates how a Dog class can inherit from an Animal class. The parent class Animal has a makeSound() method, which is overridden in the Dog class to provide a more specific behavior.

When we create an Animal object, calling makeSound() executes the parent method. When we create a Dog object, calling makeSound() executes the overridden method in the child class. This shows how inheritance allows child classes to reuse and customize functionality from their parent classes.

question mark

Which keyword is used in PHP to create a subclass that inherits from a parent class?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt