Вы находитесь на странице: 1из 13

Inheritance

Shubhangi Shinde

Inheritance
Inheritance is a fundamental
capability/construct in OOP where you can use
one class, as the base/basis for another class
or many other classes.
By implementing inheritance you can
inherit(or get) all properties and methods of
one class to another class.
The class who inherit feature of another class
known aschild class.
The class which is being inherited is know
asparent class.
Concept of the inheritance in oop is same as
inheritance in real world.

With the help of inheritance you can


increase re-usability of code.
You only have to type the code out
once.
The actual code being reused, can be
reused in many (unlimited) classes
but it is only typed out in one place.
'extends' is the keyword that enables
inheritance!

Reusing code with


inheritance
<?php
class person {
var $name; public $height;
protected $social_insurance;
private $pinn_number;
function __construct($persons_name)
{
$this->name = $persons_name;
}
private function get_pinn_number() {
return $this->$pinn_number;
}
}

<? php
class employee extends person
{
function __construct($employee_name)
{
$this->set_name($employee_name)
}
}
?>
We are able to use set_name() in 'employee',
even though we did not declare that method in
the 'employee' class. That's because we
already created set_name() in the class
'person.

Multilevel and Multipleinheritancein PHP


In php multilevelinheritanceis possible but
multipleinheritanceis not possible.
In simplified terms in php child class can not
inherit more than one parent class.
Buthierarchicalinheritance is possible in php.
Hierarchical means Parent inherit property of
grand parent class. Grand child inherit
property of parent class.
So in multilevel inheritance child can get
some property of from grandparentclass also.

Overriding methods
Meaning ofoverriding phenomenais replacing the
same parental behavior in child.
In oop meaning of overriding is to replace parent
class method in child class.
Sometimes (when using inheritance,) you may need
to change how a method works from the base class.
For example, let's say set_name() method in the
'employee' class, had to do something different than
what it does in the 'person' class.
You 'override' the 'person' classes version of
set_name(), by declaring the same method in
'employee'.

Method overriding example.

Abstract Classes
Abstract classes are those classes which can not be
directlyinitialized. Or in other word we can say that
you can not create object of abstract classes.
Abstract classes always created for inheritance
purpose.
You can only inherit abstract class in
yourchildclass.
In abstract classes you can have at least one
method abstract.
Abstract methods are the method which is only
defined but declared.
Usually abstract class are also known as base class.
It can only act as parent class of any normal class.

Abstract classes in PHP


You can create abstract classes in php
usingabstractkeyword. Once you will make any class
abstract in php you can not create object of that class.
abstract class abc
{
public function xyz()
{
return 1;
}
}
$a = new abc();//this will throw error in php

Abstract classes in php are only for inheriting in


other class.
abstract class testParent
{
public function abc()
{
//body of your funciton
}
}
class testChild extends testParent
{
public function xyz()
{
//body of your function
}
}
$a = new testChild();

Implementationof abstract method


Abstract functions are those functions of abstract class
which is only defined.
You can only create abstract method either in abstract
class or interface.
abstract class abc
{
abstract protected function f1($a , $b);
}
class xyz extends abc
{
protected function f1($name , $address)
{
echo "$name , $address";
}
}
$a = new xyz();

If you have an abstract method in


your abstract class then once you
inherit your abstract class then it is
necessary to declare your abstract
method. If you will not declare your
abstract method then PHP will throw
error in that case.

Вам также может понравиться