The code is just a junkyard if it is not designed for reuse. Re-usability is very simple if you understand inheritance, a very powerful principle of object oriented programming. For this reason I call inheritance the Ace of the object-oriented programming world.

Here, I explain inheritance with a simple illustration. From the figure below can you list the properties that are common to all the balloons?

 

The below properties are common to every balloon:

  1. Color
  2. Size
  3. Price and so on

These properties that are common to all classes of a given type say balloon is stored in a class called the base class or super class or parent class. Inheritance is a powerful object oriented programming feature that allows a class to reuse the properties and methods of an already existing class while adding its own functionality. It’s the process of creating a new class as an extension of an existing class primarily to enhance the code re-usability. The class that extends is called the derived / sub class and the class getting extended is called the super / base class.

Suppose there is a super class called superBalloon. There are different kinds of balloons such as birthday balloons, balloons that represent a theme like sports or Disneyland and so on. For class inheritance java uses the keyword extends. Now let’s write a small java program for the base class “superBalloon”.

 

package balloonInherit;
 
public class superBalloon
{
	/**
	 * The superBalloon class defines methods and variables common to all balloons such as
	 * size, color and price
	 */
	//commonly used fields in all balloons
	public int size;
        public String color;
	public double price;
	public int number_of_balloons_purchased;
	public double total_cost;
 
	public void purchase(double price, int number_of_balloons_purchased)
	{
		total_cost = (price * number_of_balloons_purchased);
	}
}

A derived class is more specific and can have additional functionality of its own apart from that of the base class. Thus a birthday balloon is a specialized version of the balloon class having a new field called decoration cost specific to it’s type to better suit the needs.

We can also override /replace the behavior of the base class in the derived class. Here, in the below example the birthday balloon inherits the superBalloon class and defines a new field called decoration cost specific to it’s kind. We use the super keyword to reuse the already existing properties of the base class.

package balloonInherit;
 
public class subPartyBalloon extends superBalloon
{
	/**
	 * Since this is a partyBalloon, an additional new field called decoration cost is added
	 * which is specific to this kind of balloon
	 */
 
	public static void main(String args[])
	{
		//Create an object for subPartyBalloon class and invoke the purchase method
                subPartyBalloon bDayBalloon = new subPartyBalloon();
		bDayBalloon.purchase();
	}
 
	public void purchase()
	{
		//Additional property specific to the type partyBalloon
		double decoration_cost = 2.00;
 
		//Reuse the existing properties of the base class using super keyword
		super.size = 20;
		super.number_of_balloons_purchased = 3;
		super.color = "red";
 
		//These are the prices of the balloon depending on it's size
		switch (super.size)
		{
			case 10:
				super.price = 5.00;
				break;
			case 20:
				super.price = 10.00;
				break;
			case 30:
				super.price = 15.00;
				break;
			default:
				System.out.println("Invalid size");
		}
 
		//Invoke the the purchase methods of the super class to calculate the base_cost
		super.purchase(price,number_of_balloons_purchased);
 
		//Since it is a birthday party balloon the additional cost for decoration is added
		total_cost = total_cost + decoration_cost;
 
		System.out.format("The total cost of %d %s birthday balloons is %.2f", number_of_balloons_purchased,color,total_cost);
	}
}

The switch-case control structure decides the price of the balloon based on the input size. Once a matching size is found it assigns value to the price variable and breaks the execution of the switch-case block. The total cost of the birthday balloons is calculated and the output is as below.

OUTPUT:

The total cost of 3 red birthday balloons is 32.00

Now let’s create a java program for another derived class called subThemeBalloon. A theme based balloon has theme and design as additional fields specific to it’s kind. For example to represent the theme of Disneyland, a balloon is designed with the theme Disneyland and with the shape Mickey mouse.

The subThemeBalloon class reuses the existing properties of the base class superBalloon using the super keyword. It also has three additional new fields viz. theme, design and decoration cost specific to it’s kind to better suit the needs.

package balloonInherit;
 
public class subThemeBalloon extends superBalloon
{
	/**
	 * A theme based balloon has theme and design as additional fields. For example to represent
	 * the theme of Disneyland a balloon is designed with the theme Disneyland and with
	 * the shape mickey mouse
	 **/
 
	public static void main(String[] args)
	{
                //Create an object of subThemeBalloon class and invoke the purchase method
                subThemeBalloon themeBalloon = new subThemeBalloon();
		themeBalloon.purchase();
	}
 
	public void purchase()
	{
		//Declare three additional properties that are specific to the type themeBalloon
		String theme = "Disneyland";
		String design = "Mickey Mouse";
		int decoration_cost = 5;
 
		//Reuse the existing properties of the base class using super keyword
		super.number_of_balloons_purchased = 20;
		super.price = 15.00;
 
		//Invoke the purchase method of the base balloon class to calculate base cost using super keyword
		super.purchase(price, number_of_balloons_purchased);
 
		//Add the decoration cost for the balloon
		total_cost = total_cost + decoration_cost;
 
		//Print the total cost of the balloon
		System.out.format("The total cost of a %s %s balloon is %.2f", theme, design, total_cost);
	}
}

OUTPUT:

The total cost of a Disneyland Mickey Mouse balloon is 305.00

 

This is all about inheritance. Feel free to post any comments /questions.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks