Inheritance in Java is one of the core concepts of Object-Oriented Programming (OOP). It allows one class to reuse the properties and behaviors of another class, making code more modular, maintainable, and reusable.
This detailed guide explains inheritance in Java from the ground up, using real-world examples, use cases, and practical Java code. Whether you are a beginner or brushing up your concepts, this article will help you understand inheritance clearly and confidently.
Inheritance in Java is a mechanism where one class (called the child or subclass) acquires the properties and methods of another class (called the parent or superclass).
In simple words:
Inheritance represents an IS-A relationship.
Inheritance provides several benefits that make Java programs efficient and scalable.
class ParentClass { // fields and methods } class ChildClass extends ParentClass { // additional fields and methods }
class Vehicle { String brand = "Toyota"; void start() { System.out.println("Vehicle is starting"); } }
class Car extends Vehicle { int speed = 120; void displayDetails() { System.out.println("Brand: " + brand); System.out.println("Speed: " + speed); } }
public class Main { public static void main(String[] args) { Car car = new Car(); car.start(); car.displayDetails(); } }
Java supports different types of inheritance. Some types are supported directly, while others are achieved using interfaces.
class Animal { void eat() { System.out.println("Animal eats food"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } }
class Animal { void eat() { System.out.println("Eating"); } } class Mammal extends Animal { void walk() { System.out.println("Walking"); } } class Human extends Mammal { void think() { System.out.println("Thinking"); } }
class Shape { void draw() { System.out.println("Drawing shape"); } } class Circle extends Shape {} class Rectangle extends Shape {}
interface Camera { void takePhoto(); } interface MusicPlayer { void playMusic(); } class Smartphone implements Camera, MusicPlayer { public void takePhoto() { System.out.println("Photo taken"); } public void playMusic() { System.out.println("Music playing"); } }
Method overriding allows a child class to provide a specific implementation of a method already defined in the parent class.
class Bank { double getInterestRate() { return 5.0; } } class SBI extends Bank { double getInterestRate() { return 6.5; } }
The super keyword refers to the parent class object.
class Employee { String name = "John"; } class Manager extends Employee { String name = "Alice"; void display() { System.out.println(super.name); System.out.println(name); } }
| Industry | Use Case |
|---|---|
| Banking | Account → SavingsAccount, CurrentAccount |
| E-commerce | User → Admin, Seller, Customer |
| Gaming | Character → Player, Enemy |
| Education | Person → Student, Teacher |
Inheritance in Java is a powerful OOP feature that promotes code reuse, scalability, and maintainability. By understanding its types, syntax, real-world use cases, and limitations, developers can write clean and efficient Java programs.
Inheritance in Java allows one class to acquire the properties and methods of another class, enabling code reuse and logical hierarchy.
Java avoids multiple inheritance with classes to prevent ambiguity and complexity caused by method name conflicts.
No, constructors are not inherited, but they can be called using the super keyword.
Inheritance is about acquiring properties, while polymorphism allows methods to behave differently based on object type.
Inheritance should be avoided when there is no clear IS-A relationship or when composition offers a cleaner solution.
Copyrights © 2024 letsupdateskills All rights reserved