MVC Lesson 5: Abstract class & Interface do same jobs then how they are different?

Great question! While both abstract classes and interfaces are used to achieve abstraction in PHP, they serve slightly different purposes, and there are key differences between them.

Here’s a breakdown of the differences:

1. Abstract Class

  • Purpose: An abstract class is used to define a common base class for other classes to inherit from. It allows you to provide some implementation (i.e., methods with a body) while enforcing that some methods are abstract (must be implemented in child classes).
  • Methods: Can have both abstract methods (no body) and concrete methods (with body).
  • Properties: Can have properties and methods with visibility modifiers (public, protected, private).
  • Inheritance: A class can inherit from only one abstract class due to PHP’s single inheritance model.
  • Instantiation: Cannot be instantiated directly; it must be inherited by a child class.

2. Interface

  • Purpose: An interface defines a contract of methods that a class must implement, but it does not provide any implementation. It is pure abstraction.
  • Methods: All methods in an interface must be abstract (i.e., no method body), and they cannot have any implementation.
  • Properties: Cannot have properties (only method declarations).
  • Inheritance: A class can implement multiple interfaces, making interfaces useful for multiple inheritance of behavior.
  • Instantiation: Cannot be instantiated directly; it is meant to be implemented by a class.

Key Differences:

FeatureAbstract ClassInterface
PurposeProvides partial implementation and a base for inheritance.Defines a contract (no implementation).
MethodsCan have both abstract and concrete methods.All methods are abstract (without a body).
PropertiesCan have properties.Cannot have properties.
VisibilityMethods can have any visibility (public, protected, private).All methods are implicitly public.
InheritanceA class can extend only one abstract class.A class can implement multiple interfaces.
InstantiationCannot be instantiated directly.Cannot be instantiated directly.
UsageUsed when some shared behavior is needed (partially implemented).Used when you need to ensure that certain methods exist in multiple classes (pure contract).

Example:

Abstract Class:

abstract class Animal {
    protected $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    abstract public function makeSound(); // Abstract method, must be implemented in child class
}

class Dog extends Animal {
    public function makeSound() {
        return "Woof!";
    }
}

$dog = new Dog("Buddy");
echo $dog->getName(); // Buddy
echo $dog->makeSound(); // Woof!

Interface:

interface Animal {
    public function getName();
    public function makeSound();
}

class Dog implements Animal {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function makeSound() {
        return "Woof!";
    }
}

$dog = new Dog("Buddy");
echo $dog->getName(); // Buddy
echo $dog->makeSound(); // Woof!

When to Use Each:

  • Use an abstract class when you have some shared code or functionality that multiple classes can inherit and extend. It’s great for when you want to provide common methods or properties to multiple child classes.
  • Use an interface when you want to define a contract that must be implemented by different classes, but where you don’t care about the common implementation. It’s great for achieving polymorphism or ensuring that different classes have the same set of methods.

In short, both abstract classes and interfaces help you achieve abstraction, but abstract classes allow partial implementation, while interfaces strictly define the structure (a contract) without any implementation.

More example of when to use abstract class & when the interface:

Abstract Class (Imagine it’s like a blueprint for a house that isn’t fully built yet):

When to use it: When you want to create something that has a lot of things in common, but you know some things need to be different in each house. You give basic rules, but let the builders (or other classes) fill in the details.

Example: Think of an abstract class like a Car blueprint. Every car needs wheels and an engine to work, but each car might look different or have extra features. The abstract class gives the basic things (like wheels and engine) that every car will have, but doesn’t say exactly how each part works. Then, when you create a specific car (like a Tesla or a Jeep), you fill in how things work.

abstract class Car {
    void startEngine() {
        System.out.println("Engine starting...");
    }

    abstract void drive();  // This part is different for each car
}

class Tesla extends Car {
    void drive() {
        System.out.println("Tesla is driving with electric power!");
    }
}

class Jeep extends Car {
    void drive() {
        System.out.println("Jeep is driving off-road!");
    }
}

Interface (Think of it as a set of rules or instructions everyone has to follow, no matter what):

When to use it: If you want to make sure that different things follow the same rules, but you don’t care how they do it. The focus is just on what they should be able to do, not how.

Example: Imagine you have different toys that can all make a sound, but some toys make a car sound, some make animal sounds, etc. They don’t have to be cars or animals, but they all need to make sound. The interface just says, “Hey, you must be able to make a sound,” but you don’t care what sound it is.

interface SoundMaking {
    void makeSound();  // Every toy must make a sound, but how is up to them
}

class CarToy implements SoundMaking {
    public void makeSound() {
        System.out.println("Vroom Vroom!");
    }
}

class AnimalToy implements SoundMaking {
    public void makeSound() {
        System.out.println("Roar!");
    }
}

Key Differences:

  • Abstract Class:
    • Can have some code and some abstract methods (like a blueprint with instructions).
    • You can put common things that are shared by many things (like wheels in cars).
    • You can only inherit from one abstract class.
  • Interface:
    • Can only have method signatures (just the rules, not the details).
    • Makes sure different objects all do the same thing (like make sound).
    • You can implement multiple interfaces.

In Summary:

  • Use an abstract class when you have things in common but also want some details to be different.
  • Use an interface when you just want to make sure that different objects do the same thing, but you don’t care how.