
Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation
Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called the derived class or child class.
What is the purpose of inheritance?
Suppose we are building a system for a school. We create a class called “Employee” that contains common information like name, ID, and salary. Now, we want to create a “Teacher” class. Instead of writing all the properties again, we let the “Teacher” class inherit from the “Employee” class. This way, Teacher automatically gets all the common features from Employee. We can then add teacher-specific features like subject or department. This is called code reusability. We do not repeat code unnecessarily, and we build a more organized structure where each class plays its specific role.
Basic syntax of inheritance
In Java, inheritance is declared using the keyword extends
. Here is the general format:
class Base {
// members of base class
}
class Derived extends Base {
// members of derived class
}
The extends
keyword means that the derived class will inherit all public and protected members from the base class.
Simple example of inheritance
class Employee {
String name;
int id;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}
}
class Teacher extends Employee {
String subject;
void displayTeacher() {
displayInfo();
System.out.println("Subject: " + subject);
}
}
public class Main {
public static void main(String[] args) {
Teacher t = new Teacher();
t.name = "Dr. Afzal";
t.id = 101;
t.subject = "Computer Science";
t.displayTeacher();
}
}
In this example, Teacher is inheriting from Employee. The Teacher class gets access to the name and id fields, and also to the displayInfo function. It also has its own field called subject and a function called displayTeacher.
How constructors behave in inheritance
Constructors are special methods that are automatically called when an object is created. In inheritance, when a derived class object is created, the base class constructor runs first.
class Employee {
Employee() {
System.out.println("Employee constructor called");
}
}
class Teacher extends Employee {
Teacher() {
System.out.println("Teacher constructor called");
}
}
public class Main {
public static void main(String[] args) {
Teacher t = new Teacher();
}
}
Output:
Employee constructor called
Teacher constructor called
Multiple Inheritance in Java
Java does not support multiple inheritance with classes. However, it can be achieved using interfaces.
Example using interfaces:
interface Person {
void setName(String n);
}
interface Worker {
void setHours(int h);
}
class Engineer implements Person, Worker {
String name;
int hoursWorked;
public void setName(String n) {
name = n;
}
public void setHours(int h) {
hoursWorked = h;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Hours Worked: " + hoursWorked);
}
}
public class Main {
public static void main(String[] args) {
Engineer e = new Engineer();
e.setName("Ali");
e.setHours(40);
e.display();
}
}
In this program, the class Engineer
implements both Person
and Worker
interfaces. It gains the ability to store a name and number of hours worked, then display both using its own method.
Access specifiers and inheritance
Java has four main access specifiers:
public
: accessible everywhereprotected
: accessible in the same package and subclassesprivate
: accessible only within the same class- (default): accessible within the same package
In public inheritance, public and protected members of the base class are accessible in the derived class.
Real-life analogy to summarize
Think about vehicles. You have a base class called Vehicle. It has functions like start() and stop(). Now you create a derived class called Car. It automatically gets start() and stop(), but it may also have its own function openSunroof(). Similarly, a Truck class may have its own function loadCargo(). This is how we build specialized classes from a general one.
Another example: Person and Student
class Person {
String name;
int age;
void showPerson() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
class Student extends Person {
int rollNumber;
void showStudent() {
showPerson();
System.out.println("Roll Number: " + rollNumber);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.name = "Sara";
s.age = 20;
s.rollNumber = 1234;
s.showStudent();
}
}
Inheritance allows you to create a new class that can reuse the features of an existing class. It helps in making your code more organized, more reusable, and easier to manage. You can use inheritance to model real-world relationships where one entity is a specialized version of another. A Teacher is an Employee. A Student is a Person. An Engineer is both a Person and a Worker. These examples help map real-world understanding into Java code.