Abstract Class and Interface

In this post, we learn what is an abstract class and an interface.
  • What is an Abstract Class?
  • Private methods cannot be Abstract
  • What is an Interface?
  • Defining an Interface
  • Abstract Class v/s Interface

What is an Abstract Class?

An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract.
Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created
To define a class as Abstract, the keyword abstract is to be used e.g. abstract class ClassName { }
Abstract Class Example :
 
abstract class Furniture {
 private $height, width, length;
 
 public function setData($h, $w, $l) {
  $this->height = $h;
  $this->width = $w;
  $this->length = $l;
 }
 
        //this function is declared as abstract and hence the function
        //body will have to be provided in the child class
 public abstract function getPrice();
 
}
 
 
class BookShelf extends Furniture {
 
   private $price;
 
   public setData($h, $w, $l, $p) {
      parent::setData($h, $w, $l);
      $this->price = $p;
   }
 
 
   //this is the function body of the parent abstract method
   public function getPrice() {
      return $this->price;
   }
}
In the above example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the responsibility of the child class to provide the functionality of getPrice(). The BookShelf class is a child of the Furniture class and hence provides the function body for getPrice().

Private methods can not be abstract

If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.
abstract class BaseClass {
 
   private abstract function myFun();
 
}
 
class DerivedClass extends BaseClass {
   public function myFun() {
    //logic here
   }
}
 
$d = new DerivedClass(); //will cause error

What is an Interface ?

An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour.
To extend from an Interface, keyword implements is used.
We can have a class extend from more than one Interface.
 
interface Storable {
 function getContentsAsText();
}
 
class Document implements Storable {
 public function getContentsAsText() {
  return "This is Text of the Document\n";
 }
}
 
class Indexer {
 public function readAndIndex(Storable $s) {
  $textData = $s->getContentsAsText();
  //do necessary logic to index
  echo $textData;
 }
}
 
$p = new Document();
 
$i = new Indexer();
$i->readAndIndex($p);
In the above example, Document and the Indexer class are two independant classes. The Indexer class is designed to index the contents of any text. Using the Storable interface above, we declare a method getContentsAsText() in the Document class. Because the Indexer class is only concerned with the TEXT, hence we can call getContentsAsText() method on the object of Document. This way any class if it implements the method getContentsAsText() can get indexed

Difference between Abstract Class and Interface

Abstract Classes
  1. An abstract class can provide some functionality and leave the rest for derived class
  2. The derived class may or may not override the concrete functions defined in base class
  3. The child class extended from an abstract class should logically be related
Interface
  1. An interface cannot contain any functionality. It only contains definitions of the methods
  2. The derived class must provide code for all the methods defined in the interface
  3. Completely different and non-related classes can be logically be grouped together using an interface
  4. Subscribe below for updates of Artical

No comments: