Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class to inherit attributes and behaviors from another class. It is a mechanism that promotes code reuse, improves maintainability, and establishes hierarchical relationships between classes.
Base Class and Derived Class
Inheritance involves two types of classes: the base class (also known as the parent class or superclass) and the derived class (also known as the child class or subclass).
The base class is the class that provides the common attributes and behaviors. It serves as a blueprint for the derived class. The derived class inherits the attributes and behaviors of the base class and can also add its own unique attributes and behaviors.
Syntax of Inheritance
In Java, the keyword extends
is used to implement inheritance. The derived class follows the extends
keyword, followed by the name of the base class.
Here's an example of implementing inheritance in Java:
1// Base class
2public class Vehicle {
3 protected String brand;
4
5 public void honk() {
6 System.out.println("Tuut, tuut!");
7 }
8}
9
10// Derived class
11public class Car extends Vehicle {
12 private String model;
13
14 public Car(String brand, String model) {
15 this.brand = brand;
16 this.model = model;
17 }
18
19 public String getModel() {
20 return model;
21 }
22}
In this example, the Car
class extends the Vehicle
class, making Vehicle
the base class and Car
the derived class. The Car
class inherits the brand
attribute and the honk()
method from the Vehicle
class, and it also adds its own model
attribute and getModel()
method.
Access Modifiers in Inheritance
When inheriting attributes and methods from a base class, the derived class can access them depending on their access modifiers:
public
: The attribute or method can be accessed from anywhere.protected
: The attribute or method can be accessed within the same package and by subclasses.private
: The attribute or method cannot be accessed by subclasses.
Inheritance Hierarchies
Inheritance allows for the creation of inheritance hierarchies, where multiple classes can be derived from a single base class, and each derived class can have its own child classes.
Here's an example of an inheritance hierarchy in Java:
1// Base class
2public class Animal {
3 protected String name;
4
5 public Animal(String name) {
6 this.name = name;
7 }
8
9 public void makeSound() {
10 System.out.println("The animal makes a sound.");
11 }
12}
13
14// Derived class
15public class Dog extends Animal {
16 public Dog(String name) {
17 super(name);
18 }
19
20 public void makeSound() {
21 System.out.println("The dog barks.");
22 }
23}
24
25// Derived class
26public class Cat extends Animal {
27 public Cat(String name) {
28 super(name);
29 }
30
31 public void makeSound() {
32 System.out.println("The cat meows.");
33 }
34}
35
36class Main {
37 public static void main(String[] args) {
38 Animal animal = new Animal("Generic Animal");
39 Dog dog = new Dog("Max");
40 Cat cat = new Cat("Whiskers");
41
42 animal.makeSound();
43 dog.makeSound();
44 cat.makeSound();
45 }
46}
In this example, the Animal
class is the base class, and the Dog
and Cat
classes are derived classes. Each derived class overrides the makeSound()
method from the base class to provide its own implementation. The Main
class creates instances of each class and calls the makeSound()
method, resulting in different sounds being outputted.
Benefits and Use Cases
Inheritance provides several benefits in programming:
- Code Reuse: Inheritance allows for the reuse of code from base classes, reducing the need for duplicate code.
- Polymorphism: Inheritance enables the use of polymorphism, where objects of different derived classes can be treated as objects of the base class, providing flexibility in program design.
- Maintainability: Inheritance promotes modular code design and makes it easier to make changes or add new functionality.
Inheritance is commonly used in various programming scenarios, such as creating different types of objects with shared attributes and behaviors, implementing interfaces and abstract classes, and building class hierarchies to model real-world relationships.
Example: Shape Hierarchy
Let's consider an example of a shape hierarchy to demonstrate how inheritance can be used to model different types of shapes:
1// Base class
2public abstract class Shape {
3 protected String color;
4
5 public Shape(String color) {
6 this.color = color;
7 }
8
9 public abstract double getArea();
10
11 public void display() {
12 System.out.println("Shape: " + this.getClass().getSimpleName());
13 }
14}
15
16// Derived class
17public class Rectangle extends Shape {
18 protected double length;
19 protected double width;
20
21 public Rectangle(String color, double length, double width) {
22 super(color);
23 this.length = length;
24 this.width = width;
25 }
26
27 public double getArea() {
28 return length * width;
29 }
30}
31
32// Derived class
33public class Circle extends Shape {
34 protected double radius;
35
36 public Circle(String color, double radius) {
37 super(color);
38 this.radius = radius;
39 }
40
41 public double getArea() {
42 return Math.PI * radius * radius;
43 }
44}
45
46class Main {
47 public static void main(String[] args) {
48 Shape rectangle = new Rectangle("Blue", 5, 3);
49 Shape circle = new Circle("Red", 2.5);
50
51 rectangle.display();
52 System.out.println("Area: " + rectangle.getArea());
53
54 circle.display();
55 System.out.println("Area: " + circle.getArea());
56 }
57}
In this example, the Shape
class is the base class, and the Rectangle
and Circle
classes are derived classes representing specific shapes. The Shape
class is abstract and defines the color
attribute and the getArea()
method, which is implemented by the derived classes. The Rectangle
class calculates the area based on length
and width
, while the Circle
class calculates the area based on radius
. The Main
class creates instances of each class and calls the display()
and getArea()
methods to output the shape information.
Conclusion
Inheritance is a powerful feature in Java that allows for the creation of hierarchical class relationships, enabling code reuse, modularity, and flexibility in program design. Understanding inheritance and its implementation is essential for building robust and scalable Java applications.