C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Inheritance in Java with Example

Submitted by Mayank Singh, on June 10, 2017

Learn: Inheritance in Java. This article will explain about Java's Inheritance Concept and its various types.

As we know,
Java is an Object-Oriented Programming Language (NOTE that it is not pure OOP language as it supports primitive data types such as int, float, double etc.) Inheritance is an important concept of Object-Oriented language as it offers us the mechanism which allows a class to inherit the features of another class, the class inheriting the features is called derived class or inherited class while the class from which the features are inherited is called Base Class or Super Class.

NOTE: All the Public and Protected Members of the Base Class become the Public and Protected for the Derived Class.

Inheritance’s main use is providing reusability of already written code. Which helps in reducing the number of lines of codes and confusion of the programmer.

Java Keyword: extends is used to inherit the features of one class to another class.

Inheritance Syntax:

class Base 
{	/*Body of the Base Class*/		}
class Derived extends Base
{	/* Body of the Derived Class */		}

Consider the program using Inheritance:

import java.util.Scanner;
class Headquarters
{
	int totalemployees; // Data Member 1
	String cityname; // Data Member 2
	Scanner KB=new Scanner(System.in);
	void getDetails()
	{
		System.out.println("Enter City Where Headquarters is Sitiuated :");
		cityname=KB.nextLine();
		System.out.println("Enter Total Number of Employees in Headquarters");
		totalemployees=KB.nextInt();
	}

	void showDetails()
	{
		System.out.println("Company Headquarters is Sitiuated in "+cityname+" and has "+totalemployees+" Employees");
	}
}

class Mainbranch extends Headquarters
{
	void getDetails()
	{
		System.out.println("Enter City Where Main Branch is Sitiuated");
		cityname=KB.nextLine();
		System.out.println("Enter The Total Number of Employees In Main Branch");
		totalemployees=KB.nextInt();
	}

	void showDetails()
	{
		System.out.println("Company Main Branch is Sitiuated in "+cityname+" and has "+totalemployees+" Employees");
	}
}

class Company
{
	public static void main(String args[])
	{
		Headquarters H=new Headquarters();
		H.getDetails();
		H.showDetails();	
		Mainbranch M=new Mainbranch();
		M.getDetails(); // Method Calling by Object M works correctly as the features of the HeadQuarters are inherited to Mainbranch
		M.showDetails();// Note That Inheritance provides reusability of code as observed in the above program
	}
}

/*This Program Also Highlights the Concepts of Method Overriding */

Output for Single Inheritance:

Enter City Where Headquarters is Sitiuated :
Delhi
Enter Total Number of Employees in Headquarters
1500
Company Headquarters is Sitiuated in Delhi and has 1500 Employees
Enter City Where Main Branch is Sitiuated
Indore
Enter The Total Number of Employees In Main Branch
500
Company Main Branch is Sitiuated in Indore and has 500 Employees

But, if we use the same example as given above, with private Data Members, we will get error message.

Headquarters Snippet:

class Headquarters
{
	private int totalemployees; // Data Member 1
	private String cityname; // Data Member 2
	Scanner KB=new Scanner(System.in);

	void getDetails()
	{
		System.out.println("Enter City Where Headquarters is Sitiuated:");
		cityname=KB.nextLine();
		System.out.println("Enter Total Number of Employees in Headquarters");
		totalemployees=KB.nextInt();
	}

	void showDetails()
	{
		System.out.println("Company Headquarters is Sitiuated in "+cityname+" and has "+totalemployees+" Employees");
	}
}

Error Message:

Company.java:26: error: cityname has private access in Headquarters
                cityname=KB.nextLine();
                ^
Company.java:28: error: totalemployees has private access in Headquarters
                totalemployees=KB.nextInt();
                ^
Company.java:32: error: cityname has private access in Headquarters
                System.out.println("Company Main Branch is Sitiuated in "+cityname+" and has "+totalemployees+" Employees");
                                                                          ^
Company.java:32: error: totalemployees has private access in Headquarters
                System.out.println("Company Main Branch is Sitiuated in "+cityname+" and has "+totalemployees+" Employees");
                                                                                               ^
4 errors */

These Error Messages clearly state that private data method cannot be overridden.

Inheritance in Java is divided into Several Types:

  1. Single Inheritance: In Single Inheritance, there is only one Derived Class/Inherited Class and a Super Class/Base Class.
  2. Multilevel Inheritance: In Multilevel Inheritance, there is a Base Class which has a Derived Class and further there is again a new derived class, derived from previous derived class, its analogy is same to parent to child to grandchild relationship, grandchild class cannot inherit the features of parent class directly.
  3. Hierarchical Inheritance: In Hierarchical Inheritance one Super Class acts as Base Class to more than one Derived Class.








COMMENTS