2. 2
Agenda
o Important points to remember while using inheritance
o Overriding Inherited Methods
o Copy Constructor
o Encapsulation
o Abstraction
o Abstraction Vs. Encapsulation
3. Important points to remember
while using inheritance
o Child class can only access and utilize non-private
parent-class properties and methods.
o Child class can also have its own methods that the
parent class cannot find or access.
o Child class can override and implement a method
specified in parent class.
4. Overriding
Inherited Methods
o Methods overriding, both parent and child classes should
have same method name with and number of arguments.
o Used to replace parent method in child class.
o The purpose of overriding is to change the behavior of parent
class method.
o The two methods with the same name and same parameter is
called overriding.
5. 5
Overriding Inherited Methods
Example
<?php
class P {
public function geeks() {
echo "Parent<br/>";
}
}
class C extends P {
public function geeks() {
echo "Child";
}
}
$p = new P();
$c = new C();
$p->geeks();
$c->geeks();
?>
OUTPUT
Parent
Child
6. 6
Copy Constructor is a type of constructor which
is used to create a copy of an already existing
object of a class.
Copy
Constructor
7. 7
Copy Constructor
Example
<?php
class CopyConstructor {
public $name;
public function __construct() {
}
public function copyCon(CopyConstructor $object){
$this->name = $object->name;
}
public function show(){
echo "Name = " . $this->name . "<br/>";
}
}
$obj1 = new CopyConstructor();
$obj1->name = 'Copy Constructor';
$obj1->show();
echo '<br/>';
$obj2 = new CopyConstructor();
$obj2->copyCon($obj1);
$obj2->show();
?>
8. 8
Encapsulation is a process of binding the
properties and methods together in a single unit
called class.
Encapsulation
o Declare each property private.
o Create public set method for each property to set the
values of properties.
o Create public get method for each property to get the
values of properties.
Data Encapsulation steps
9. 9
Encapsulation
Example
<?php
class person
{
private $Name, $Age;
public function setNameAge($name, $age) {
$this->Name = $name;
$this->Age = $age;
}
public function displayNameAge() {
echo "Name : ".$this->Name."<br/>";
echo "Age : ".$this->Age."<br/>";
}
};
$pObject = new person();
$pObject->setNameAge("Jhon Luther", 40);
$pObject->displayNameAge();
?>
OUTPUT
Name : Jhon Luther
Age : 40
10. 10
Abstraction is the concept of object-oriented
programming that “shows” only essential attributes
and “hides” unnecessary information.
The main purpose of abstraction is hiding the
unnecessary details from the users.
Abstraction
abstract class Class_Name {
//class code
}
abstract access_modifier function function_name();
abstract class abstract method
11. 11
Abstraction
Rules
o abstract keyword is used to declare an abstract class or method.
o An abstract class must contains at least one abstract method. However, it can also contains
non-abstract methods as well.
o An abstract method has no body. (It has no statements.) It declares an access modifier,
return type, and method signature followed by a semicolon.
o Objects cannot be created from abstract classes.
o If the abstract class uses type hinting (type declaration for return values), the child class
should use the same.
public function myMethod3() : int {...}
o The child class should override (redeclare) all the abstract methods.
o The arguments for methods should be the same as the abstract method.
o The child class can have arguments with default values where the abstract class hasn't
defined.
public function myMethod($name, $age, $country = 'USA') {...}
o The visibility of the child's method should be the same as the parent's or less restricted.
12. 12
Abstraction
Example
<?php
abstract class Person
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
abstract public function greet(): string;
}
?>
Explanation: In the parent class, the __construct method and $name property are declared. So, the
child class will automatically have them. But, greet() is a method that should be defined in all the child
classes and they should return a string.
13. 13
Abstraction
Example (Child Classes)
<?php
class Programmer extends Person {
public function greet(): string {
return "Hello World from ". $this->name;
}
}
class Student extends Person {
public function greet(): string {
return "Howdy! I'm ". $this->name;
}
}
class Teacher extends Person {
public function greet(): string {
return "Good morning dear students";
}
}
$programmer = new Programmer('John');
echo $programmer->greet();
$student = new Student('Doe');
echo $student->greet();
$teacher = new Teacher('Mary');
echo $teacher->greet();
?>
14. Abstraction Vs. Encapsulation
14
Parameter Abstraction Encapsulation
Use for
Abstraction solves the problem and issues that
arise at the design stage.
Encapsulation solves the problem and issue that
arise at the implementation stage.
Focus
Abstraction allows you to focus on what the
object does instead of how it does it
Encapsulation enables you to hide the code and data
into a single unit to secure the data from the outside
world.
Implementation
You can use abstraction using Interface and
Abstract Class.
You can implement encapsulation using Access
Modifiers (Public, Protected & Private.)
Focuses Focus mainly on what should be done. Focus primarily on how it should be done.
Application During design level. During the Implementation level.