Inheritance in Java
A Simple Thought
Imagine a company that makes both bicycles and motorbikes.
Both have wheels, seats, and handlebars, but a motorbike also has an engine.
Instead of rewriting everything for both, the company can reuse the shared design and only add what’s different.
In programming, this idea is called inheritance.
It lets one class use the features of another, saving time and keeping your code clean.
In this lesson, you will learn how inheritance works and how it helps build relationships between classes.
The Project: Vehicle Inheritance
You will create two classes: one called Vehicle, and another called Car that inherits from it.
The Vehicle class will contain properties shared by all vehicles, while Car will have its own specific detail.
Your output will look like this:
1 2 3 4 5 6======================== VEHICLE DETAILS Brand: Toyota Wheels: 4 Type: Car ========================
The Car class will inherit the brand and wheels from the Vehicle class and add its own type.
The Concept: Inheritance
Inheritance allows one class (the child or subclass) to use the fields and methods of another class (the parent or superclass).
You can use the extends keyword to show this relationship.
Here’s a basic example:
1 2 3 4 5 6 7 8class Vehicle { String brand; int wheels; } class Car extends Vehicle { String type; }
Now, the Car class automatically has access to everything inside Vehicle.
You can set the values of both parent and child fields using one object.
Why This Matters
Inheritance helps you avoid repeating the same code in multiple places.
It also helps organize your programs into logical groups.
Real examples include:
- A Teacher and Student class that both inherit from Person.
- A Dog and Cat class that both inherit from Animal.
- A Truck, Car, and Motorcycle class that all inherit from Vehicle.
Using inheritance keeps your code short, readable, and easier to maintain.
Let’s Build the Vehicle Program
Here’s the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40class Vehicle { String brand; int wheels; Vehicle(String b, int w) { brand = b; wheels = w; } void showDetails() { System.out.println(" Brand: " + brand); System.out.println(" Wheels: " + wheels); } } class Car extends Vehicle { String type; Car(String b, int w, String t) { super(b, w); type = t; } void showCarDetails() { showDetails(); System.out.println(" Type: " + type); } } public class VehicleInfo { public static void main(String[] args) { System.out.println("========================"); System.out.println(" VEHICLE DETAILS"); Car car1 = new Car("Toyota", 4, "Car"); car1.showCarDetails(); System.out.println("========================"); } }
When you run this program, the Car class inherits the properties of Vehicle and adds its own field type.
The keyword super is used to call the parent class’s constructor and reuse its setup code.
How It Works
- The Vehicle class defines the shared features, brand and wheels.
- The Car class inherits from Vehicle using the extends keyword.
- The Car constructor uses super() to call the Vehicle constructor and set up shared details.
- The Car class adds its own type field and displays all details together.
This shows how inheritance helps combine shared and unique features easily.
Learn Together with AI
If you are using an AI Copilot, you can experiment with inheritance by describing simple relationships.
Try this prompt:
“Write a Java program where a class Car inherits from a class Vehicle and prints their details.”
Then explore variations:
- “Add another subclass named Motorcycle.”
- “Add a method in Vehicle that prints a message called ‘This is a vehicle.’”
- “Use constructors to initialize all fields automatically.”
These prompts will help you see how inheritance allows flexibility and reuse across classes.
Practice Challenge
Create a program with two classes: Person and Teacher.
The Person class should have fields for name and age.
The Teacher class should inherit from Person and add a new field subject.
Your output should look like this:
1 2 3 4 5 6======================== TEACHER DETAILS Name: Ahmed Ali Age: 35 Subject: Mathematics ========================
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40class Person { String name; int age; Person(String n, int a) { name = n; age = a; } void showPersonInfo() { System.out.println(" Name: " + name); System.out.println(" Age: " + age); } } class Teacher extends Person { String subject; Teacher(String n, int a, String s) { super(n, a); subject = s; } void showTeacherInfo() { showPersonInfo(); System.out.println(" Subject: " + subject); } } public class TeacherInfo { public static void main(String[] args) { System.out.println("========================"); System.out.println(" TEACHER DETAILS"); Teacher t1 = new Teacher("Ahmed Ali", 35, "Mathematics"); t1.showTeacherInfo(); System.out.println("========================"); } }
When you run this program, the Teacher class uses all the fields and methods from Person while adding its own.
You’ve now built a system that shares code and extends functionality efficiently.
Coming Up Next
You have learned how inheritance allows classes to reuse existing code and extend their behavior.
Next, you will explore another key concept called encapsulation, which keeps data safe by controlling how it can be accessed and changed.