SlideShare a Scribd company logo
2
Most read
www.vineetsaini.wordpress.com
Abstract Class & Interface in PHP
Abstract Class
Abstract classes are those classes which can not be directly initialized. Or in other word we
can say that you can’t create object of abstract classes.
Abstract classes always created for inheritance purpose. You can only inherit abstract class
in your child class. Many peoples say that in abstract class at least your one method should
be abstract. Abstract method are the method which is only defined but declared. But your
any class has at least one method abstract than your class is abstract class.
Abstract class are also known as base class. We call it base class because abstract class are
not the class which is available directly for creating object. It can only act as parent class of
any normal class. You can use abstract class in class hierarchy. It means one abstract class
can inherit another abstract class also.
We can create abstract classes in php using abstract keyword. Once we will make any class
abstract in php we can’t create object of that class.
1. An abstract method contains no body.
2. An abstract method is not implemented by the base class.
3. There can be no objects of an abstract class.
4. An abstract class can inherit from a class and one or more interfaces.
5. An abstract class can implement code with non-Abstract methods.
6. An abstract class can have constructors or destructor's.
7. An abstract class cannot support multiple inheritance.
<?php
abstract class testParent
{
public function abc()
{
echo 'Vineet';
}
}
class testChild extends testParent
{
public function xyz()
{
echo 'Saini';
}
}
$a = new testChild();
echo $a->abc();
echo $a->xyz();
?>
www.vineetsaini.wordpress.com
Interface
We know that PHP does not support the multiple inheritance. For support multiple
inheritance in php we use Interface.
Interface is just like a class using interface keyword and contains only function declarations
(function with no body). Function is defined in the classes where you call it.
Interface gets useful when you need to extend a class from more than one parent class
since class can’t do this.
An interface cannot contain any functionality. It only contains definitions of the methods
<?php
interface template1
{
public function f1();
}
class test implements template1
{
public function f1()
{
echo 'Vineet';
}
public function f2()
{
echo 'Saini';
}
}
$a = new test();
echo $a->f1();
echo $a->f2();
?>
Differences between abstract class and interface in PHP
Following are some main difference between abstract classes and interface in php.
1. In abstract classes this is not necessary that every method should be abstract. But in
interface every method is abstract.
2. Multiple and multilevel both type of inheritance is possible in interface. But single
and multilevel inheritance is possible in abstract classes.
3. Method of php interface must be public only. Method in abstract class in php could
be public or protected both.
4. In abstract class you can define as well as declare methods. But in interface you can
only defined your methods.

More Related Content

PPTX
What is Kernel, basic idea of kernel
PPTX
Constructors in C++
PPTX
Linux ppt
PPTX
C Language (All Concept)
PPTX
Inheritance in c++
PDF
Arm device tree and linux device drivers
PPT
Introduction to System Calls
PPTX
Overloading and overriding in vb.net
What is Kernel, basic idea of kernel
Constructors in C++
Linux ppt
C Language (All Concept)
Inheritance in c++
Arm device tree and linux device drivers
Introduction to System Calls
Overloading and overriding in vb.net

What's hot (20)

PPTX
Interface in java
PPTX
JAVA AWT
PPT
Error reporting in php
PPTX
This keyword in java
PPTX
INHERITANCE IN JAVA.pptx
PPT
Os Threads
PDF
Computer Organization and Architecture.pdf
ODP
Linux Internals - Kernel/Core
PPTX
Presentation on-exception-handling
PPTX
Event Handling in java
PDF
Q2.12: Debugging with GDB
PPT
01 Php Introduction
PPTX
Introduction to Debuggers
PPTX
Inheritance in JAVA PPT
PPT
Linux seminar
PPTX
LINUX DISTRIBUTIONS.pptx
PPTX
Unit 5 java-awt (1)
PPTX
PDF
Course 101: Lecture 5: Linux & GNU
PPTX
Introduction to system programming
Interface in java
JAVA AWT
Error reporting in php
This keyword in java
INHERITANCE IN JAVA.pptx
Os Threads
Computer Organization and Architecture.pdf
Linux Internals - Kernel/Core
Presentation on-exception-handling
Event Handling in java
Q2.12: Debugging with GDB
01 Php Introduction
Introduction to Debuggers
Inheritance in JAVA PPT
Linux seminar
LINUX DISTRIBUTIONS.pptx
Unit 5 java-awt (1)
Course 101: Lecture 5: Linux & GNU
Introduction to system programming
Ad

Viewers also liked (7)

PPT
Functions in php
PDF
Php tutorial
PDF
Php Tutorials for Beginners
PPT
Class 3 - PHP Functions
PPTX
PHP Functions & Arrays
PDF
Functions in PHP
PDF
Sorting arrays in PHP
Functions in php
Php tutorial
Php Tutorials for Beginners
Class 3 - PHP Functions
PHP Functions & Arrays
Functions in PHP
Sorting arrays in PHP
Ad

Similar to Abstract Class and Interface in PHP (20)

PDF
Web Design & Development - Session 9
PDF
Abstract Class and Interface.pdf
PDF
8 abstract classes and interfaces
PPTX
Lecture-10_PHP-OOP.pptx
PPT
Java interfaces & abstract classes
PPTX
OOPS Characteristics (With Examples in PHP)
PPTX
what is differance between abstract class and interface ppt
PPTX
Object oreinted php | OOPs
PPTX
Only oop
PPTX
Abstraction1
PPT
UNIT-IV WT web technology for 1st year cs
PPTX
Abstract class and Interface
PPTX
Abstraction
PDF
Effective PHP. Part 4
PPTX
2CPP14 - Abstraction
PDF
Object_oriented_programming_OOP_with_PHP.pdf
PPTX
Oopsinphp
PPT
Inheritance
PPTX
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
Web Design & Development - Session 9
Abstract Class and Interface.pdf
8 abstract classes and interfaces
Lecture-10_PHP-OOP.pptx
Java interfaces & abstract classes
OOPS Characteristics (With Examples in PHP)
what is differance between abstract class and interface ppt
Object oreinted php | OOPs
Only oop
Abstraction1
UNIT-IV WT web technology for 1st year cs
Abstract class and Interface
Abstraction
Effective PHP. Part 4
2CPP14 - Abstraction
Object_oriented_programming_OOP_with_PHP.pdf
Oopsinphp
Inheritance
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx

More from Vineet Kumar Saini (20)

PDF
Add edit delete in Codeigniter in PHP
PDF
Introduction to Html
PDF
Computer Fundamentals
PDF
Country State City Dropdown in PHP
PDF
Pagination in PHP
PDF
Stripe in php
PDF
Install Drupal on Wamp Server
PDF
Joomla 2.5 Tutorial For Beginner PDF
PDF
Dropdown List in PHP
PDF
Update statement in PHP
PDF
Delete statement in PHP
PDF
Implode & Explode in PHP
PDF
Types of Error in PHP
PDF
GET and POST in PHP
PDF
Database connectivity in PHP
PDF
Arrays in PHP
PDF
Programming in C
PDF
Browser information in PHP
PDF
Operators in PHP
PDF
Variables in PHP
Add edit delete in Codeigniter in PHP
Introduction to Html
Computer Fundamentals
Country State City Dropdown in PHP
Pagination in PHP
Stripe in php
Install Drupal on Wamp Server
Joomla 2.5 Tutorial For Beginner PDF
Dropdown List in PHP
Update statement in PHP
Delete statement in PHP
Implode & Explode in PHP
Types of Error in PHP
GET and POST in PHP
Database connectivity in PHP
Arrays in PHP
Programming in C
Browser information in PHP
Operators in PHP
Variables in PHP

Recently uploaded (20)

PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
Business Ethics Teaching Materials for college
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
English Language Teaching from Post-.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Week 4 Term 3 Study Techniques revisited.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
How to Manage Starshipit in Odoo 18 - Odoo Slides
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Business Ethics Teaching Materials for college
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Module 3: Health Systems Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
Onica Farming 24rsclub profitable farm business
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Cardiovascular Pharmacology for pharmacy students.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
UPPER GASTRO INTESTINAL DISORDER.docx
O7-L3 Supply Chain Operations - ICLT Program
The Final Stretch: How to Release a Game and Not Die in the Process.
English Language Teaching from Post-.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Anesthesia in Laparoscopic Surgery in India
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Abstract Class and Interface in PHP

  • 1. www.vineetsaini.wordpress.com Abstract Class & Interface in PHP Abstract Class Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can’t create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Many peoples say that in abstract class at least your one method should be abstract. Abstract method are the method which is only defined but declared. But your any class has at least one method abstract than your class is abstract class. Abstract class are also known as base class. We call it base class because abstract class are not the class which is available directly for creating object. It can only act as parent class of any normal class. You can use abstract class in class hierarchy. It means one abstract class can inherit another abstract class also. We can create abstract classes in php using abstract keyword. Once we will make any class abstract in php we can’t create object of that class. 1. An abstract method contains no body. 2. An abstract method is not implemented by the base class. 3. There can be no objects of an abstract class. 4. An abstract class can inherit from a class and one or more interfaces. 5. An abstract class can implement code with non-Abstract methods. 6. An abstract class can have constructors or destructor's. 7. An abstract class cannot support multiple inheritance. <?php abstract class testParent { public function abc() { echo 'Vineet'; } } class testChild extends testParent { public function xyz() { echo 'Saini'; } } $a = new testChild(); echo $a->abc(); echo $a->xyz(); ?>
  • 2. www.vineetsaini.wordpress.com Interface We know that PHP does not support the multiple inheritance. For support multiple inheritance in php we use Interface. Interface is just like a class using interface keyword and contains only function declarations (function with no body). Function is defined in the classes where you call it. Interface gets useful when you need to extend a class from more than one parent class since class can’t do this. An interface cannot contain any functionality. It only contains definitions of the methods <?php interface template1 { public function f1(); } class test implements template1 { public function f1() { echo 'Vineet'; } public function f2() { echo 'Saini'; } } $a = new test(); echo $a->f1(); echo $a->f2(); ?> Differences between abstract class and interface in PHP Following are some main difference between abstract classes and interface in php. 1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract. 2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes. 3. Method of php interface must be public only. Method in abstract class in php could be public or protected both. 4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.