CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Inheritance in Java: A Developer’s Guide to Code Reusability

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Inheritance in Java: A Developer’s Guide to Code Reusability

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: February 27, 2021 · 5 min read · 0 Comments
Share: in X

Inheritance in Java is a cornerstone of object-oriented programming (OOP) that allows classes to inherit properties and behaviors from other classes. By creating hierarchical relationships, inheritance promotes code reuse, reduces redundancy, and simplifies maintenance. In this guide, you’ll learn how to implement inheritance, use method overriding, avoid common mistakes, and follow best practices for clean, scalable code.

Table of Contents

  • What is inheritance?
    • Key Terms:
  • Why inheritance is important?
  • Syntax of Inheritance in Java
  • Types of Inheritance in Java
  • Single-level inheritance
    • Example of single-level inheritance
  • Multi-level inheritance
    • Example of multi-level inheritance
  • Hierarchical inheritance
    • Example of hierarchical inheritance
  • Multiple inheritance
  • Best Practices for Inheritance in Java
  • Conclusion
  • FAQs
    • Can a class inherit from multiple classes in Java?
    • What is the difference between super() and this()?
    • Can constructors be inherited?

What is inheritance?

inheritance in java

Inheritance in Java enables a subclass (child class) to inherit fields and methods from a superclass (parent class). It follows an “is-a” relationship (e.g., a Car is a Vehicle).

Key Terms:

  • Superclass: Parent class whose features are inherited.
  • Subclass: Child class that inherits from the superclass.
  • Method Overriding: Subclass redefines a superclass method.

Why inheritance is important?

There are several reasons why inheritance is important in Java.

  • First, it allows us to reuse code. This can save us a lot of time and effort, as we don’t have to rewrite code that already exists.
  • Second, inheritance can help us to organize our code in a logical way. By grouping related classes together, we can make our code easier to understand and maintain.
  • Third, inheritance can help us to create more complex and powerful classes. By inheriting from existing classes, we can add new features and functionality to our own classes.

Syntax of Inheritance in Java

Use the extends keyword to create a subclass:

class Vehicle {                  // Superclass  
    void start() {  
        System.out.println("Vehicle starts");  
    }  
}  

class Car extends Vehicle {      // Subclass  
    // Inherits start() from Vehicle  
}  

public class Main {  
    public static void main(String[] args) {  
        Car myCar = new Car();  
        myCar.start(); // Output: Vehicle starts  
    }  
}  

Types of Inheritance in Java

There are four types of inheritance:

  • Single inheritance: This is the simplest type of inheritance. A subclass inherits from a single superclass.
  • Multi-level inheritance: This is when a subclass inherits from another subclass.
  • Hierarchical inheritance: This is when there is a hierarchy of classes, with each class inheriting from a single superclass.

Single-level inheritance

In single-level inheritance, a subclass inherits from a single superclass. This is the simplest type of inheritance, and it is the most common type of inheritance in Java.

Single Level inheritance

Example of single-level inheritance

The following code shows an example of single-level inheritance in Java:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

class Dog extends Animal {
  public void bark() {
    System.out.println("Dog is barking");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.eat();
    dog.bark();
  }
}

In this code, the Dog class inherits from the Animal class. This means that the Dog class inherits all of the methods and fields of the Animal class. In the main() method, we create a Dog object and call the eat() and bark() methods. The eat() method is inherited from the Animal class, and the bark() method is a method that is specific to the Dog class.

Multi-level inheritance

In multi-level inheritance, a subclass inherits from another subclass. This is a more complex type of inheritance than single-level inheritance, but it can be very useful for organizing our code.

Multi Level Inheritance

Example of multi-level inheritance

The following code shows an example of multi-level inheritance in Java:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

class Dog extends Animal {
  public void bark() {
    System.out.println("Dog is barking");
  }
}

class GermanShepherd extends Dog {
  public void herd() {
    System.out.println("German Shepherd is herding");
  }
}

public class Main {
  public static void main(String[] args) {
    GermanShepherd shepherd = new GermanShepherd();
    shepherd.eat();
    shepherd.bark();
    shepherd.herd();
  }
}

Hierarchical inheritance

In hierarchical inheritance, there is a hierarchy of classes, with each class inheriting from a single superclass. This is a more complex type of inheritance than single-level or multi-level inheritance, but it can be very useful for organizing our code.

Hierarchical Inheritance

Example of hierarchical inheritance

The following code shows an example of hierarchical inheritance in Java:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

class Dog extends Animal {
  public void bark() {
    System.out.println("Dog is barking");
  }
}

class Cat extends Animal {
  public void meow() {
    System.out.println("Cat is meowing");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.eat();
    dog.bark();

    Cat cat = new Cat();
    cat.eat();
    cat.meow();
  }
}

In this code, we have three classes: Animal, Dog, and Cat. The Animal class is the superclass, and the Dog and Cat classes are subclasses. The Dog class inherits from the Animal class, and the Cat class inherits from the Animal class. This means that both the Dog and Cat classes can eat, as they inherit the eat() method from the Animal class. However, the Dog class can also bark, and the Cat class can also meow, as these are methods that are specific to each subclass.

Multiple inheritance

In multiple inheritance, a subclass inherits from multiple superclasses. This is not supported in Java, as it can lead to ambiguity.

There is a separate post on why multiple inheritance is not supported in Java.

Best Practices for Inheritance in Java

  1. Use @Override Annotation: Avoid typos and ensure correct overriding.
  2. Keep Hierarchies Shallow: Prefer composition for complex relationships.
  3. Design for Extension: Make superclasses abstract if they’re meant to be inherited.
  4. Avoid Public Fields: Encapsulate data with private fields and getters/setters.

Conclusion

Inheritance in Java empowers you to build modular, reusable code by leveraging hierarchical relationships. By mastering method overriding, the super keyword, and best practices, you’ll create scalable applications that are easy to maintain.

FAQs

Can a class inherit from multiple classes in Java?

No. Java supports single inheritance for classes (use interfaces for multiple inheritance).

What is the difference between super() and this()?

super(): Calls superclass constructor.
this(): Calls another constructor in the same class.

Can constructors be inherited?

No. Subclasses must define their own constructors.

Related Posts:

  • Method Overloading vs Method Overriding:…
  • Control Statements in Java
  • Most Frequently Asked Spring Boot Interview…
  • Difference between OOP and POP
  • Polymorphism in Java
  • MySQL Commands for Developers
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleNested and Inner Class in Java
Next Article →Setting Up Your Environment for JDBC

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap