Inheritance in Java Programming
OOPs in Java
In this lesson, we will understand what is Inheritance in Java and how to use it in class object programming.
What is Inheritance in Java?
In Java, when we create a new class by utilizing the code of an existing class, then this process is known as Inheritance.
In inheritance, we don't copy the code from an existing class to a new class. Instead, we include the existing class in a new class when we create the new class.
Let's take an example to clear the concept of inheritance. Suppose we have a class called Information with two instance variables: roll, name, and two instance methods as inputdata() and displaydata(). See the code given below.
Code for Information Class
class Information
{
private int roll;
private String name;
public void inputinfo()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Roll: ");
roll=sc.nextInt();
sc.nextLine(); // consume the new line charatcter \n left by the previous input
System.out.print("Enter Name: ");
name=sc.nextLine();
}
public void displayinfo()
{
System.out.println("Roll: " + roll);
System.out.println("Name: " + name);
}
}
We want to create a new class called Result that will store roll, name, marks of three subjects (English, Maths, Computer), total marks, and percentage obtained.
As the class Information already exists that can store roll and name, we will create our new class Result by inheriting all the codes from the Information class without copying and pasting the codes from the Information class into the Result class. See the complete example given below.
Example of Inheritance
import java.util.Scanner;
class Information
{
private int roll;
private String name;
public void inputinfo()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Roll: ");
roll=sc.nextInt();
sc.nextLine(); // consume the new line charatcter \n left by the previous input
System.out.print("Enter Name: ");
name=sc.nextLine();
}
public void displayinfo()
{
System.out.println("Roll: " + roll);
System.out.println("Name: " + name);
}
}
// Inherit the class Information into Result
class Result extends Information
{
private int eng;
private int math;
private int comp;
private int total;
private float per;
public void inputdata()
{
Scanner sc=new Scanner(System.in);
inputinfo(); // calling the inputinfo() method of the parent class Information
System.out.print("Enter English: ");
eng=sc.nextInt();
System.out.print("Enter Math: ");
math=sc.nextInt();;
System.out.print("Enter Computer: ");
comp=sc.nextInt();
total=eng+math+comp;
per=(total/300.0f)*100;
}
public void displaydata()
{
displayinfo(); // calling the displayinfo() method of the parent class Information
System.out.println("English: " + eng);
System.out.println("Math: " + math);
System.out.println("Computer: " + comp);
System.out.println("Total: " + total);
System.out.println("Percentage: " + per);
}
}
public class Example
{
public static void main(String args[])
{
Result x=new Result();
x.inputdata();
x.displaydata();
}
}
Output
Enter Roll: 1 Enter Name: Peter Enter English: 75 Enter Math: 84 Enter Computer: 92 Roll: 1 Name: Peter English: 75 Math: 84 Computer: 92 Total: 251 Percentage: 83.666664
In the above program, We can see that we have created a new class Result by inheriting the class Information using the extend keyword.
The class Result is created by inheriting the class Information. So we can call the Information class as Base class or Parent class and the Result class as Derived class or Subclass or Child class.
Use of super Keyword
In Java, the super keyword is used to refer to the immediate parent class of the class in which it is used. We can use it in the following ways:
To invoke a method of the parent class
When a method in a subclass has the same name as a method in its parent class, we use the super keyword to call the parent class's method. For example:
Example
class Alpha
{
public void display()
{
System.out.println("I am in Alpha Class");
}
}
// Inherit the class Alpha into Beta
class Beta extends Alpha
{
public void display()
{
super.display(); // calling the display() method of the parent class Alpha
System.out.println("I am in Beta Class");
}
}
public class Example
{
public static void main(String args[])
{
Beta x=new Beta();
x.display();
}
}
Output
I am in Alpha Class I am in Beta Class
To access a public instance variable of the parent class:
Suppose a subclass has an instance variable with the same name as a public instance variable in its parent class. In that case, we use the super keyword to refer to the parent class's public member variable. For example:
Example
class Alpha
{
public int x;
Alpha()
{
x=10;
}
}
// Inherit the class Alpha into Beta
class Beta extends Alpha
{
private int x;
Beta()
{
x=20;
}
public void display()
{
System.out.println("Value of Parent Class's x = " + super.x);
System.out.println("Value of Child Class's x = " + x);
}
}
public class Example
{
public static void main(String args[])
{
Beta x=new Beta();
x.display();
}
}
Output
Value of Parent Class's x = 10 Value of Child Class's x = 20
To invoke the parameterized constructor of the parent class:
We use the super keyword to invoke the parameterized constructor of the parent class from the parameterized constructor of the child class. For example:
Example
class Alpha
{
public int x;
Alpha() // Default constructor
{
x=10;
}
Alpha(int num) // Parameterized constructor
{
x=num;
}
}
// Inherit the class Alpha into Beta
class Beta extends Alpha
{
private int x;
Beta() // Default constructor
{
x=20;
}
Beta(int num1, int num2) // Parameterized constructor
{
super(num1); // Invoking the parameterized constructor of the parent class
x=num2;
}
public void display()
{
System.out.println("Value of Parent Class's x = " + super.x);
System.out.println("Value of Child Class's x = " + x);
}
}
public class Example
{
public static void main(String args[])
{
Beta x=new Beta(15,24);
x.display();
}
}
Output
Value of Parent Class's x = 15 Value of Child Class's x = 24
Note: We can use the super keyword only inside an instance method or inside the constructor of a child class. When using the super keyword inside the constructor of a child class, it must be the first statement of the constructor.