Object-Oriented Programming (OOP) – Inheritance
Introduction
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create new classes based on existing ones. It enables code reuse, promotes a hierarchical structure, and is a key feature in Java. In this guide, we’ll explore the concept of inheritance in Java and how it can be used to create new classes that inherit attributes and behaviors from existing ones.
Base and Derived Classes
Inheritance involves two types of classes: base classes (also known as superclasses or parent classes) and derived classes (also known as subclasses or child classes). The base class serves as the template or blueprint from which the derived class inherits.
// Base class
public class Shape {
protected int x;
protected int y;
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int deltaX, int deltaY) {
x += deltaX;
y += deltaY;
}
}
// Derived class
public class Circle extends Shape {
private double radius;
public Circle(int x, int y, double radius) {
super(x, y); // Call the base class constructor
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
}
In this example, ‘Shape’ is the base class with attributes ‘x’ and ‘y’ and a method ‘move.’ ‘Circle’ is a derived class that extends ‘Shape’ and adds its attribute ‘radius’ and a method ‘area.’ The ‘super(x, y)’ call in the ‘Circle’ constructor initializes the base class attributes.
Accessing Base Class Members
In a derived class, you can access the attributes and methods of the base class using the ‘super’ keyword. This allows you to leverage the functionality of the base class while extending or customizing it in the derived class.
public class Rectangle extends Shape {
private int width;
private int height;
public Rectangle(int x, int y, int width, int height) {
super(x, y); // Call the base class constructor
this.width = width;
this.height = height;
}
public int area() {
return width * height;
}
public void resize(int newWidth, int newHeight) {
super.move(-x, -y); // Move to the origin
width = newWidth;
height = newHeight;
super.move(x, y); // Move back to the original position
}
}
In the ‘Rectangle’ class, we access the ‘move’ method of the base class ‘Shape’ using ‘super.move(-x, -y)’ to move the rectangle to the origin before resizing it and then moving it back.
Method Overriding
Inheritance allows you to override methods in the derived class, providing a new implementation while retaining the same method signature as the base class. This is useful for customization and specialization.
public class Square extends Shape {
private int sideLength;
public Square(int x, int y, int sideLength) {
super(x, y); // Call the base class constructor
this.sideLength = sideLength;
}
@Override
public int area() {
return sideLength * sideLength;
}
}
In the ‘Square’ class, we override the ‘area’ method from the base class ‘Shape’ to provide a new implementation specific to squares.
Benefits of Inheritance
Inheritance offers several advantages in OOP, including:
- Code Reuse: Inheritance allows you to reuse the attributes and methods of the base class, reducing code duplication.
- Specialization: Derived classes can specialize the behavior of the base class, adapting it to specific requirements.
- Polymorphism: Inheritance is a foundation for polymorphism, where objects of different derived classes can be treated as objects of the base class, enhancing flexibility and modularity.
Conclusion
Inheritance is a powerful mechanism in Java that enables the creation of new classes based on existing ones, fostering code reuse and flexibility. It is a fundamental concept in object-oriented programming and plays a significant role in designing and structuring Java applications.