Lecture Notes Of Class 22 - Object-Oriented Programming (OOP) - Part 4

Rashmi Mishra
0

 

Lecture Notes: Class 22 - Object-Oriented Programming (OOP) - Part 4

Objective

  • Understand inheritance and interfaces in PHP.
  • Learn to extend classes and implement interfaces.

Outcome

  • Students will be able to create subclasses, use inheritance to extend functionality, and implement interfaces.

Introduction to Inheritance

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (known as a subclass or child class) to inherit properties and methods from another class (known as a superclass or parent class). This promotes code reusability and establishes a hierarchical relationship between classes.

Key Points:

  • Parent Class: The class whose properties and methods are inherited.
  • Child Class: The class that inherits from another class.
  • Syntax: In PHP, inheritance is established using the extends keyword.

Example of Inheritance

php

Copy code

class Animal {

    public $name;

 

    public function __construct($name) {

        $this->name = $name;

    }

 

    public function speak() {

        return "Animal speaks";

    }

}

 

class Dog extends Animal {

    public function speak() {

        return "Woof! My name is " . $this->name;

    }

}

 

$dog = new Dog("Buddy");

echo $dog->speak();  // Output: Woof! My name is Buddy

Explanation:

  • The Animal class is the parent class with a property $name and a method speak().
  • The Dog class is a child class that extends the Animal class, inheriting its properties and methods.
  • The Dog class overrides the speak() method to provide a specific implementation.

Advantages of Inheritance

1.   Code Reusability: By inheriting properties and methods, code can be reused across multiple classes, reducing redundancy.

2.   Maintainability: Changes made to the parent class automatically propagate to child classes, simplifying maintenance.

3.   Polymorphism: Inheritance allows methods to be overridden, enabling objects to be treated as instances of their parent class.


Interfaces in PHP

An interface is a contract that defines a set of methods that a class must implement. Interfaces allow for the implementation of multiple inheritance in PHP since a class can implement multiple interfaces.

Key Points:

  • Defining an Interface: Use the interface keyword.
  • Implementing an Interface: Use the implements keyword in a class.
  • Interfaces can contain method declarations but cannot contain properties.

Example of Interfaces

php

Copy code

interface Vehicle {

    public function start();

    public function stop();

}

 

class Car implements Vehicle {

    public function start() {

        return "Car started";

    }

 

    public function stop() {

        return "Car stopped";

    }

}

 

class Bike implements Vehicle {

    public function start() {

        return "Bike started";

    }

 

    public function stop() {

        return "Bike stopped";

    }

}

 

$car = new Car();

echo $car->start();  // Output: Car started

 

$bike = new Bike();

echo $bike->start();  // Output: Bike started

Explanation:

  • The Vehicle interface defines two methods: start() and stop().
  • Both Car and Bike classes implement the Vehicle interface, providing their specific implementations for the methods.

Using Inheritance and Interfaces Together

Classes can inherit from a parent class and also implement interfaces. This combination provides both reusability through inheritance and flexibility through interfaces.

Example

php

Copy code

interface Shape {

    public function area();

}

 

class Rectangle implements Shape {

    private $width;

    private $height;

 

    public function __construct($width, $height) {

        $this->width = $width;

        $this->height = $height;

    }

 

    public function area() {

        return $this->width * $this->height;

    }

}

 

class Square extends Rectangle {

    public function __construct($side) {

        parent::__construct($side, $side);  // Call parent constructor

    }

}

 

$square = new Square(4);

echo "Area of square: " . $square->area();  // Output: Area of square: 16

Explanation:

  • The Shape interface defines the area() method.
  • The Rectangle class implements the Shape interface and provides the area() method.
  • The Square class extends the Rectangle class, reusing its functionality while adhering to the Shape interface.

Conclusion

In this class, we explored the concepts of inheritance and interfaces in PHP. Students should now be able to create subclasses, extend functionality through inheritance, and implement interfaces effectively. These concepts are essential for building robust, maintainable, and flexible PHP applications.

Homework Assignment:

1.   Create a class hierarchy using inheritance, including at least one parent class and two child classes.

2.   Define an interface with several methods and implement it in multiple classes.


Additional Resources:


Post a Comment

0Comments

Post a Comment (0)