Abstraction means to represent the essential feature to hide irrelevant details or internal implementation details, without detailing the background implementation or internal working detail.
Abstraction places the emphasis on what an object is or does rather than how it is represented or how it works.
Abstraction helps in building independent modules, lets you focus on what the object does instead of how it is done.
The person who uses that module does not need to bother about how the task is achieved or what exactly is happening in the background.
Modifying one independent module does not impact the other modules.
Abstraction is everywhere. One example is the phone. You interact with the phone by using only a few buttons. You don’t have to know how it work. Implementation changes(for example, a software update) does not impact you.
In OOP, Abstract class
is the concept described what to do and implementation gets completed when it is being realized by a subclass.
That is:
Abstract class
: what to do
Subclass inherited from abstract class
: How to do
Abstract classes, which declared with the abstract
keyword, cannot be instantiated, and any class that contains at least one abstract method must also be abstract.
<?php abstract class AbstractClass { // Force Extending class to define this method abstract protected function getValue(); // Common method public function render() { echo $this->getValue() . "\n"; } } // $obj = new AbstractClass(); // Fatal Error,Abstract class cannot be instantiated ?>
Methods(like getValue()
) defined as abstract simply declare the method's signature - they cannot define the implementation. (They don't have a body,no curly brackets {})
The class inherited from abstract class must override all abstract method.So, Abstract method's visibility (getValue
) can either be public or protected, but not private.
Abstract class can have non-abstract methods :render()
Abstract class example:
<?php abstract class AbstractClass { abstract protected function getValue(); abstract protected function prefixValue($prefix); abstract protected function prefixName($name); }
When inheriting from an abstract class,
all methods marked abstract in the parent's class declaration must be defined by the child.
these methods must be defined with the same (or a less restricted) visibility.
Abstract Method's Visibility | Child Method's Visibility |
---|---|
Public | Public |
Protected | Protected or Public |
For example, method prefixValue($prefix)
has one argument, The method prefixValue
in the child class should have the same arguments.
public function prefixValue($prefix) { }
public function prefixName($name, $separator = ".") { }
Phone
describes the features of phones (what to do)$myPhone
is an actual phone in your hand.<?php abstract class Phone { abstract public function sendSMS($msg); } class ApplePhone extends Phone { public function sendSMS($msg) { return "Sending text: " . $msg; } } $myPhone = new ApplePhone(); echo $myPhone -> sendSMS("Hello Lautturi."); ?>