Single Inheritance: PHP OOP

Inheritance is a way to reuse the code of existing objects.
In this video tutorial we’re illustrating single inheritance mechanism.

php-oop-single-inheritance

In this tutorial, we take a class called Vehicle with 3 attributes – color, accelerate and decelerate. And 3 methods – design, acceleration, deceleration.

Vehicle Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
< ?php
class Vehicle
{
public $color;
public $accelerate;
public $decelerate;
 
function design($assign)
{
$this->color = $assign;
}
function acceleration($positive)
{
$this->accelerate = $positive;
}
function deceleration($negetive)
{
$this->decelerate = $negetive;
}
}
?>

Using the methods – design, acceleration and deceleration we assign values to the public attributes color, accelerate and decelerate.

Inherit Vehicle Class To Class Car
inheritance.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< ?php
class Vehicle
{
public $color;
public $accelerate;
public $decelerate;
 
function design($assign)
{
$this->color = $assign;
}
function acceleration($positive)
{
$this->accelerate = $positive;
}
function deceleration($negetive)
{
$this->decelerate = $negetive;
}
}
 
class Car extends Vehicle
{
 
}
?>

Now create objects benze and bmw of class Car.

Objects of Class Car
vehicles.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
include_once("inheritance.php");
 
$benze = new Car();
$benze->design("Red");
echo $benze->color;
 
$bmw  = new Car();
$bmw->design("Silver");
echo "<br />".$bmw->color;
 
$benze->acceleration(20);
echo "<br />".$benze->accelerate;
 
$benze->deceleration(5);
echo "<br />".$benze->decelerate;
?>

Here, using the methods – design, acceleration and deceleration we assign attribute values to individual cars.

Single Inheritance: PHP OOP


[youtube https://www.youtube.com/watch?v=pdDqtHlkzEY]

YouTube Link: https://www.youtube.com/watch?v=pdDqtHlkzEY [Watch the Video In Full Screen.]



Uses of Inheritance:
Re-use of code.
Less and cleaner looking code.
Ease of understanding of code.
Ease of maintenance of project.

Access Specifiers: PHP OOP

Controlling Access with public, private and protected access modifiers.
These access specifiers can be used on both variables/attributes and methods.

In this video tutorial, we’ll demonstrate it using attributes.

Access Specifiers:
1. public
2. private
3. protected

Class With Attributes

1
2
3
4
5
6
7
8
9
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
?>

var $a is similar to writing public $a

We shall extend class container to another class called contains.
Extending class is an inheritance property, and we shall discuss inheritance in another article. For now, know that, with extends keyword, all the properties and methods with public / protected access are inherited.

Extends

1
2
3
4
5
6
7
8
9
10
11
12
13
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
?>

Here, contents of class container is extended by class contains.

Object of class container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
 
 $obj1 = new container();
         echo $obj1->a;
         echo $obj1->b;
?>

This outputs: 10 and 20 respectively.

Object of class contains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
 
 $obj2 = new contains();
         echo $obj2->a;
         echo $obj2->b;
?>

This outputs: 10 and 20 respectively.
i.e., public attributes are inherited

Accessing private / protected attributes outside class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
 
 $obj1 = new container();
         echo $obj1->c;
         echo $obj1->d;
?>

It through’s error, since you cannot access, private and protected variables/attributes outside the class.

Now lets check if private and protected variables are actually inherited:

Inheriting private attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 function __construct()
{
echo $this->c;
}
 }
 
 $obj1 = new contains();
?>

This through’s error, because $c is not inherited to class contains and thus not present inside class contains. Which means, private variables and methods cannot be inherited.

Inheriting protected attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 function __construct()
{
echo $this->d;
}
 }
 
 $obj1 = new contains();
?>

Output’s 40.
This means, protected variables and methods are inherited.

Access Specifiers: PHP OOP


[youtube https://www.youtube.com/watch?v=2Ow_cgBEGmk]

YouTube Link: https://www.youtube.com/watch?v=2Ow_cgBEGmk [Watch the Video In Full Screen.]



Conclusion
public attributes and methods are inherited and can be accessed outside the class.
private attributes and methods cannot be inherited and cannot be accessed outside the class.
protected attributes and methods can be inherited but cannot be accessed outside the class.

Creating Simple Window in Java: using swings

This Java video tutorial demonstrates the creation of a simple window by importing Swings package and extending JFrame class.

import javax.swing.*;

javax.swing provides a set of lightweight components that, to the maximum degree possible, works the same on all platforms.

myWindow Class

class myWindow extends JFrame
{
 
}

myWindow is user the name given to the defined class. extends is the keyword. JFrame is a built-in class of swing package.

Constructor

 myWindow()
 { 
setTitle("My window program, using Swings");
setSize(400, 200);
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 }

myWindow() is the constructor, and thus it’s name matches with that of its class name.
setTitle() : To set the string present on the title bar of the window we are creating.
setSize() : To set the size of the window. It takes two parameters, width and height of the window.
setVisible() : It’s very important for all the programs which involves visual components. passing a value of true makes the window visible.

Main method

public static void main(String args[])
{
 // new myWindow();
myWindow m1 = new myWindow();
}

main method just calls the constructor. We can simply write new myWindow() since we are not at all taking the help of any object here.

Video Tutorial: Creating Simple Window in Java: using swings


[youtube https://www.youtube.com/watch?v=7ThtNST8gKE]

YouTube Link: https://www.youtube.com/watch?v=7ThtNST8gKE [Watch the Video In Full Screen.]



Full Source code to create a simple window in Java: using swings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import javax.swing.*;
 
class myWindow extends JFrame
{
myWindow()
{
setTitle("My window program, using Swings");
setSize(400, 200);
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
 
public static void main(String args[])
{
 // new myWindow();
myWindow m1 = new myWindow();
}
}

Output:
window-swing-java