Java Inheritance
Definitions
Inheritance is a fundamental concept in object-oriented programming (OOP) languages like Java. It allows classes to inherit properties and behaviors from other classes, promoting code reusability and creating hierarchical relationships among classes.
This hierarchical relationship forms an "is-a" type of relationship, where a subclass "is a" specialized version of its superclass.
Syntax of Inheritance in Java
// Superclass
public class Superclass {
// Fields, constructors, and methods
}
// Subclass inheriting from Superclass
public class Subclass extends Superclass {
// Additional fields, constructors, and methods
}
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).
The class from which the subclass is derived is called superclass (also a base class or a parent class).
Advantages Of Inheritance in Java
- Code Reusability: Inheritance allows for code reuse and reduces the amount of code that needs to be written. The subclass can reuse the properties and methods of the superclass, reducing duplication of code.
- Abstraction: Inheritance allows for the creation of abstract classes that define a common interface for a group of related classes. This promotes abstraction and encapsulation, making the code easier to maintain and extend.
- Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used to model real-world objects and their relationships.
- Polymorphism: Inheritance allows for polymorphism, which is the ability of an object to take on multiple forms. Subclasses can override the methods of the superclass, which allows them to change their behavior in different ways.
Inheritance Types in Java
Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance.
Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes.
Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass.
Multiple Inheritance
In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes.
Please note that Java does not support multiple inheritances with classes.
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance.
Since Java doesn’t support multiple inheritances with classes, hybrid inheritance involving multiple inheritance is also not possible with classes.
we can achieve hybrid inheritance only through Interfaces if we want to involve multiple inheritance to implement Hybrid inheritance.
Inheritance in Java Example
import java.io.*;
import java.lang.*;
class Resource
{
String address,name;
void accept()
{
Console c=System.console();
System.out.print("\tName...");
name=c.readLine();
System.out.print("\tAddress...");
address=c.readLine();
}
void print()
{
System.out.println("Name is..>"+name);
System.out.println("Address..."+address);
}
}
class Student extends Resource
{
int sfee;
void saccept()
{
System.out.println("Please Enter Student Details");
accept();
Console c=System.console();
System.out.print("\tStudent Fee..");
sfee=Integer.parseInt(c.readLine());
}
void sprint()
{
print();
System.out.println("Fee is..."+sfee);
System.out.println();
}
}
class Emp extends Resource
{
int sal;
void eaccept()
{
System.out.println("Please Enter Employee Details");
accept();
Console c=System.console();
System.out.print("\tEmployee Salary..");
sal=Integer.parseInt(c.readLine());
}
void eprint()
{
print();
System.out.println("Employee salary.."+sal);
System.out.println();
}
}
public class InheritanceDemo2
{
public static void main(String args[])
{
Student st=new Student();
st.saccept();
st.sprint();
Emp e=new Emp();
e.eaccept();
e.eprint();
}
}