xxxxxxxxxx
<?php
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>
xxxxxxxxxx
<?php
abstract class Cars
{
abstract function car_details();
}
class Maruti extends Cars
{
function car_details()
{
echo "Maruti Details";
}
}
class Honda extends Cars
{
function car_details()
{
echo "<br>Honda Details<br>";
}
}
class Scoda extends Cars
{
function car_details()
{
echo "<br>Scoda Details<br>";
}
}
$obj = new Maruti();
$obj -> car_details();
$obj1 = new Honda();
$obj1 -> car_details();
$obj2 = new Scoda();
$obj2 -> car_details();
?>
xxxxxxxxxx
<?php
// abstract definition
abstract class Cars {
public $car_name;
public function __construct($car_name) {
$this->car_name = $car_name;
}
abstract public function carDetails();
}
// Class definitions
class Tata extends Cars {
public function carDetails() {
echo " $this->car_name Car Details \n";
}
}
class Audi extends Cars {
public function carDetails() {
echo " $this->car_name Car Details \n";
}
}
class Honda extends Cars {
public function carDetails() {
echo " $this->car_name Car Details \n";
}
}
class Maruti extends Cars {
public function carDetails() {
echo " $this->car_name Car Details \n";
}
}
// Create a list of Cars object
$objTata = new Tata('Tata');
print $objTata->carDetails();
$objAudi = new Audi('Audi');
print $objAudi->carDetails();
$objHonda = new Honda('Honda');
print $objHonda->carDetails();
$objMaruti = new Maruti('Maruti');
print $objMaruti->carDetails();
?>
xxxxxxxxxx
<?php
/**
*Abstract Methods and Classes
*An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
*An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
*abstract void car_details(char model, double price);
*If a class includes abstract methods, then the class itself must be declared abstract, as in:
* public abstract class Cars {
* // declare fields
* // declare nonabstract methods
* abstract void car_details();
* }
* When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
* In Abstract class someone can write the definitation and someone can use/extend it, abstract means incomplete.
* It is not possible to create object of abstract method
* abstract class contains atleast 1 abstract function, it can have abstract and non abstract methods.
* abstract function can not contain body or definitation it must declare, but should not have function definitation.
* abstract class, child class must contain abstract function
*/
abstract class Cars
{
abstract function car_details();
}
class Maruti extends Cars
{
function car_details()
{
echo "Maruti Details";
}
}
class Honda extends Cars
{
function car_details()
{
echo "<br>Honda Details<br>";
}
}
class Scoda extends Cars
{
function car_details()
{
echo "<br>Scoda Details<br>";
}
}
$obj = new Maruti();
$obj -> car_details();
$obj1 = new Honda();
$obj1 -> car_details();
$obj2 = new Scoda();
$obj2 -> car_details();
?>
xxxxxxxxxx
<?php
abstract class father
{
public abstract function abstractMethod();
public function normalFunction()
{
print "I am from normal function of Abstract class \n";
}
}
class son extends father
{
public function abstractMethod()
{
print "abstractMethod inherited from Abstract class \n";
}
}
$obj = new son();
$obj->normalFunction();
$obj->abstractMethod();
?>
xxxxxxxxxx
// Define an abstract class
abstract class Animal {
protected $name;
// Abstract method that needs to be implemented by the child classes
abstract public function makeSound();
// Non-abstract method that can be used by child classes
public function setName($name) {
$this->name = $name;
}
}
// Creating a child class that extends the abstract class
class Cat extends Animal {
public function makeSound() {
return "Meow!";
}
}
// Creating an instance of the child class
$cat = new Cat();
$cat->setName("Tom");
// Accessing the method from the abstract class
echo $cat->makeSound(); // Output: Meow!
// Accessing the method from the child class
echo $cat->name; // Output: Tom