abstraction in php

In the last lesson we have learnt abut one of the principle of Object Oriented Programming i.e Inheritance. Today we are going to learn about OOPs second principle i.e Abstraction. So let’s start with the meaning of abstraction.

In general term abstraction means incomplete or something which is removed from original thing.

In Object oriented concept, Abstraction provides a standard to the programmer where a method is defined in the abstract class but its body is left for the programmer. I will clear it more with real life example.

Situation : Your company got a calculator project and your manager creates a class name calculator and then he assigned this program to you in which you have to perform sum, subtract, multiply,divide operations.

  • Syntax : We use abstract keyword before the class as illustrated below.

  • Abstract class must have a method name followed by abstract keyword.

  • Abstract methods which are inside abstract class does not have its body as illustrated above.

Here need to pay attention, Manager created a standard that the calculator have functionality of sum, subtract, multiply and divide.

Now each of the methods developed by you is given below.

Output :

6
2
8
2

  • Abstract class can’t be instantiated directly. It means you can’t create object of abstract class directly. If you want to use the abstract class you have to extends the abstract class then you can use it.
  • If there is a method which is abstract in the class then, that class must be abstract.
  • Abstract class can have non-abstract methods and properties too, but atleast one method should be declared without its body.

 

Leave a Reply

Your email address will not be published. Required fields are marked *