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 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 { }.

example:
	<?php
	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;
		}
	}
	?>
Description :

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 cannot 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.

example:
	<?php
	abstract class BaseClass {
		private abstract function myFun();
	}
	class DerivedClass extends BaseClass {
	    public function myFun() {
		    //logic here
	    }
	}
	$d = new DerivedClass(); //will cause error
	?>

Questions for Abstract Class

  1. What is Abstract class ?Explain with example?

Interface :

An interface is a contract between unrelated objects to perform a common function on the fly. 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. It's basically knows as dynamic binding , meaning the class which implements the interface provides the body dynamically .

To use an Interface, keyword implements is used.

Note : We can have a class extend from more than one Interface.
example:
    <?php
    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);
    ?>
Description :

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.

Questions for Interface

  1. What is an Interface?Explain with example?
  2. What is difference between Abstract class and Interface?

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 .

After editing Click here:
Output:
Hello World