codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

32 / 65 topics
32Java Inheritance33Java Polymorphism34Java Inner Classes35Java Abstraction36Java Interface37Java Enum
Tutorials/Java Programming/Java Inheritance
☕Java Programming

Java Inheritance

Updated 2026-05-12
30 min read

Java Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create new classes based on existing ones. This tutorial will cover the basics of inheritance, including subclasses, superclasses, the extends keyword, and the final keyword.

Introduction

Inheritance enables code reusability and promotes a hierarchical classification of classes. By inheriting properties and behaviors from a superclass, you can create more specific classes (subclasses) that extend or modify the functionality of the superclass. This not only simplifies code but also enhances maintainability by reducing redundancy.

Understanding inheritance is crucial for building scalable and organized Java applications. It forms the basis for polymorphism, which we will explore in the next topic.

Subclasses and Superclasses

In Java, a class that inherits from another class is called a subclass (or derived class), while the class being inherited from is called a superclass (or base class). The extends keyword is used to establish an inheritance relationship between classes.

Example 1: Basic Inheritance

Let's start with a simple example where we have a superclass named Animal and a subclass named Dog.

Animal.java
1public class Animal {
2 public void eat() {
3 System.out.println("This animal eats food.");
4 };
5}
Dog.java
1public class Dog extends Animal {
2 public void bark() {
3 System.out.println("The dog barks.");
4 }
5}

In this example, the Dog class inherits the eat() method from the Animal class. We can now create a Dog object and call both its own methods and inherited methods.

Terminal
$ javac Animal.java
$ javac Dog.java
$ java Dog
Output
This animal eats food.
The dog barks.

Example 2: Overriding Methods

Subclasses can override methods of the superclass to provide specific implementations. Let's modify the Dog class to override the eat() method.

Dog.java
1public class Dog extends Animal {
2 @Override
3 public void eat() {
4 System.out.println("The dog eats meat.");
5 }
6
7 public void bark() {
8 System.out.println("The dog barks.");
9 }
10}
Terminal
$ javac Dog.java
$ java Dog
Output
The dog eats meat.
The dog barks.

In this example, the Dog class overrides the eat() method to provide a more specific implementation.

The extends Keyword

The extends keyword is used to create a subclass that inherits from a superclass. It establishes an "is-a" relationship between classes.

Syntax

Java
1public class SubclassName extends SuperclassName {
2 // Class body
3}

Common Mistakes

  • Multiple Inheritance: Java does not support multiple inheritance (a class cannot extend more than one class). If you need to inherit from multiple sources, consider using interfaces.
  • Access Modifiers: Ensure that the methods and fields you want to override or access have appropriate visibility. For example, private methods cannot be overridden.

The final Keyword

The final keyword can be used with classes, methods, and variables to restrict modifications.

final Classes

A class declared as final cannot be subclassed. This is useful when you want to prevent any further extension of the class.

FinalClass.java
1public final class FinalClass {
2 public void display() {
3 System.out.println("This is a final class.");
4 }
5}

final Methods

A method declared as final cannot be overridden by subclasses. This is useful when you want to ensure that the behavior of a method remains consistent across different subclasses.

Animal.java
1public class Animal {
2 public final void eat() {
3 System.out.println("This animal eats food.");
4 }
5}

final Variables

A variable declared as final cannot be reassigned after its initial value is set. This is useful for defining constants.

Constants.java
1public class Constants {
2 public static final int MAX_VALUE = 100;
3}

Example: Using final

Let's combine the use of final with inheritance.

Animal.java
1public class Animal {
2 public final void eat() {
3 System.out.println("This animal eats food.");
4 }
5}
Dog.java
1// This will cause a compile-time error
2public class Dog extends Animal {
3 @Override
4 public void eat() {
5 System.out.println("The dog eats meat.");
6 }
7}
Terminal
$ javac Dog.java
Dog.java:4: cannot override final method from Animal
public void eat() {
^
1 error

In this example, attempting to override the eat() method in the Dog class results in a compile-time error because the eat() method in the Animal class is declared as final.

Practical Example

Let's create a practical example that demonstrates inheritance and polymorphism. We'll define a superclass Vehicle and two subclasses Car and Motorcycle. Each subclass will have its own specific methods.

Vehicle.java
1public class Vehicle {
2 public void start() {
3 System.out.println("The vehicle starts.");
4 }
5}
Car.java
1public class Car extends Vehicle {
2 @Override
3 public void start() {
4 System.out.println("The car starts with a key.");
5 }
6
7 public void openTrunk() {
8 System.out.println("The trunk is opened.");
9 }
10}
Motorcycle.java
1public class Motorcycle extends Vehicle {
2 @Override
3 public void start() {
4 System.out.println("The motorcycle starts with a kick.");
5 }
6
7 public void wheelie() {
8 System.out.println("Doing a wheelie!");
9 }
10}
Main.java
1public class Main {
2 public static void main(String[] args) {
3 Vehicle myCar = new Car();
4 Vehicle myMotorcycle = new Motorcycle();
5
6 myCar.start(); // Output: The car starts with a key.
7 myMotorcycle.start(); // Output: The motorcycle starts with a kick.
8
9 // Type casting to access specific methods
10 if (myCar instanceof Car) {
11 ((Car) myCar).openTrunk(); // Output: The trunk is opened.
12 }
13
14 if (myMotorcycle instanceof Motorcycle) {
15 ((Motorcycle) myMotorcycle).wheelie(); // Output: Doing a wheelie!
16 }
17 }
18}
Terminal
$ javac Vehicle.java
$ javac Car.java
$ javac Motorcycle.java
$ javac Main.java
$ java Main
Output
The car starts with a key.
The motorcycle starts with a kick.
The trunk is opened.
Doing a wheelie!

In this example, we have a Vehicle superclass and two subclasses Car and Motorcycle. Each subclass overrides the start() method to provide specific behavior. We also demonstrate how to use type casting to access methods that are unique to each subclass.

Summary

ConceptDescription
SubclassA class that inherits from another class.
SuperclassThe class being inherited from.
extendsKeyword used to create a subclass.
finalKeyword to prevent inheritance or method overriding.
  • Inheritance allows you to create a hierarchy of classes, promoting code reusability.
  • Subclasses inherit properties and behaviors from superclasses.
  • The extends keyword is essential for establishing an inheritance relationship.
  • The final keyword can be used to prevent further subclassing or method overriding.

What's Next?

In the next topic, we will explore Java Polymorphism, which allows objects of different classes to be treated as objects of a common superclass. This concept is crucial for building flexible and dynamic applications. Stay tuned!


PreviousJava Packages / APINext Java Polymorphism

Recommended Gear

Java Packages / APIJava Polymorphism