Inheritance


computer apg

In Object-Oriented Programming, OOP an inheritance occurs when a class derives from another class.

The child class will inherit all the public and protected properties and methods from the parent class.

In addition, it can have its own properties and methods.



 extends 


The inherited class is defined using the extends keyword.



  1 EXERCISE   

<?php

class Dwarfs {
  public 
$name;
  public 
$behavior;
  
  public function 
__construct($name$behavior
  {
    
$this->name $name;
    
$this->behavior $behavior;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br>';
    echo 
$this->behavior '<br><br>';
  }
}

/* The class Dwarf is inherited from the class Dwarfs */

class Dwarf extends Dwarfs 
{
  public function 
message() 
  {
    echo 
"A real famous dwarf:<br><br>";
  }
}
$rd '1 - Tyrion Lanister.';
$bh '2 - played by Peter Dinklage.';
$Dwarf = new Dwarf($rd$bh);
$Dwarf->message();
$Dwarf->intro();
     
?>

 RESULT   

A real famous dwarf:

1 - Tyrion Lanister

2 - played by Peter Dinklage


  2 EXERCISE   

<?php

class Dwarfs {
  public 
$name;
  public 
$behavior;
  public 
$friend;
  public 
$age;
  
  public function 
get_name() 
    {
    return 
$this->name;
    }
    public function 
get_behavior() 
    {
    return 
$this->behavior;
    }
    public function 
get_friend() 
    {
    return 
$this->friend;
    }
    public function 
get_age() 
    {
    return 
$this->age;
    }   
    
  public function 
__construct($name$behavior$friend$age
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
  }
}

$nm '1 - Happy.';
$bh '2 - This dwarf is always cheerful and happy about everything.';
$fr '3 - Snow White friend.';
$ag '4 - 1937 - Year of the first performance of "Snow White and the Seven Dwarfs".';

$Dwarf_sw = new Dwarfs($nm$bh$fr$ag);

echo 
$Dwarf_sw->get_name();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_behavior();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_friend();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_age();
echo 
'<br><br><br>';

/* The class Dwarf is inherited from the class Dwarfs */

class Dwarf extends Dwarfs 
{
  public function 
message() 
  {
    echo 
"A real famous dwarf:<br><br>";
  }
}
$rd '1 - Tyrion Lanister';
$bi '2 - played by Peter Dinklage';
$fm '3 - in "The Game of Thrones".';
$aa '4 - Borned in June 11, 1969.';

$Dwarf = new Dwarf($rd$bi$fm$aa);

$Dwarf->message();
$Dwarf->intro();
     
?>

 RESULT   

1 - Happy.

2 - This dwarf is always cheerful and happy about everything.

3 - Snow White friend.

4 - 1937 - Year of the first performance of "Snow White and the Seven Dwarfs".

A real famous dwarf

1 - Tyrion Lanister

2 - played by Peter Dinklage

3 - in "The Game of Thrones".

4 - Borned in June 11, 1969.

  3 EXERCISE   

<?php

class Dwarfs {
  public 
$name;
  public 
$behavior;
  public 
$friend;
  public 
$age;
  
  public function 
get_name() 
    {
    return 
$this->name;
    }
    public function 
get_behavior() 
    {
    return 
$this->behavior;
    }
    public function 
get_friend() 
    {
    return 
$this->friend;
    }
    public function 
get_age() 
    {
    return 
$this->age;
    }   
    
  public function 
__construct($name$behavior$friend$age
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;
  }
  
  
/* - - - - - - - - - - - - - - - 
     A T T E N T I O N
     - - - - - - - - - - - - - - - */
  
  
protected function intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
  }
}

$nm '1 - Happy.';
$bh '2 - This dwarf is always cheerful and happy about everything.';
$fr '3 - Snow White friend.';
$ag '4 - 1937 - Year of the first performance of "Snow White and the Seven Dwarfs".';

$Dwarf_sw = new Dwarfs($nm$bh$fr$ag);

echo 
$Dwarf_sw->get_name();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_behavior();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_friend();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_age();
echo 
'<br><br><br>';

/* The class Dwarf is inherited from the class Dwarfs */

class Dwarf extends Dwarfs 
{
  public function 
message() 
  {
    echo 
"A real famous dwarf:<br><br>";
  }
}
$rd '1 - Tyrion Lanister';
$bi '2 - played by Peter Dinklage';
$fm '3 - in "The Game of Thrones".';
$aa '4 - Borned in June 11, 1969.';

$Dwarf = new Dwarf($rd$bi$fm$aa);

$Dwarf->message(); 
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  The result of message() will be displayed 
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$Dwarf->intro();   
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   The result of intro() will not be displayed
   because its visibility is protected.
   
   An ERROR will be issued.   
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     
?>

 RESULT   

        DO THE TEST YOURSELF          


  Study the displayed data carefully


  4 EXERCISE   

<?php

class Dwarfs {
  public 
$name;
  public 
$behavior;
  public 
$friend;
  public 
$age;
  
  public function 
get_name() 
    {
    return 
$this->name;
    }
    public function 
get_behavior() 
    {
    return 
$this->behavior;
    }
    public function 
get_friend() 
    {
    return 
$this->friend;
    }
    public function 
get_age() 
    {
    return 
$this->age;
    }   
    
  public function 
__construct($name$behavior$friend$age
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;
  }
  
  
/* - - - - - - - - - - - - - - - 
     A T T E N T I O N
     - - - - - - - - - - - - - - - */
  
  
protected function intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
  }
}

$nm '1 - Happy.';
$bh '2 - This dwarf is always cheerful and happy about everything.';
$fr '3 - Snow White friend.';
$ag '4 - 1937 - Year of the first performance of "Snow White and the Seven Dwarfs".';

$Dwarf_sw = new Dwarfs($nm$bh$fr$ag);

echo 
$Dwarf_sw->get_name();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_behavior();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_friend();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_age();
echo 
'<br><br><br>';

class 
Dwarf extends Dwarfs 
{
  
/* - - - - - - - - - - - - - - - 
     A T T E N T I O N
     - - - - - - - - - - - - - - - */
    
  
public function message() 
  {
    echo 
"A real famous dwarf:<br><br>";
    
$this->intro();
  }
}
$rd '1 - Tyrion Lanister';
$bi '2 - played by Peter Dinklage';
$fm '3 - in "The Game of Thrones".';
$aa '4 - Borned in June 11, 1969.';

$Dwarf = new Dwarf($rd$bi$fm$aa);

$Dwarf->message(); 
     
?>

 RESULT   

1 - Happy.

2 - This dwarf is always cheerful and happy about everything.

3 - Snow White friend.

4 - 1937 - Year of the first performance of "Snow White and the Seven Dwarfs".

A real famous dwarf

1 - Tyrion Lanister

2 - played by Peter Dinklage

3 - in "The Game of Thrones".

4 - Borned in June 11, 1969.


Although intro has visibility protected, it can be called by the message function whose visibility is public.

message is public and it calls intro, (which is protected), from within the derived class.

  5 EXERCISE   

<?php

class Dwarfs {
  public 
$name;
  public 
$behavior;
  public 
$friend;
  public 
$age;
  
  public function 
__construct($name$behavior$friend$age
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
  }
}

class 
Dwarf extends Dwarfs {
  public 
$height;
  public 
$weight;
  
  public function 
__construct($name$behavior$friend$age$height$weight)
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;    
    
$this->height $height;
    
$this->weight $weight;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
    echo 
$this->height .  '<br><br>';    
    echo 
$this->weight .  '<br><br>';
  }
}

$rd '1 - Tyrion Lanister';
$bi '2 - played by Peter Dinklage';
$fm '3 - in "The Game of Thrones".';
$aa '4 - Borned in June 11, 1969.';
$ht '5 - He has the height 4.3307ft = 1.32m';
$wt '6 - and the weight 110.231lbs = 50kg.';

$Dwarf = new Dwarf($rd$bi$fm$aa$ht$wt);
$Dwarf->intro();

?>

 RESULT   

1 - Tyrion Lanister

2 - played by Peter Dinklage

3 - in "The Game of Thrones".

4 - Borned in June 11, 1969.

5 - He has the height 4.3307ft = 1.32m

6 - and the weight 110.231lbs = 50kg.


This example allows us to conclude that Inherited methods can be overridden by redefining the methods, (use the same name), in the child class.

The __construct and intro methods in the child class, Dwarf, will override the __construct and intro methods in the parent class Dwarfs.

 final keyword 


The final keyword can be used to prevent class inheritance or to prevent method overriding.



  6 EXERCISE   

<?php

final class Dwarfs {
  public 
$name;
  public 
$behavior;
  public 
$friend;
  public 
$age;
  
  public function 
get_name() 
    {
    return 
$this->name;
    }
    public function 
get_behavior() 
    {
    return 
$this->behavior;
    }
    public function 
get_friend() 
    {
    return 
$this->friend;
    }
    public function 
get_age() 
    {
    return 
$this->age;
    }   
    
  public function 
__construct($name$behavior$friend$age
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
  }
}

$nm '1 - Happy.';
$bh '2 - This dwarf is always cheerful and happy about everything.';
$fr '3 - Snow White friend.';
$ag '4 - 1937 - Year of the first performance of "Snow White and the Seven Dwarfs".';

$Dwarf_sw = new Dwarfs($nm$bh$fr$ag);

echo 
$Dwarf_sw->get_name();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_behavior();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_friend();
echo 
'<br><br>';
echo 
$Dwarf_sw->get_age();
echo 
'<br><br><br>';

/* The class Dwarf is inherited from the class Dwarfs */

class Dwarf extends Dwarfs 
{
  public function 
message() 
  {
    echo 
"A real famous dwarf:<br><br>";
  }
}
$rd '1 - Tyrion Lanister';
$bi '2 - played by Peter Dinklage';
$fm '3 - in "The Game of Thrones".';
$aa '4 - Borned in June 11, 1969.';

$Dwarf = new Dwarf($rd$bi$fm$aa);

$Dwarf->message();
$Dwarf->intro();
     
?>

 RESULT   

Fatal error: Class Dwarf may not inherit from final class (Dwarfs) in H:\WEB\public_html\code\inheritance06.php on line 66

This ERROR is issued when using the final keyword to prevent class inheritance.

  7 EXERCISE   

<?php

final class Dwarfs {
  public 
$name;
  public 
$behavior;
  public 
$friend;
  public 
$age;
  
  public function 
__construct($name$behavior$friend$age
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
  }
}

class 
Dwarf extends Dwarfs {
  public 
$height;
  public 
$weight;
  
  public function 
__construct($name$behavior$friend$age$height$weight)
  {
    
$this->name $name;
    
$this->behavior $behavior;
    
$this->friend $friend;
    
$this->age $age;    
    
$this->height $height;
    
$this->weight $weight;
  }
  public function 
intro() 
  {
    echo 
$this->name '<br><br>';
    echo 
$this->behavior '<br><br>';
    echo 
$this->friend '<br><br>';
    echo 
$this->age '<br><br>';
    echo 
$this->height .  '<br><br>';    
    echo 
$this->weight .  '<br><br>';
  }
}

$rd '1 - Tyrion Lanister';
$bi '2 - played by Peter Dinklage';
$fm '3 - in "The Game of Thrones".';
$aa '4 - Borned in June 11, 1969.';
$ht '5 - He has the height 4.3307ft = 1.32m';
$wt '6 - and the weight 110.231lbs = 50kg.';

$Dwarf = new Dwarf($rd$bi$fm$aa$ht$wt);
$Dwarf->intro();

?>

 RESULT   

Fatal error: Class Dwarf may not inherit from final class (Dwarfs) in H:\WEB\public_html\code\inheritance06.php on line 47

This ERROR is issued when using the final keyword to prevent class inheritance.