Home »
Java programming language
Java Inheritance: An Overview, Use, Keyword Used, and Example
By Mayank Singh Last updated : January 14, 2024
Java Inheritance - An Overview
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.
Use of Java Inheritance
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.
Keyword Used in Java Inheritance
To inherit from a class, use the extends keyword.
Java Inheritance Syntax
Below is the syntax to implement Java inheritance:
class Base {
/*Body of the Base Class*/
}
class Derived extends Base {
/* Body of the Derived Class */
}
Example of Java Inheritance: Single Inheritance
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");
}
}
public class Main {
public static void main(String args[]) {
Headquarters H = new Headquarters();
H.getDetails();
H.showDetails();
Mainbranch M = new Mainbranch();
// Method Calling by Object M works correctly as the features
// of the HeadQuarters are inherited to Mainbranch
M.getDetails();
// Note That Inheritance provides reusability of code
// as observed in the above program
M.showDetails();
}
}
Note
This program also highlights the concepts of method overriding.
Output
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
What happens, if we use private data members in inheritance?
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");
}
}
Output with Error Message
Main.java:23: error: cityname has private access in Headquarters
cityname = KB.nextLine();
^
Main.java:25: error: totalemployees has private access in Headquarters
totalemployees = KB.nextInt();
^
Main.java:29: error: cityname has private access in Headquarters
System.out.println("Company Main Branch is Sitiuated in " + cityname + " and has " + totalemployees + " Employees");
^
Main.java:29: 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.