Inheritance in Java
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviours of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class. Moreover, you can add new methods and
fields in your current class also.
Derived Class/Sub-class: Derived class is a class that inherits from a base class. It is
also known as subclass or child class.
Base Class/Superclass: The base class is the main class where derived classes inherit
the features. It is also known as the superclass or parent class.
Reusability: The name itself says reuse the repeated code in the programs. It is a
mechanism to reuse existing code when you are creating new classes.
Syntax
The keyword “extends” is used while inheriting a class. It defines that the
functionality of the superclass is being extended to the subclass.
The new class created is called a sub-class while the inherited class is termed a
parent
As displayed in the above figure, Programmer is the subclass and Employee is the
superclass. The relationship between the two classes is Programmer IS-A Employee.
It means that Programmer is a type of Employee.
class Employee{
float salary=40000;
int bonus=10000;
} }
Single inheritance in Java has several benefits and limitations. Here are some of
them:
Benefits:
● a class can only inherit from a single parent which results in less flexibility
for class designs.
● limit the ability to reuse code as some code might not fit in a hierarchy
caused due to single inheritance.
● This can cause ambiguity in code or throw errors, as some inherited
methods can have the same names but have different operations.
class Shape {
import java.util.Scanner;
public class SingleInherit {
int rollno;
String name;
String college;
Scanner sc = new Scanner(System.in);
void input() {
System.out.print("Enter Roll Number: ");
rollno=sc.nextInt();
System.out.println("Enter Student Name");
name = sc.next();
System.out.println("Enter College Name");
college = sc.next();
}
}
class Department extends SingleInherit
{
void display()
{
String deptname ;
String classname;
System.out.println("Enter Department name");
deptname=sc.next();
System.out.println("Enter Class Name");
classname=sc.next();
System.out.println("Student Roll No: " +rollno);
System.out.println("Student Name: "+ name);
System.out.println("College Name: " +college);
System.out.println("Department Name: "+deptname);
System.out.println("Class Name :"+ classname);
}
public static void main(String [] args)
{
Department d= new Department();
d.input();
d.display();
}
}
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.
Overall, the super keyword is a powerful tool for subclassing in Java, allowing
subclasses to inherit and build upon the functionality of their parent classes.
Use of super keyword in Java
It is majorly used in the following contexts as mentioned below:
● Use of super with variables
● Use of super with methods
● Use of super with constructors
In the above example, both the base class and subclass have a member maxSpeed. We
could access maxSpeed of base class in subclass using super keyword.
This is used when we want to call the parent class method. So whenever a parent and
child class have the same-named methods then to resolve ambiguity we use the super
keyword.
This code snippet helps to understand the said usage of the super keyword.
Example
// Java program to show use of super with methods
// superclass Person
class Person {
void message()
{
System.out.println("This is person class\n");
}
}
// Subclass Student
class Student extends Person {
void message()
{
System.out.println("This is student class");
}
// Note that display() is only in Student class
void display()
{
// will invoke or call current class message() method
message();
// will invoke or call parent class message() method
super.message();
}
}
// Driver Program
class Test {
public static void main(String args[])
{
Student s = new Student();
// calling display() of Student
s.display();
}
}
In the above example, we have seen that if we only call method message() then, the
current class message() is invoked but with the use of the super keyword, message() of
the superclass could also be invoked.
The super keyword can also be used to access the parent class constructor. One more
important thing is that ‘super’ can call both parametric as well as non-parametric
constructors depending on the situation.
Following is the code snippet to explain the above concept:
Example 1
// Java Code to show use of super keyword with constructor
// superclass Person
class Person {
Person()
{
System.out.println("Person class Constructor");
}
}
// subclass Student extending the Person class
class Student extends Person {
Student()
{
// invoke or call parent class constructor
super();
System.out.println("Student class Constructor");
}
}
// Driver Program
class Test {
public static void main(String[] args)
{
Student s = new Student();
} }
In the above example, we have called the superclass constructor using the keyword
‘super’ via subclass constructor.
Ex
// Java Program to Illustrate Invocation of Constructor Calling Without Usage of // super
Keyword
// Class 1 Super class
class Base {
// Constructor of super class
Base()
{
// Print statement
System.out.println("Base Class Constructor Called ");
}}
// Class 2 Sub class
class Derived extends Base {
// Class 3
// Main class
public class GFG {