---Advertisement---

Inheritance in Java Don’t Miss This Great 2025.

By Shiva

Updated On:

---Advertisement---

Inheritance in Java is a powerful object-oriented concept that enables code reusability and efficient program design.

Java supports single inheritance through classes, allowing one class to inherit the properties of another.

Using inheritance, developers can create a hierarchical relationship between classes to build more modular applications.

The extends keyword in Java is used to inherit from a parent (super) class.

Inheritance promotes code reusability, reducing redundancy and improving maintainability in large Java projects.

Java’s method overriding feature allows a subclass to provide its specific implementation of a method defined in the superclass.

Inheritance is a fundamental concept in Object-Oriented Programming (OOPs) that allows one class to acquire the properties and behaviors of another class. This promotes code reusability and reduces redundancy.

In Java, inheritance is implemented using the extends keyword. The class being inherited is called the superclass (parent class), and the class that inherits is called the subclass (child class).

java

class SuperClass {
// Methods and fields
}

class SubClass extends SuperClass {
// Additional methods and fields
}

Java supports different types of inheritance:

  • In single inheritance, a class inherits from only one superclass.
  • Example: Class B inherits from Class A.
  • Code Example: javaCopyEditclass Animal { void sound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.sound(); d.bark(); } }
  • In multilevel inheritance, a subclass inherits from another subclass.
  • Example: Class C inherits from Class B, which inherits from Class A.
  • In hierarchical inheritance, multiple child classes inherit from a single parent class.
  • Example: Class B, Class C, and Class D all inherit from Class A.
  • Hybrid inheritance is a mix of single and multiple inheritance.
  • Java does not support multiple inheritance with classes to avoid ambiguity but supports it with interfaces.
java

class Doctor {
void showDetails() {
System.out.println("Doctor details...");
}
}

class Surgeon extends Doctor {
void showSpecialization() {
System.out.println("Surgeon specialization...");
}
}

public class Hospital {
public static void main(String[] args) {
Surgeon s = new Surgeon();
s.showDetails();
s.showSpecialization();
}
}

The super keyword is used to access the parent class methods, variables, or constructors in the child class.

Example of super Keyword Usage:

java

class Parent {
String name = "Parent Class";
}

class Child extends Parent {
String name = "Child Class";

void showNames() {
System.out.println(name); // Child class variable
System.out.println(super.name); // Parent class variable
}
}

public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.showNames();
}
}

Advantages of Inheritance:

Code Reusability – Reduces duplication by allowing child classes to reuse methods from parent classes.
Enhances Code Maintainability – Changes made in the superclass reflect in all subclasses.
Improves Code Readability – The hierarchical structure makes the code more understandable.
Encapsulation & Modularity – Keeps functionalities well-organized.

Disadvantages of Inheritance:

Increases Dependency – Any change in the superclass affects all subclasses.
Leads to Tight Coupling – Overuse can make the program complex.
Can Lead to Performance Issues – If too many levels of inheritance exist, method calls require more memory.

  1. Banking Systems – Different account types like Savings and Current accounts inherit from a common Account class.
  2. E-Commerce PlatformsUser class can have subclasses like Seller and Buyer.
  3. Automobile SystemsCar and Bike can inherit from a general Vehicle class.

Inheritance in Java is a powerful OOPs concept that improves code efficiency, readability, and maintainability. While it simplifies code reusability, it should be used wisely to prevent over-complexity and tight coupling.

Do you want more real-world examples or an interview-based guide on Inheritance? Let me know in the comments! 🚀

Leave a Comment

Index