Inheritance : Extending a Class

Inheritance : Extending a Class

Reusability is one of the most important aspects of OOP paradigm. Java supports the concept of reusability. In java classes can be re used in several ways. This is basically done by creating new classes, reusing the properties of existing ones.

The mechanisms of deriving a new class from an old one is called inheritance. The old class is known as the base class or super class or parent class and the new one is calls the sub class or derived class or child class.

The inheritance allows subclasses to inherit all the variables and methods of their parent classes. Inheritance may take different forms:

  1. Single Inheritance ( Only one super class )
  2. Multiple Inheritance ( Several Super Classes )
  3. Hierarchical Inheritance ( One super class, many sub classes )
  4. Multilevel inheritance ( Derived from a derived class )

Program Illustration of single inheritance

Class room

{

Int length;

Int breadth;

Room(int x, int y )

Room ( int x, int y )

{

Length = x ;

Beadth = y ;

}

Intarea()

{

Return( length * breadth);

}

}

Class bedroom extendsroom // inheriting room

{

Intheight ;

Bedroom(int x, int y , int z )

{

Super ( x, y ) / pass values to super class

Height = z ;

}

Intvolume()

{

Return( length * beadth * height ) ;

}

 

}

Class inhertest

{

Public static void main (string args[ ] )

{

Bedroom room 1 = new bedroom(114, 12, 10 );

Int area1= room1.area();// super class method

Int volume1 = room1. Volume();// baseclass method

System.out.println(“Area = “ +area1);

System.out.println(“Volume = “ + volume);

}

}

 

Output :

Area1 : 168

Volume1 = 1680

 

The program defines a class room and extends it to another class bedroom. Note that the class bedroom defines its own data members and methods. The subclass bedroom now includes three instance variables namely length , breadth and height and two methods area and volume.

The constructor in the derived class uses the super keyword to pass the values that are required by the base constructor .The statement

Bedroom room1 = new bedroom(14,12,10);

Calls first the bedroom constructor method which in turn calls the room constructor method by using the super keyword.

Finally the object room1 of the subclass bedroom class the method area defined in the super class as well as the method volume defined in the sub class itself.

 

Subclass Constructor

A subclass constructor is used to construct the instance variables of both the sub class and the super class. The subclass constructor used the keyword super to invoke the constructor method of the superclass. The keyword super is used subject to the following conditions:

  1. Super may only be used within a subclass constructor method.
  2. The call to superclass constructor must appear as the first statement with in the sub class constructor. 
  3. The parameter in the super class must match the order and type of the instance variable declared in the super class.

Multilevel Inheritance

A common requirement in object oriented programming is the use of a derived class as a super class. Java supports this concepts and uses it extensively in building its class library. This concept allows us to build a chain of classes .

The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. the chain ABC is known as inheritance path.

A derived class with multilevel base classes is declared as follows :

Class A

{

{

……………..

……………..

}

Class B extends A // first level

{

 ……………

…………….

}

Class c extends b // second level

{

….

….

}

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control