Abstract classes in PHP are declared with the <abstract>
keyword. They cannot be instantiated directly and are designed to be inherited by other classes.
Abstract classes provide a blueprint for other classes. They can include abstract methods, which must be defined in the child classes.
abstract class Animal {
abstract public function makeSound();
public function sleep() {
echo "Sleeping...";
}
}
Abstract methods are declared without a body and must be implemented by subclasses. They define a contract that all derived classes must follow.
Consider an abstract class Animal
with an abstract method makeSound()
. Any subclass must implement this method.
Console Output:
Sleeping...
When a class extends an abstract class, it must implement all abstract methods. This ensures consistency across subclasses.
class Dog extends Animal {
public function makeSound() {
echo "Woof!";
}
}
Here, the Dog
class extends the Animal
abstract class and provides an implementation for the makeSound()
method.
Console Output:
Woof!
Abstract classes can also have properties, which can be used by subclasses to store data relevant to the class.
abstract class Vehicle {
protected $speed;
abstract public function accelerate();
}
The Vehicle
abstract class has a protected property $speed
, which is accessible to its subclasses.
Console Output:
No direct output, property demonstration.
Abstract classes can have constructors, which can be used to initialize properties that are common to all subclasses.
abstract class Device {
protected $brand;
public function __construct($brand) {
$this->brand = $brand;
}
abstract public function operate();
}
The constructor in the Device
class initializes the $brand
property, which is accessible to subclasses.
Console Output:
No direct output, constructor demonstration.
An abstract class can contain multiple abstract methods, each of which must be implemented by subclasses.
abstract class Shape {
abstract public function area();
abstract public function perimeter();
}
The Shape
abstract class defines two abstract methods, area()
and perimeter()
, which must be implemented by any subclass.
Console Output:
No direct output, method declaration.
Abstract classes can have final methods, which cannot be overridden by subclasses, ensuring consistent behavior.
abstract class Appliance {
final public function plugIn() {
echo "Appliance plugged in.";
}
abstract public function use();
}
The plugIn()
method in the Appliance
class is final, meaning it cannot be changed by subclasses.
Console Output:
Appliance plugged in.
When a class inherits from an abstract class, it gains access to its implemented methods and must implement its abstract methods.
class Cat extends Animal {
public function makeSound() {
echo "Meow!";
}
}
The Cat
class inherits from the Animal
abstract class and implements the makeSound()
method.
Console Output:
Meow!
Abstract classes can have both implemented and abstract methods, while interfaces can only have abstract methods. Abstract classes can also have properties, whereas interfaces cannot.
// Abstract class example
abstract class Machine {
abstract public function start();
public function stop() {
echo "Machine stopped.";
}
}
// Interface example
interface Operatable {
public function operate();
}
The Machine
class is an abstract class with both an abstract and a concrete method, while Operatable
is an interface with only abstract methods.
Console Output:
Machine stopped.
A payment system can use an abstract class to define a template for different payment methods, ensuring each method implements necessary operations.
abstract class Payment {
abstract public function processPayment($amount);
public function validate() {
echo "Payment validated.";
}
}
class CreditCardPayment extends Payment {
public function processPayment($amount) {
echo "Processing credit card payment of $amount.";
}
}
The Payment
abstract class defines a structure for payment processing, while CreditCardPayment
implements the specific logic for credit card transactions.
Console Output:
Payment validated. Processing credit card payment of $amount.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies