Understanding Inheritance in Java with Examples
Inheritance is one of the most important concepts in object-oriented programming. Many modern programming languages, including Java, support inheritance as the most important feature. It is a powerful tool that allows the formulation of a new class on an existing class. In this blog, we will explore the concept of inheritance in Java, its syntax, and its types. Inheritance helps developers build more efficient code and increases productivity. We will also explore inheritance in Java with example programs to get a better understanding of it.
What is Inheritance in Java?
Inheritance is a way in which new classes are created from existing classes. The existing class is called the base class or parent class, and the new class is called the derived class. The derived class takes away all the properties of the base class, such as variables and methods. It can also introduce new properties or modify the existing ones.
Types of Inheritance in Java
There are four types of inheritance that Java supports, which are as follows:
1. Single Inheritance
When we talk about single inheritance, a derived class inherits from a single base class. For example – Consider a class named “Employee” that contains properties such as name, age, and salary. A new class called “Manager” can be created that inherits from the Employee class and adds new properties such as department and bonus.
2. Hierarchical Inheritance
In hierarchical inheritance, many derived classes come from a single base class. For example – Consider a class named “Vehicle” with properties such as speed and color. A new class called “Bus” can be created that inherits from the Vehicle class. And another class named “Train” can be created that also inherits from the Vehicle class.
3. Multilevel Inheritance
When a derived class is derived from a base class, which is also derived from another base class, this is known as multilevel inheritance. For example – Consider a class named “Domestic animals” that contains properties such as breed and color. A new class named “Dog” can be created that inherits from the Animal class, and another class named “Labrador” can be created that inherits from the Dog class.
4. Multiple Inheritance (with interface):
Multiple inheritance allows a class to inherit properties from multiple base classes. Java programming language does not support multiple inheritances of classes, but it supports multiple inheritances of interfaces. An interface is a contract that defines a set of methods that a class must implement.
For example – Consider two interfaces named “Drawable” and “Movable” that contain methods such as draw() and move(). A new class named “Rectangle” can be created that implements both the Drawable and Movable interfaces.
In Java, inheritance is a fundamental concept that allows the creation of new classes based on existing ones. It is a great way to promote code reusability. With inheritance, you can create a new class that is a modified version of an existing class. You can do this by creating a subclass that inherits properties and methods from the superclass.
Uses of Inheritance in Java
Some of the benefits of using inheritance in java are:
1. Code Reuse
Inheritance allows the reusing of code. With the help of reusing, you can create new classes based on existing ones. It makes the process of maintaining and updating the codebase easier.
2. Polymorphism
A subclass can be treated as an instance of its superclass, meaning that inheritance enables Polymorphism. It helps write code that works with many classes sharing the same properties.
3. Encapsulation
Inheritance can help encapsulate code by separating implementation details into different classes. It helps organize and modularize code. As a result, it makes it more maintainable and easier to extend.
4. Extensibility
Inheritance in Java offers extensibility which means creating new classes that are an extension of existing classes. It implies that you can create new functionality without changing the existing code.
Overall, inheritance can help improve the maintainability and quality of code. It is a powerful feature that enables code reuse and modularity. You can opt for a course on Java to master your basics and learn concepts of inheritance in Java.
Inheritance Syntax in Java
The syntax of inheritance in Java is as follows:
class BaseClass {
// properties and methods of the base class
}
class DerivedClass extends BaseClass {
// properties and methods of the derived class
}
Advantages of Inheritance in Java
- Code Reusability & Extensibility: Inheritance in Java helps developers reuse the code of an existing class and build on top of it to create new classes.
- Modularity: It makes it easier to maintain the code because changes made to the base class automatically reflect in the derived classes.
Java Inheritance Example Programs
Most modern programming languages support Inheritance. The examples presented in this blog demonstrate how it can create different types of relationships between classes, such as single, multilevel, hierarchical, and multiple inheritances. Here are some example programs that demonstrate inheritance in Java:
1. Single Inheritance Example Program
class Employee {
String name;
int age;
double salary;
void display () {
System. out.println(“Name: ” + name);
System.out.println(“Age: ” + age);
System.out.println(“Salary: ” + salary);
}
}
class Manager extends Employee {
String department;
double bonus;
void display() {
super.display();
System.out.println(“Department: ” + department);
System.out.println(“Bonus: ” + bonus);
}
}
public class Main {
public static void main(String[] args) {
Manager manager = new Manager();
manager.name = “John Doe”;
manager.age = 35;
manager.salary = 50000.0;
manager.department = “Sales”;
manager.bonus = 10000.0;
manager.display();
}
}
In this program, a new class named Manager is created that inherits from the Employee class. The Manager class adds new properties such as department and bonus and overrides the display() method to display all the properties.
2. Multilevel Inheritance in Java Example Program
class Animal {
String breed;
String color;
void display() {
System.out.println(“Breed: ” + breed);
System.out.println(“Color: ” + color);
}
}
class Dog extends Animal {
String name;
void display() {
super.display();
System.out.println(“Name: ” + name);
}
}
class Bulldog extends Dog {
String size;
void display() {
super.display();
System.out.println(“Size: ” + size);
}
}
public class Main {
public static void main(String[] args) {
Bulldog bulldog = new Bulldog();
bulldog.breed = “Bulldog”;
bulldog.color = “Brown”;
bulldog.name = “Max”;
bulldog.size = “Medium”;
bulldog.display();
}
}
In this program, a new class named Bulldog is created that inherits from the Dog class, which, in turn, inherits from the Animal class. The Bulldog class adds a new property called size and overrides the display() method to display all the properties.
3. Hierarchical Inheritance in Java Example Program
class Vehicle {
int speed;
String color;
void display() {
System.out.println(“Speed: ” + speed);
System.out.println(“Color: ” + color);
}
}
class Car extends Vehicle {
int wheels;
void display() {
super.display();
System.out.println(“Wheels: ” + wheels);
}
}
class Bus extends Vehicle {
int passengers;
void display() {
super.display();
System.out.println(“Passengers: ” + passengers);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.speed = 100;
car.color = “Red”;
car.wheels = 4;
car.display();
Bus bus = new Bus();
bus.speed = 80;
bus.color = “Yellow”;
bus.passengers = 50;
bus.display();
}
}
In this program, two new classes named Car and Bus are created that both inherit from the Vehicle class. The Car class adds a new property called wheels, and the Bus class adds a new property called passengers. Both classes override the display() method to display all the properties.
4. Multiple Inheritance in Java Example Program
interface Drawable {
void draw();
}
interface Movable {
void move();
}
class Rectangle implements Drawable, Movable {
public void draw() {
System.out.println
(“Drawing rectangle…”); }
public void move() {
System.out.println(“Moving rectangle…”);
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.draw();
rectangle.move();
}
}
In this program, two interfaces named Drawable and Movable are created. The Rectangle class implements both interfaces and defines their respective methods. The Main class creates an instance of the Rectangle class and calls the draw() and move() methods, which were inherited from the Drawable and Movable interfaces, respectively.
Note: Remember that your code follows good design principles such as the SOLID principles when using inheritance. These principles promote modularity, flexibility, and extensibility. Additionally, it is essential to understand the implications of the access modifiers used in the base and derived classes and the super keyword to call methods from the base class.
Conclusion
Inheritance in Java allows programmers to create new things using existing ideas, promoting code reuse and organization in programs. It’s an essential concept to improve the software and bring creative changes. Therefore, it is crucial for all programmers to understand its basics, including the syntax, types, and advantages, to build efficient codes.