Java Inheritance

Now its time for us to get into Java Object Oriented Programming concepts (OOP). Inheritance is defined as defining the common features (eg. variables & methods) in a single class and reusing them by using the sub objects. Sub objects means the sub classes which inherits the common feature class.


Inheritance:

Parent Class – The class which has the common features.

Child or Sub Class – The class which inherits the Parent class. Which reuses the states and behaviors of the parent class.

extends Keyword:

For inheriting the features of the parent class, we should use the extends keyword in the child class as shown in the below example.

In Java, Multiple Inheritance is not supported. So a child class can inherit or extends only one parent class.

Whom does the parent class inherit?

All the java class which doesn’t explicitly extends a parent class will internally extends the java.lang.Object class.

Super Keyword:

Super keyword is used in the sub classes to call the constructor of its parent class. Sometimes ‘super’ keyword is used like ‘this’ reference keyword.

Below is the sample program which shows the simple usage of Inheritance. Assume that we are going to create Human Beings. In specific, we are going to create a male and a female using Java Inheritance. We will be having three Java classes as below.

  1. HumanBeing.java (Parent class)
  2. Female.java (Child Class)
  3. Male.java (Child class)

 

So in the human being parent class we have defined the common features required for both the male and female objects such as total count of eyes, nose, head, legs, hands etc.. and behaviors such as walk(), run() & breath().

 

Sample Program:

HumanBeing.java


public class HumanBeing {
	
	protected int eyeCount = 2;
	protected int noseCount = 1;
	protected int headCount = 1;
	protected int handCount = 2;
	protected int legCount = 2;
	protected String name;
	
	public static void main(String[] args) {
		
		Male johnObject = new Male();
		johnObject.name = "John";
		
		Female catherineObject = new Female();
		catherineObject.name = "Catherine";
		
		johnObject.maleVoice();
		System.out.println(johnObject.name + " has " + johnObject.headCount + 
				" Head, " + johnObject.noseCount + " Nose, " + johnObject.eyeCount +
				" Eyes, " + johnObject.handCount + " Hands, " + johnObject.legCount +
				" Legs.");
		
		catherineObject.femaleVoice();
		System.out.println(catherineObject.name + " has " + catherineObject.headCount + 
				" Head, " + catherineObject.noseCount + " Nose, " + catherineObject.eyeCount +
				" Eyes, " + catherineObject.handCount + " Hands, " + catherineObject.legCount +
				" Legs.");
	}
	
	protected void walk(){
		
	}

	protected void run(){
		
	}
	
	protected void breath(){
		
	}
}

Female.java


public class Female extends HumanBeing {
	
	public void femaleVoice(){
		System.out.println("Hi, I am " + this.name + "." + " I am a female.");
	}

}

Male.java


public class Male extends HumanBeing {
	
	public void maleVoice(){
		System.out.println("Hi, I am " + this.name + "." + " I am a male.");
	}	
}

Both the Male and the Female classes uses the extends keyword to inherit the features of the parent Class (HumanBeing.java). So the variables and methods in the parent class is easily accessible to the child class objects.

By running the HumanBeing.java class, we will get the below output in the console.

Output:

Hi, I am John. I am a male.
John has 1 Head, 1 Nose, 2 Eyes, 2 Hands, 2 Legs.
Hi, I am Catherine. I am a female.
Catherine has 1 Head, 1 Nose, 2 Eyes, 2 Hands, 2 Legs.

Posted on July 1, 2014 in Java for Beginners

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top