php abstract class without abstract methods in 2021

php abstract class without abstract methods in 2021
php abstract class without abstract methods in 2021

Php Abstract Class Without Abstract Methods in 2021


Abstract class in PHP: Abstract class is an important element in PHP which is declared with abstract keyword. It is a collection of abstract methods/functions or non-abstract methods. We can not create an instance(object) of an abstract class. 

We can not define an abstract function inside the abstract class only can be declared, so it is the responsibility of the derived class to implement the method/function of an abstract class. An abstract class is also inherited by a class using extended keywords.

Python Language

How to use Abstract class without abstract method in PHP?

<?php

abstract class Rectangle

{

public function rec_area()

  {

  $h=20;

  $w=10;

  echo "Area of Rectangle:".($h*$w)."<br>";

  }

}

class Triangle extends Rectangle{

  public function tri_area()

  {

  $b=25;

  $h=30;

  echo "Area of Triangle:".(0.5*$b*$h)."<br>";

  }

}

$obj = new Triangle();

$obj->rec_area();

$obj->tri_area();

?>

*****OUTPUT*****

Area of Rectangle:200

Area of Triangle:375

Abstract method and function in PHP: abstract function is declared with abstract keyword. It can be declared only inside the abstract class. It can not be defined inside an abstract class, only can be declared so it is the responsibility of the derived class to implement the abstract method.

Java Language

How to use the Abstract class with the abstract method in PHP?

<?php

abstract class Geometry

{

 abstract public function rec_area();

 abstract public function tri_area();

 }

 class Derived extends Geometry

 {

public function rec_area()

  {

  $h=20;

  $w=10;

  echo "Area of Rectangle:".($h*$w)."<br>";

  }

  public function tri_area()

  {

  $b=25;

  $h=30;

  echo "Area of Triangle:".(0.5*$b*$h)."<br>";

  }

}

$obj = new Derived();

$obj->rec_area();

$obj->tri_area();

?>

*****OUTPUT*****

Area of Rectangle:200

Area of Triangle:375

How To use Abstract class with the abstract and non-abstract methods in PHP?

<?php

abstract class Geometry

{

abstract public function rec_area();

   public function tri_area()

  {

  $b=25;

  $h=30;

  echo "Area of Triangle:".(0.5*$b*$h)."<br>";

  }

 }

 class Derived extends Geometry

 {

 public function rec_area()

  {

  $h=20;

  $w=10;

  echo "Area of Rectangle:".($h*$w)."<br>";

  }

}

$obj = new Derived();

$obj->rec_area();

$obj->tri_area();

?>

*****OUTPUT*****

Area of Rectangle:200

Area of Triangle:375

Post a Comment

0 Comments