Inheritance

This lesson introduces the concept of Inheritance focusing on base and derived classes.

What is Inheritance

Inheritance is a very useful and popular concept that enhances the object-oriented programming experience. It allows a programmer to create and define classes that can inherit and build upon functionalities already present in existing classes without having to duplicate a lot of the code.

  • Provides a way to create a new class from an existing class.

  • The new class is a specialized version of the existing class.

  • Allows the new class to override or redefine inherited methods from the existing class to perform differently than how they are defined in the existing class.

Terminology

  • Base Class (or Parent): inherited by child class.
  • Derived Class (or child): inherits from base class.

Characteristics

A derived class has:

  • All members defined in the derived class.
  • All ​members declared in the base class.

A derived class can:

  • Use all public and protected members defined in the derived class.
  • Use all public and protected members defined in the base class.
  • Override an inherited member

A derived class cannot:

  • Inherit constructors and destructors

Inheritance is one of the major reasons for using object-oriented programming in PHP.

Notation

Classes can inherit the properties and methods of another class using the keyword extends.

Let’s take a look at the notation below.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy