๐ Introduction
In real-world programming, we often encounter situations where multiple classes share similar properties and behaviors. Writing the same code again and again in different classes is not only inefficient but also makes the program difficult to maintain.
Java solves this problem using a powerful concept called Inheritance.
Inheritance is one of the four fundamental pillars of Object-Oriented Programming (OOP):
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Among these, inheritance plays a crucial role in code reuse and hierarchical design.
Think about the real world. A child inherits features from parents such as height, eye color, or genetic traits. Similarly, in Java programming, a class can inherit properties and methods from another class.
This mechanism allows programmers to reuse existing code, reduce redundancy, and build structured applications.
In this blog, which is the first article in the inheritance series, we will explore:
- What inheritance means
- Why inheritance is needed
- Basic syntax of inheritance
- Parent class and child class
- Real-world analogies
- Working code examples with explanation
- Common beginner mistakes
- Important rules to remember
By the end of this article, you will have a strong conceptual understanding of inheritance, which will prepare you for advanced topics like method overriding, types of inheritance, and the super keyword in the upcoming articles.
๐ง What is Inheritance in Java?
Inheritance is a mechanism in Java where one class acquires the properties and behaviors of another class.
In simple words:
Inheritance allows a class to reuse the fields and methods of another class.
The class whose features are inherited is called the Parent class (Superclass).
The class that inherits those features is called the Child class (Subclass).
๐ Important idea:
Child Class โ inherits โ Parent Class
This means the child class automatically gets access to the members of the parent class.
๐งฉ Real-World Analogy of Inheritance
Understanding inheritance becomes easier with a real-world example.
Consider this hierarchy:
Vehicle
|
โโโ Car
A Car is a type of Vehicle.
So the car automatically has properties like:
- speed
- engine
- fuel
Instead of rewriting these properties in the Car class, we can inherit them from the Vehicle class.
This is exactly how inheritance works in Java.
๐ฏ Why Do We Need Inheritance?
Without inheritance, code duplication becomes a big problem.
Imagine the following scenario.
We want to create classes for:
- Student
- Teacher
- Administrator
Each class may contain common properties such as:
- name
- age
- address
Without inheritance, we would repeat the same code multiple times.
Example without inheritance:
class Student {
String name;
int age;
}class Teacher {
String name;
int age;
}class Admin {
String name;
int age;
}
This approach creates duplicate code.
Now imagine we want to change the variable name or add a new property.
We would need to modify every class individually.
This makes the code hard to maintain.
Inheritance solves this problem by allowing us to create a common base class.
๐ Basic Syntax of Inheritance in Java
Inheritance in Java is implemented using the extends keyword.
General syntax:
class ParentClass {
// properties and methods
}class ChildClass extends ParentClass {
// additional properties and methods
}
Explanation:
ParentClassโ the class whose features are inheritedChildClassโ the class that inherits the featuresextendsโ keyword used to establish inheritance
๐ป Simple Example of Inheritance
Let us understand inheritance through a basic Java program.
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}public class Main {
public static void main(String[] args) { Dog d = new Dog(); d.eat();
d.bark(); }
}
๐ Line-by-Line Explanation
First class:
class Animal {
This defines the parent class.
void eat() {
System.out.println("This animal eats food");
}
This is a method inside the parent class.
Any class inheriting from Animal will get access to this method.
Second class:
class Dog extends Animal {
This means:
Dog inherits from Animal.
Dog automatically gains access to the eat() method.
void bark() {
System.out.println("Dog barks");
}
This is a method specific to the Dog class.
Main class:
Dog d = new Dog();
This creates an object of the Dog class.
d.eat();
Even though eat() is defined in the Animal class, the Dog object can use it because of inheritance.
d.bark();
Dog can also call its own method.
Program Output:
This animal eats food
Dog barks
๐ Important Terminology in Inheritance
When studying inheritance, you will frequently encounter these terms.
Superclass
The class whose features are inherited.
Example:
Animal
Subclass
The class that inherits from another class.
Example:
Dog
Parent Class
Another name for superclass.
Child Class
Another name for subclass.
โ๏ธ What Happens in Memory During Inheritance?
This is a concept that confuses many beginners.
Consider this statement:
Dog d = new Dog();
When the Dog object is created:
The object actually contains:
Animal part + Dog part
So the Dog object has access to:
- Parent class variables
- Parent class methods
- Child class variables
- Child class methods
However:
Parent class cannot access child class methods.
๐ซ Important Rule of Inheritance
Child class can access parent members, but the reverse is not true.
Example:
Dog โ can use โ eat()
Animal โ cannot use โ bark()
โ ๏ธ Common Beginner Mistakes
Understanding these mistakes will save you hours of debugging.
โ Mistake 1: Thinking constructors are inherited
Constructors are not inherited.
They are executed when objects are created but not copied to child classes.
โ Mistake 2: Private members are not inherited
If a variable or method is declared private, the child class cannot access it directly.
Example:
class Parent {
private int value = 10;
}
Child class cannot access value.
โ Mistake 3: Forgetting extends keyword
Inheritance works only when the extends keyword is used.
๐ฏ Advantages of Inheritance
Inheritance provides several benefits.
Code Reusability
The biggest advantage is code reuse.
Write once โ reuse everywhere.
Better Organization
Inheritance helps create logical class hierarchies.
Easy Maintenance
Changes in the parent class automatically apply to child classes.
Supports Polymorphism
Inheritance is required for method overriding and runtime polymorphism.
๐ Simple Hierarchy Example
Person
/ \
Student Teacher
Person class can contain:
- name
- age
- address
Student and Teacher classes can inherit these properties.
๐ก A More Practical Example
class Person { String name;
int age; void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}}class Student extends Person { int rollNumber; void showRoll() {
System.out.println("Roll Number: " + rollNumber);
}}public class Main { public static void main(String[] args) { Student s = new Student(); s.name = "Ajay";
s.age = 20;
s.rollNumber = 101; s.display();
s.showRoll(); }}
๐ Output
Name: Ajay
Age: 20
Roll Number: 101
Here the Student class inherits name, age, and display() from Person.
๐ง Key Points to Remember
โ Inheritance enables code reuse
โ Implemented using extends keyword
โ Child class inherits fields and methods
โ Parent cannot access child members
โ Private members are not directly inherited
โ Constructors are not inherited
๐ Conclusion
Inheritance is one of the most powerful features of Java’s object-oriented design. It allows developers to build programs that are organized, reusable, and easier to maintain.
By allowing one class to inherit the features of another, inheritance promotes code reuse and hierarchical design, which are essential for building large and scalable applications.
In this first article of the inheritance series, we explored:
- The concept of inheritance
- Parent and child classes
- Syntax using the
extendskeyword - Practical examples
- Real-world analogies
- Common beginner mistakes
Understanding these fundamentals will make it much easier to learn the more advanced topics that build upon inheritance.
In the next article, we will dive deeper into:
๐ Types of Inheritance in Java (Single, Multilevel, Hierarchical, and why Java does not support Multiple Inheritance with classes).
Mastering these concepts will move you one step closer to becoming confident in object-oriented programming in Java.