Abstract Class |
---|
Abstract class is a class that can't be instantiated but it can be inherited by sub class and provide its own implementation for the abstract method.
It represent an identity of some closely RELATED class. For e.g. a Vehicle abstract class can have abstract method call start & this identity is shared by closely related class such as car & bike which have their own mechanism to start a engine.
abstract class Vehicle { protected abstract function start(); } Car class extends Vehicle { protected function start() { //Car own mechanism to start a engine } } Motorcycle extends Vehicle { protected function start() { //Motor cycle mechanism to start a engine } } |
Don't support Multiple Inheritance: Subclass can inherit only one Abstract Class. |
Abstract class, beside abstract method, can also have other method with its own implementation. |
Interface |
---|
Like Abstract class, Interface cannot be instantiated. But rather than class it is like a contract or a pattern that represent an ability or capability of some UNRELATED class to do something. For e.g. if we have EnvironmentalFriendly interface then this capability, ability or contract can be shared my any unrelated class. A Car can be environmental friendly, A Furniture can be environment friendly.
interface EnvironmentalFriendly { public function recycle(); } Car class extends Vehicle implements EnvironmentalFriendly { protected function start() { //Car own mechanism to start a engine } public function recycle() { // Car own recycle criteria } } Motorcycle extends Vehicle implements EnvironmentalFriendly { protected function start() { //Motor cycle mechanism to start a engine } public function recycle() { // MotorCycle own recycle criteria } } Furniture class implements EnvironmentalFriendly { public function recycle() { // Cloth own recycle criteria } } |
Supports Multiple Inheritance: Subclass can implement multiple Interface. |
Interface method have not actual implementation. Subclass implementing Interface would provide its own implementation |