What is Java Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors 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.
Why Use Inheritance
Inheritance is used to promote code reusability and establish a natural hierarchy between classes. Here are the main reasons to use inheritance:
- Code Reusability: By inheriting from an existing class, a new class can reuse methods and fields of the existing class without having to rewrite them.
- Method Overriding: Inheritance allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
- Hierarchical Classification: It establishes a parent-child relationship between classes, which can be used to model real-world hierarchies.
- Polymorphism: Through inheritance, objects can be treated as instances of their parent class rather than their actual class. This allows for dynamic method binding.
Keyword
extends :- The extends keyword is used to create a subclass (derived class) from a superclass (base class). The subclass inherits the fields and methods of the superclass.
Syntax:- class Subclass extends Superclass {
// additional fields and methods
}
Implements :- The implements keyword is used when a class wants to implement an interface. An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors.
Syntax:- Class ClassName implements InterfaceName {
// implementation of interface methods
}
Syntax and Basic Example of Inheritance in Java
In Java, the syntax for inheritance is straightforward. Here’s how you define and use inheritance:
- Defining a Superclass (Parent Class):
- A class that will be inherited from.
- Defining a Subclass (Child Class):
- A class that inherits from the superclass using the extends keyword.
Basic Syntax
// Superclass (Parent Class)
class Superclass {
// Fields
// Methods
}
// Subclass (Child Class)
class Subclass extends Superclass {
// Additional fields
// Additional methods
Basic Example
Superclass
// Superclass (Parent class) class Person { // Properties of Person String name; int age; // Constructor for Person Person(String name, int age) { this.name = name; this.age = age; } // Method to display information about the person void displayInfo() { System.out.println("Name: " + name); System.out.println("Age: " + age); } }
Subclass
// Subclass (Child class) class Student extends Person { // Additional property for Student String studentID; // Constructor for Student Student(String name, int age, String studentID) { // Call the constructor of the superclass (Person) super(name, age); this.studentID = studentID; } // Method to display student information void displayStudentInfo() { // Call the superclass method displayInfo(); System.out.println("Student ID: " + studentID); } }
Basic Example
Using the Classes:
// Main class public class Main { public static void main(String[] args) { // Create an instance of Student Student student = new Student("Shan", 24, "S12345"); // Display student information student.displayStudentInfo(); } }
Types of Inheritance
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance (Through Interfaces)
- Multiple Inheritance (Through Interfaces)
Single Inheritance
In Java, single inheritance is where a class (child or subclass) inherits from one and only one parent class (superclass). This allows the subclass to reuse code from the superclass and extend its functionality.