Java

Inheritance in Java

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.

What is Inheritance in Java?

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:

  • The child class inherits fields and methods from the parent class
  • The keyword used for inheritance in Java is extends

Inheritance represents an IS-A relationship.

Example of IS-A Relationship

  • A Car is a Vehicle
  • A Dog is an Animal
  • A SavingsAccount is a BankAccount

Why Use Inheritance in Java?

Inheritance provides several benefits that make Java programs efficient and scalable.

 Advantages of Inheritance

  • Code Reusability: Write once, use multiple times
  • Reduced Code Duplication: Shared logic stays in one place
  • Improved Maintainability: Changes in parent affect all children
  • Supports Method Overriding: Enables runtime polymorphism
  • Better Code Organization: Logical class hierarchy

Basic Syntax of Inheritance in Java

General Syntax

class ParentClass { // fields and methods } class ChildClass extends ParentClass { // additional fields and methods }

Simple Example of Inheritance in Java

Parent Class: Vehicle

class Vehicle { String brand = "Toyota"; void start() { System.out.println("Vehicle is starting"); } }

Child Class: Car

class Car extends Vehicle { int speed = 120; void displayDetails() { System.out.println("Brand: " + brand); System.out.println("Speed: " + speed); } }

Main Class

public class Main { public static void main(String[] args) { Car car = new Car(); car.start(); car.displayDetails(); } }

Explanation

  • The Car class inherits from Vehicle
  • The brand variable and start method come from the parent class
  • The Car class adds its own behavior without rewriting existing code

Types of Inheritance in Java

Java supports different types of inheritance. Some types are supported directly, while others are achieved using interfaces.

1. Single Inheritance

class Animal { void eat() { System.out.println("Animal eats food"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } }

2. Multilevel Inheritance

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"); } }

3. Hierarchical Inheritance

class Shape { void draw() { System.out.println("Drawing shape"); } } class Circle extends Shape {} class Rectangle extends Shape {}

4. Multiple Inheritance Using Interfaces

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 in Inheritance

Method overriding allows a child class to provide a specific implementation of a method already defined in the parent class.

Example of Method Overriding

class Bank { double getInterestRate() { return 5.0; } } class SBI extends Bank { double getInterestRate() { return 6.5; } }

The super Keyword in Inheritance

The super keyword refers to the parent class object.

Uses of super Keyword

  • Access parent class variables
  • Call parent class methods
  • Invoke parent class constructors

Example

class Employee { String name = "John"; } class Manager extends Employee { String name = "Alice"; void display() { System.out.println(super.name); System.out.println(name); } }

Use Cases of Inheritance in Java

Industry Use Case
Banking Account → SavingsAccount, CurrentAccount
E-commerce User → Admin, Seller, Customer
Gaming Character → Player, Enemy
Education Person → Student, Teacher

Limitations of Inheritance in Java

  • Tight coupling between parent and child classes
  • Changes in parent may affect child classes unexpectedly
  • Multiple inheritance is not supported with classes
  • Deep inheritance hierarchies can reduce readability

Using Inheritance

  • Use inheritance only when there is a clear IS-A relationship
  • Prefer composition over inheritance when possible
  • Keep parent classes simple and generic
  • Avoid deep inheritance chains

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.

Frequently Asked Questions (FAQs)

1. What is inheritance in Java in simple words?

Inheritance in Java allows one class to acquire the properties and methods of another class, enabling code reuse and logical hierarchy.

2. Why does Java not support multiple inheritance with classes?

Java avoids multiple inheritance with classes to prevent ambiguity and complexity caused by method name conflicts.

3. Can constructors be inherited in Java?

No, constructors are not inherited, but they can be called using the super keyword.

4. What is the difference between inheritance and polymorphism?

Inheritance is about acquiring properties, while polymorphism allows methods to behave differently based on object type.

5. When should inheritance be avoided?

Inheritance should be avoided when there is no clear IS-A relationship or when composition offers a cleaner solution.

line

Copyrights © 2024 letsupdateskills All rights reserved