Q:

PHP find output programs (Inheritance) | set 3

0

Find the output of PHP programs | Inheritance | Set 3: Enhance the knowledge of PHP Inheritance by solving and finding the output of some PHP programs.

Question 1:

<?php
    class A
    {
        function fun()
        {
            echo "Fun() called from class A<br>";
        }
    
    }
    
    class B extends A
    {
        function fun()
        {
            echo "Fun() called from class B<br>";
        }
    }
    
    $obj = new B();
    $obj->fun();
?>

Question 2:

<?php
    class Vehicle
    {
        private $EngineNo;
        private $ChasisNo;
    
        public function Set($E, $C)
        {
            $this->EngineNo = $E;
            $this->ChasisNo = $C;
        }
    
        public function print ()
        {
            echo "<br>Engine Number: " . $this->EngineNo;
            echo "<br>Chasis Number: " . $this->ChasisNo;
        }
    }
    
    class Car extends Vehicle
    {
        private $ModelNo;
        private $Price;
    
        function SetCar($E, $C, $M, $P)
        {
            $this->ModelNo = $M;
            $this->Price = $P;
    
            $this->Set($E, $C);
        }
    
        function PrintCar()
        {
            $this->Print();
            echo "<br>Model Number: " . $this->ModelNo;
            echo "<br>Price: " . $this->Price;
        }
    }
    
    $obj = new Car();
    $obj->SetCar("E101", "CH101", "M101", 1000000);
    $obj->PrintCar();
?>

Question 3:

<?php
    class Person
    {
        private $Name;
        private $Age;
    
        public function Set($N, $A)
        {
            $this->Name = $N;
            $this->Age = $A;
        }
    
        public function print ()
        {
            echo "<br>Name: " . $this->Name;
            echo "<br>Age:  " . $this->Age;
        }
    }
    
    class Employee extends Person
    {
        private $Dept;
        private $Salary;
    
        function SetEmp($N, $A, $D, $S)
        {
            $this->Dept = $D;
            $this->Salary = $S;
    
            $this->Set($N, $A);
        }
    
        function PrintEmp()
        {
            $this->Print();
            echo "<br>Dept:   " . $this->Dept;
            echo "<br>Salary: " . $this->Salary;
        }
    }
    
    $obj1 = new Employee();
    $obj1->SetEmp("Rohit", 32, "Accounts", 1000);
    $obj1->PrintEmp();
    
    $obj2 = new Employee();
    $obj2->SetEmp("Virat", 31, "HR", 1500);
    $obj2->PrintEmp();
?>

Question 4:

<?php
    class A
    {
        function fun()
        {
            echo "Fun() called from class A<br>";
        }
    
    }
    class B
    {
        function fun()
        {
            echo "Fun() called from class B<br>";
        }
    }
    
    class C extends A, extends B
    {
        function Mymethod()
        {
            echo "Mymethod() called from class C<br>";
        }
    }
    
    $obj = new C();
    $obj->Mymethod();
?>

Question 5:

<?php
class A
{
    function fun1()
    {
        echo "Fun1() called from class A<br>";
    }

}
class B:
    A
    {
        function fun2()
        {
            echo "Fun2() called from class B<br>";
        }
    }

    $obj = new B();
    $obj->fun2();
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

Fun() called from class B

Explanation:

In the above program, we created two classes, class A inherited in class B, class A, and B both contain a function with the same name that is fun(). We created the object of class B then it will call function fun() of class B. Defining of member function with the same name in the child class is known as method overriding.

Answer 2:

Output:

Engine Number: E101
Chasis Number: CH101
Model Number: M101
Price: 1000000

Explanation:

In the above program, we created two classes Vehicle and Car.

The Vehicle class contains data members $EngineNo, $ChasisNo. And methods Set(), Print(). 

The Car class contains data members $ModelNo, $Price. And methods SetCar(), PrintCar(). 

The SetCar() method calls Set() method of Vehicle class and PrintCar() calls Print() method of Vehicle class.

Then we created an object of Car class, then set and print the values of data members.

Answer 3

Output:

Name: Rohit
Age: 32
Dept: Accounts
Salary: 1000
Name: Virat
Age: 31
Dept: HR
Salary: 1500

Explanation:

In the above program, we created two classes Person and Employee.

The Person class contains data members $Name, $Age. And methods Set(), Print().

The Employee class contains data members $Dept, $Salary. And methods SetEmp(), PrintEmp().

The SetEmp() method calls Set() method of Vehicle class and PrintEmp() calls Print() method of Vehicle class.

Then we created two objects of Employee class, then set and print the values of data members.

Answer 4:

Output:

PHP Parse error:  syntax error, unexpected ',', expecting '{' 
in /home/main.php on line 18

Explanation:

The above program will generate syntax error, because PHP does not support multiple inheritance, to implement multiple inheritance we need to use interface or traits in PHP.

Answer 5:

Output:

PHP Parse error:  syntax error, unexpected ':', expecting '{' 
in /home/main.php on line 10

Explanation:

The above program will generate syntax error because we cannot inherit a class using the colon ":" operator in PHP, for the inheritance we need to use the extends keyword in PHP.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now