logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

Difference between abstract Class and Interface


Difference between abstract Class and Interface

In PHP, abstract classes and interfaces are both tools for achieving abstraction and defining a contract for classes, but they have some key differences. Here are the main distinctions between abstract classes and interfaces:

 

  1. Abstract Class:

    • An abstract class can contain both abstract (methods without a body) and concrete methods (methods with a body).
    • It may have properties (attributes) with various access modifiers.
    • It can have constructors, which are called when an object of the class is instantiated.
    • A class can extend only one abstract class.

    Example of an abstract class:

    abstract class Animal {
        protected $name;
    
        public function __construct($name) {
            $this->name = $name;
        }
    
        abstract public function makeSound();
    
        public function eat() {
            echo "{$this->name} is eating.";
        }
    }
  2. Interface:

    • An interface can only have method signatures (abstract methods) without a body.
    • It cannot contain properties, constants, or method implementations.
    • A class can implement multiple interfaces.
    • It provides a way to achieve multiple inheritance in PHP.

    Example of an interface:

    interface Logger {
        public function log($message);
    }

 

Key Points:

  • Use an abstract class when you want to provide a common base class with some shared functionality and allow derived classes to override or extend it.

  • Use an interface when you want to define a contract that multiple classes can implement. This is especially useful when those classes may not share a common base implementation.

  • You can combine both abstract classes and interfaces. A class can extend an abstract class and implement one or more interfaces.

abstract class Animal {
    // Abstract methods and shared functionality
}

interface Swimmer {
    public function swim();
}

class Fish extends Animal implements Swimmer {
    // Implement abstract methods and interface method
}

 

In summary, abstract classes provide a way to share code and define a base class, while interfaces provide a contract that classes can adhere to without providing any implementation details. The choice between them depends on the specific requirements of your design.

 

Thank you.

Popular Post:

Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment