VOOZH about

URL: https://dev.to/dhanraj_s_8fe1023a6e88992/inheritance-in-java-official-definition-simple-explanation-and-examples-38de

⇱ Inheritance in Java — Official Definition, Simple Explanation and Examples - DEV Community


Hey!

Before we start — let me ask you something.

A child looks like their parent. Same eyes. Same nose. Same smile. They did not build those features from scratch — they inherited them.

But the child also has their own personality. Their own skills. Things the parent does not have.

That is exactly how inheritance works in Java.

One class gets everything from another class — and can also add its own things on top.

Let us understand it fully — starting with the official definition, then breaking it down simply.


1. Official Definition

From the Oracle Java Documentation:

"A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or parent class). A subclass inherits all the members (fields, methods, and nested classes) from its superclass."

From GeeksforGeeks:

"Inheritance in Java is a mechanism in which one class acquires the properties (fields) and behaviors (methods) of another class. It is an important part of OOPs (Object-Oriented Programming System)."


2. What Does That Mean?

Forget the formal words for a second.

Imagine you are building a game. You have a Vehicle class with basic properties — speed, fuel, color. Now you want a Car class and a Bike class. Both are vehicles. Both have speed, fuel, and color.

Without inheritance — you write speed, fuel, and color twice. Once in Car. Once in Bike. Same code. Repeated.

With inheritance — Car and Bike simply say "I am a Vehicle." They get all of Vehicle's code automatically. Then they add their own unique things on top.

One class. Written once. Used everywhere it is needed.

That is inheritance.


3. Why Do We Need Inheritance?

Three clear reasons.

No repeated code

Write a feature once in the parent class. Every child class gets it automatically. No copy-pasting.

Easy to maintain

Need to fix something? Fix it in the parent. All child classes get the fix. You do not touch 5 different files.

Logical structure

Real-world things have natural parent-child relationships. Animal → Dog. Vehicle → Car. Employee → Manager. Inheritance lets you model that in code.


4. The Syntax

class Parent {
 // parent code
}

class Child extends Parent {
 // child code
}

extends is the keyword. The child class extends the parent class.

That one word — extends — is all Java needs to set up inheritance.


5. A Simple Example

class Animal {
 String name;
 int age;

 void eat() {
 System.out.println(name + " is eating.");
 }

 void sleep() {
 System.out.println(name + " is sleeping.");
 }
}

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

class Cat extends Animal {
 void meow() {
 System.out.println(name + " is meowing.");
 }
}
public class Main {
 public static void main(String[] args) {

 Dog d = new Dog();
 d.name = "Bruno";
 d.age = 3;
 d.eat(); // inherited from Animal
 d.sleep(); // inherited from Animal
 d.bark(); // Dog's own method

 Cat c = new Cat();
 c.name = "Mimi";
 c.age = 2;
 c.eat(); // inherited from Animal
 c.meow(); // Cat's own method
 }
}

Output:

Bruno is eating.
Bruno is sleeping.
Bruno is barking.
Mimi is eating.
Mimi is meowing.

Dog and Cat never defined eat() or sleep(). But they use them. Because they inherited them from Animal.

Quick question for you.

If you add a new method breathe() in the Animal class — which classes automatically get it?

Both Dog and Cat. Every class that extends Animal gets breathe() without you changing anything in those classes.

That is the power of inheritance.


6. Types of Inheritance in Java

Java supports four types of inheritance.

Single Inheritance

One child. One parent.

Animal
 ↓
Dog
class Animal { }
class Dog extends Animal { }

Multilevel Inheritance

Child becomes a parent for the next level.

Animal
 ↓
Dog
 ↓
Puppy
class Animal {
 void eat() {
 System.out.println("Eating...");
 }
}

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

class Puppy extends Dog {
 void weep() {
 System.out.println("Weeping...");
 }
}
Puppy p = new Puppy();
p.eat(); // from Animal
p.bark(); // from Dog
p.weep(); // Puppy's own

Output:

Eating...
Barking...
Weeping...

Puppy inherits from Dog. Dog inherits from Animal. So Puppy gets everything from both.


Hierarchical Inheritance

One parent. Many children.

 Animal
 ↙ ↘
Dog Cat
class Animal {
 void breathe() {
 System.out.println("Breathing...");
 }
}

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

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

Both Dog and Cat inherit from the same Animal. Common code lives in one place.


Multiple Inheritance — Not Supported Directly

Quick question for you.

Can a class extend two parent classes in Java?

class A { }
class B { }
class C extends A, B { } // Error in Java

No. Java does not allow this with classes. It causes something called the Diamond Problem — where Java gets confused about which parent's version to use when both have the same method.

Java solves this using Interfaces — which we will cover in a separate blog.


7. The super Keyword

super is used to access the parent class's members from inside the child class.

Calling parent method

class Animal {
 void sound() {
 System.out.println("Some sound...");
 }
}

class Dog extends Animal {
 void sound() {
 super.sound(); // calls Animal's sound()
 System.out.println("Dog barks!");
 }
}
Dog d = new Dog();
d.sound();

Output:

Some sound...
Dog barks!

super.sound() runs the parent version first. Then the child adds its own.

Calling parent constructor

class Animal {
 String name;

 Animal(String name) {
 this.name = name;
 System.out.println("Animal created: " + name);
 }
}

class Dog extends Animal {
 String breed;

 Dog(String name, String breed) {
 super(name); // calls Animal's constructor
 this.breed = breed;
 System.out.println("Dog created. Breed: " + breed);
 }
}
Dog d = new Dog("Bruno", "Labrador");

Output:

Animal created: Bruno
Dog created. Breed: Labrador

super(name) must be the first line in the child constructor. It passes the value up to the parent constructor.


8. Method Overriding

When the child class writes its own version of a method that already exists in the parent — that is method overriding.

class Animal {
 void sound() {
 System.out.println("Some animal sound");
 }
}

class Dog extends Animal {
 @Override
 void sound() {
 System.out.println("Dog barks");
 }
}

class Cat extends Animal {
 @Override
 void sound() {
 System.out.println("Cat meows");
 }
}
Animal a = new Animal();
Dog d = new Dog();
Cat c = new Cat();

a.sound(); // Some animal sound
d.sound(); // Dog barks
c.sound(); // Cat meows

@Override — tells Java "this method is replacing the parent's version." Java checks that the method actually exists in the parent. If you made a spelling mistake — Java catches it.

Child uses its own version. Parent version is hidden.

This is called Runtime Polymorphism — because which version to call is decided when the program runs, not at compile time.


9. What Is Inherited and What Is Not?

Quick question for you.

Does a child class inherit everything from the parent?

Not everything. Here is what gets inherited and what does not.

Inherited?
Public methods Yes
Protected methods Yes
Public fields Yes
Protected fields Yes
Private fields No
Private methods No
Constructors No
Static members Yes — but not overridden

Private members stay private. The child cannot access them directly. But if the parent has a public getter method — the child can use that to access them.

Constructors are not inherited. But you can call them using super().


10. A Real Example — Employee System

class Employee {
 String name;
 int id;
 double basicSalary;

 Employee(String name, int id, double basicSalary) {
 this.name = name;
 this.id = id;
 this.basicSalary = basicSalary;
 }

 void displayDetails() {
 System.out.println("Name: " + name);
 System.out.println("ID: " + id);
 System.out.println("Basic Salary: " + basicSalary);
 }
}

class Manager extends Employee {
 double bonus;

 Manager(String name, int id, double basicSalary, double bonus) {
 super(name, id, basicSalary);
 this.bonus = bonus;
 }

 @Override
 void displayDetails() {
 super.displayDetails();
 System.out.println("Bonus: " + bonus);
 System.out.println("Total Salary: " + (basicSalary + bonus));
 }
}

class Intern extends Employee {
 int durationMonths;

 Intern(String name, int id, double basicSalary, int durationMonths) {
 super(name, id, basicSalary);
 this.durationMonths = durationMonths;
 }

 @Override
 void displayDetails() {
 super.displayDetails();
 System.out.println("Internship Duration: " + durationMonths + " months");
 }
}
public class Main {
 public static void main(String[] args) {

 Manager m = new Manager("Ravi", 101, 75000, 20000);
 Intern i = new Intern("Anu", 201, 15000, 6);

 System.out.println("--- Manager ---");
 m.displayDetails();

 System.out.println("\n--- Intern ---");
 i.displayDetails();
 }
}

Output:

--- Manager ---
Name: Ravi
ID: 101
Basic Salary: 75000.0
Bonus: 20000.0
Total Salary: 95000.0

--- Intern ---
Name: Anu
ID: 201
Basic Salary: 15000.0
Internship Duration: 6 months

Employee has the common code — name, id, salary, displayDetails. Manager and Intern extend it. They add their own fields. They override displayDetails to show their extra info. They do not repeat name, id, or salary anywhere.


Quick Summary — 5 Things to Remember

  1. Inheritance — child class gets all public and protected members of the parent class automatically using extends.

  2. Types — Single, Multilevel, Hierarchical are supported. Multiple inheritance with classes is not allowed in Java.

  3. super — used to access parent class methods and constructors from the child class.

  4. Method Overriding — child writes its own version of a parent method. Use @Override to tell Java clearly.

  5. What is not inherited — private members and constructors. Everything else public or protected is inherited.


Inheritance is the backbone of Object Oriented Programming. Once you get it — classes start to feel organized and logical instead of random.

Try building the Employee system above. Add a new class called Developer that extends Employee. Give it a techStack field. Override displayDetails to show it.

If you have a question — drop it in the comments below.


References


Thanks for reading. Keep building.