Tag Archives: Inheritance

Implements

We use implements keyword to extend interface of class, at the same time we can implement more than one interface and one interface can be implemented by another interface.

All methods declared in an interface must be public and the variables should be constant.

This is mandatory that we must declare the body part of the method in the derived class otherwise an error message will be generated.

Example

<?php
interface a{
public function test();
}

class b implements a{
public function test(){
echo “Function Test”;
}
}

$b=new b();
$b->test();
?>

Interface

PHP does not support multiple inheritance directly, to implement this we need Interface.

Here method are declared in the Interface body, and the body part of the method is implemented in derived class. Variables are declared as constant and it can not be changed in the child classes.

We use implement keyword to extend this kind of class, at the same time we can implement more than one interface and one interface can be implemented by another interface.

All methods declared in an interface must be public and the variables should be constant.

This is mandatory that we must declare the body part of the method in the derived class otherwise an error message will be generated.

PRIMARY PURPOSES OF AN INTERFACE

  • Interfaces allow you to define/create a common structure for your classes – to set a standard for objects.
  • Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources.
  • Interfaces provide a flexible base/root structure that you don’t get with classes.
  • Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let them worry about the details.

WHEN SHOULD YOU MAKE A CLASS AND WHEN SHOULD YOU MAKE AN INTEFACE?

  • If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface.
  • When you know what methods a class should have but you are not sure what the details will be.
  • When you want to quickly map out the basic structures of your classes to serve as a template for others to follow – keeps the code-base predictable and consistent.

A. A class can not extend more than one interface.

B. A class can implement more than one interface.

C. An interface can extend more than one interface.

D. An interface can not implement more than one interface.

EXAMPLE

<?php
interface a{
public function test();
}

class b implements a{
public function test(){
echo “Function Test”;
}
}

$b=new b();
$b->test();
?>

Hierarchical Inheritance

In hierarchical type of inheritance, one class is extended by many subclasses. It is one-to-many relationship

Example

<?php
class a
{
public function function_a(){
echo “class A”;
}
}

class b extends a
{
public function function_b(){
echo “class B”;
}
}

class c extends a
{
public function function_c(){
echo “class c”;
}
}

echo c::function_c();
echo c::function_a();

?>

Multilevel Inheritance

In multilevel, one-to-one ladder increases. Multiple classes are involved in inheritance, but one class extends only one. The lowermost subclass can make use of all its super classes’ members. Multilevel inheritance is an indirect way of implementing multiple inheritance. Following program explains.

Example

<?php
class a
{
public function function_a(){
echo “class A”;
}
}

class b extends a
{
public function function_b(){
echo “class B”;
}
}

class c extends b
{
public function function_c(){
echo “class c”;
}
}

echo c::function_c();
echo c::function_b();
echo c::function_a();

?>

Single Inheritance

In single inheritance, one class extends one class only

Example

<?php
class a
{
public function function_a(){
echo “class A”;
}
}

class b extends a
{
public function function_b(){
echo “class B”;
}
}

echo b::function_b();
echo b::function_a();

?>

Inheritance

Inheritance is the mechanism of deriving a new class from an existing class. It allows child class to access  the data member and functions of parent class.

To implement Inheritance  you should use the keyword ‘extends’ in the class definition .

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

 

Example

<?php
class a
{
public  function function_a(){
echo “class A”;
}
}

class b extends a
{
public function function_b(){
echo “class B”;
}
}

echo b::function_b();
echo b::function_a();

?>

PHP Support the below given inheritance

Single level
hierarchical level
Multilevel
Multiple
Hybrid

It does support multiple/Hybrid inheritance but multiple can be implemented through use of interface not directly

Access Specifiers

It specify the level of access to the the data member or function in a class

Please note that all data members and member functions are public by default.

Private

A private access specifier is used to hide data and member functions. A method or data member declared as private can only be accessed by the class itself and neither the outside program nor the derived class can have access to it.

Public

A public access specifier allows the data members and methods to be access from anywhere in the script. Lets look at an example below:

protected

A protected access specifier allows the derived class to access the data member or member functions of the base class, whereas disallows global access to other objects and functions.
Example

<?php
class PHPCode{
private $a;
public $b;
public function __construct($a, $b) {
$this->b = $a;
$this->b = $b;
}
}

$obj = new PHPCode(7,17);
echo $obj->b;
?>
Here we can not access the variable a but b

The reason why data members are declared private is to avoid the outside programs to either accidentally modify the values without necessary validation.

When you define a method as private that method can only be called from within that class and not from outside that is the global level script.

Private data member or functions can not be accessed by the child class objects