JAVA INHERITANCE

As discussed earlier, Java is a family of OOP, which deals mostly with class and object. In Java, an Inheritance can be described as the process in which a class adopts the characters, properties, and facilities of another class. Inheritance discuss the re-usability of program codes and each subclass defines only the characters that are special to it. Other features can be inherited from the parent class.

There are four (4) classification of Inheritance in Java, which will be stated and explained below.

  1. Single Inheritance
  2. Multi-level Inheritance
  3. Hierarchical Inheritance
  4. Multiple / Hybrid Inheritance

SINGLE INHERITANCE

This is a type of inheritance in which a class stretches (extend) to another class. It is mostly referred to as the parent to child relationship. In this inheritance, a class adopt the benefit and characteristics of another class likewise the behavior of the class which enables a derived class to inherit the properties and behavior from a single parent class. This will, in turn, allows the reuse of codes and also add new features to the previous code.

Example

Public Class {

String name;

String lastname;

}

Class main class {

Void name () {

System.out.println (“Joseph”);

}

Void lastname () {

System.out.println (“Benjamin”);

}

Public Static void main (string [] args) {

main class obj = new main class () ;

obj.name ();

obj.lastname ();

}

 

Class child extends Parent {

String name;

}

Class child class {

Void name () {

System.out.println (“Yobo”);

}

Public Static void main (string [] args) {

child class obj = new child class () ;

obj.name ();

obj.lastname ();

}

}

}.

MULTI-LEVEL INHERITANCE

In this type of inheritance, class X is the root to class Y while class Y is the root to class Z. These simply means a class is gotten from another class which also extends another class is explained as MULTI-LEVEL INHERITANCE. In the program written below, class Y inherits the features and properties of class X while class Z inherit that of class Y. The Multi-level inheritance can also be explained as

Class X {

// ============

}

Class Y extends X {

// ============

}

Class Z extends Y {

// ¬¬¬¬¬============

}

Example.

Public Class {

String name;

String lastname;

}

Class main class {

Void name () {

System.out.println (“Olayiwola”);

}

Void lastname () {

System.out.println (“Anthony”);

}

Public Static void main (string [] args) {

main class A = new main class () ;

A.name ();

A.lastname ();

}

 

Class firstchild extends Parent {

String name;

}

Class firstchild class {

Void name () {

System.out.println (“Agboola”);

}

Public Static void main (string [] args) {

firstchild J = new firstchild () ;

J.name ();

J.lastname ();

}

Class secondchild extends Parent {

String name;

}

Class secondchild class {

Void name () {

System.out.println (“Ayobami”);

}

Public Static void main (string [] args) {

secondchild A = new secondchild () ;

A.name ();

A.lastname ();

}

}

}.

HIERARCHICAL INHERITANCE.

This is when a child has more than one child classes. The program below explains further and better.

Class W {

Void method W () {

System.out.println (“This is class W”);

}

Class X extends W {}

Class Y extends W {}

Class Z extends W {}

Public static void main (string [] args) {

X obj1 = new X ();

Y obj2 = new Y ();

Z obj3 = new Z ();

}

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here