Search This Blog

Sunday, 2 October 2011

Implementing Inheritance in Java

0 comments

Inheritance is a powerful mechanism for code resuabiltiy. It promotes software reusability by allowing new classes being created from existing classes by absorbing the attributes and behavior of their super class using the keyword 'extends'. The class which absorbs the properties is called 'subclass' and the class which is used is used as the base in inheritance is called 'superclass'.

When we extend a class by creating a new class, the former and the latter will then have a parent-child relationship.  The original class is the parent class or the base class or the superclass.  The new class is called a child class or a subclass or a derived class of the parent.  The process of extending a class in object-oriented programming is called inheritance.

Java supports two types of inheritance SINGLE inheritance and MULTILEVEL inheritance.  When a class is derived from only one base class, that type of inheritance is termed as single inheritance.  Give below is a model of single inheritance: 

class A{
...
}
class B extends A {
...
}


public class C extends A {
...
}

In single inheritance, a class inherits the properties and methods of another class. In the above model of inheritance, class A is the base class and the classes B and C are sub classes that inherits the properties and methods of class A. Here is an example that illustrates single inheritance:

class Room {
void roomAttributes()
{
int roomlength;
int roomheight;
int roombreadth;
String roomname;
}
void roomBehavior()
{
System.out.println("Two windows");
System.out.println("One 6.2*3.5 door");
}
}

public class BedRoom {
public static void main(String[] args)
{
Room roomobj=new Room();
roomAttributes();
roomBehavior();
int area= room.length*room.breadth*room.height;
}
}

In the above example, Room is a super class from which Bedroom (subclass) is defined to inherit the properties and attributes of Room.  Given below is a scenario, where multi level inheritence is achieved:

class A extends B
class B extends C
class C extends D

Here, class A is the super class and class B is a sub class that inherits the properties and methods of class A.  Class C is derived from class B, which is a sub class of class A.  Similarly, class C extends its functionality from class C, which inherits its properties and methods from class B. Like a chain, one class is derived from another and the derived class becomes a parent class for the next derivation.  This is called multi-level inheritance.

Though Java supports inheritance in multiple levels, it does not support multiple inheritance. For example, deriving class A from two base classes namely B and C is not possible in Java.   

Method Overriding

Inheritance gives us the ability to add new functionality in a sub class that does not exist in the original class.  In a subclass we can add new methods and new fields.  Throug Inheritance, we can also change the behaviors of the existing class to better suit your needs.  This is possible by overridding the existing methods of the parent class to modify their behaviors in the sub class.

Method overriding is achieved when we rewrite a method of the base class in its subclass to have a behavior that performs more than that of its existing version in the base class.   A method is said to be overridden when it is rewritten in a sub class with the same name and the same function signature (parameter and return type) as that of a method in the parent class.

Leave a Reply