
📌Inheritance in Java:
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.
📌What is Inheritance in Java?
👉Tutorial-1:-Java Programming.
👉Tutorial-2:-Java Interface.
👉Tutorial-3:-Abstract Class vs Interface in Java.
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).
📌Syntax of Inheritance in Java:
javaclass SuperClass {
// Methods and fields
}
class SubClass extends SuperClass {
// Additional methods and fields
}
📌Types of Inheritance in Java:-
Java supports different types of inheritance:
1. Single Inheritance:
- In single inheritance, a class inherits from only one superclass.
- Example:
Class B
inherits fromClass A
. - Code Example: javaCopyEdit
class 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(); } }
2. Multilevel Inheritance:
- In multilevel inheritance, a subclass inherits from another subclass.
- Example:
Class C
inherits fromClass B
, which inherits fromClass A
.
3. Hierarchical Inheritance:
- In hierarchical inheritance, multiple child classes inherit from a single parent class.
- Example:
Class B
,Class C
, andClass D
all inherit fromClass A
.
4. Hybrid Inheritance (Not Supported Directly in Java):
- 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.
📌Example of Inheritance in Java:
javaclass 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();
}
}
📌super Keyword in Java:
The super
keyword is used to access the parent class methods, variables, or constructors in the child class.
Example of super
Keyword Usage:
javaclass 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();
}
}
📌Why Use Inheritance in Java?
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.
📌Real-World Applications of Inheritance in Java:
- Banking Systems – Different account types like Savings and Current accounts inherit from a common Account class.
- E-Commerce Platforms –
User
class can have subclasses likeSeller
andBuyer
. - Automobile Systems –
Car
andBike
can inherit from a generalVehicle
class.
📌Conclusion:
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! 🚀