You are on page 1of 6

Name: Nakul Sharma

Practical no: 08

• Write a program to implements multilevel inheritance.

Code:-

<?php
class Fruit {
protected $name;

public function setName($name) {


$this->name = $name;
}

public function getName() {


return $this->name;
}
}

class CitrusFruit extends Fruit {


protected $vitaminCContent;

public function setVitaminCContent($vitaminCContent) {


$this->vitaminCContent = $vitaminCContent;
}

public function getVitaminCContent() {


return $this->vitaminCContent;
}
}
class Orange extends CitrusFruit {
public function displayInfo() {
echo "I am an " . $this->getName() . ". I belong to the citrus
family and have a vitamin C content of " . $this-
>getVitaminCContent() . " mg per 100g.\n";
}
}

$orange = new Orange();


$orange->setName("Orange");
$orange->setVitaminCContent(53);
$orange->displayInfo();
?>

Output:

• Write a program to implements multiple inheritance

Code:-

<?php
interface FruitProperties {
public function setName($name);
public function getName();
}

interface CitrusFruitProperties {
public function setVitaminCContent($vitaminCContent);
public function getVitaminCContent();
}

class Fruit implements FruitProperties {


protected $name;

public function setName($name) {


$this->name = $name;
}

public function getName() {


return $this->name;
}
}

class CitrusFruit extends Fruit implements CitrusFruitProperties {


protected $vitaminCContent;

public function setVitaminCContent($vitaminCContent) {


$this->vitaminCContent = $vitaminCContent;
}

public function getVitaminCContent() {


return $this->vitaminCContent;
}
}

class Orange extends CitrusFruit implements FruitProperties,


CitrusFruitProperties {
public function displayInfo() {
echo "I am an " . $this->getName() . ". I belong to the citrus
family and have a vitamin C content of " . $this-
>getVitaminCContent() . " mg per 100g.\n";
}
}

$orange = new Orange();


$orange->setName("Orange");
$orange->setVitaminCContent(53);
$orange->displayInfo();
?>

Output:-

• Write a program to demonstrate parameterized constructor

Code:-

<?php
class MyClass {
private $name;

public function __construct($initialName) {


$this->name = $initialName;
}

public function getName() {


return $this->name;
}
}

$obj = new MyClass("Nakul");


echo "Name: " . $obj->getName() . "\n";
?>
Output:

• Write a program to demonstrate default constructor.

Code:

<?php
class MyClass {
private $name;

public function __construct() {


$this->name = "DefaultName";
}

public function getName() {


return $this->name;
}
}

$obj = new MyClass();


echo "Name: " . $obj->getName() . "\n";
?>

Output:

You might also like