Welcome back! Now that you have a solid understanding of classes and objects in Java, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).
Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.
In this lesson, you'll understand how to use inheritance in Java. We'll cover:
- What Inheritance Is
- How to Implement Inheritance in Java
- Why Inheritance Is Beneficial
You'll also learn about chaining inheritance and how to manage multiple inheritance using interfaces.
Inheritance is a way to establish a relationship between a new class (derived class) and an existing class (base class). The derived class inherits properties and behaviors (methods) from the base class. To better understand this concept, we'll use an example involving a Person
class as the base class and a Student
class as the derived class. This example will help demonstrate how properties and methods are inherited from the base class and how additional features can be added to the derived class.
Let’s start by defining a Person
class, which will act as the base class in our example:
Java1// Define the base class Person with name and age attributes 2class Person { 3 private String name; 4 private int age; 5 6 // Constructor to initialize name and age 7 public Person(String name, int age) { 8 this.name = name; 9 this.age = age; 10 } 11 12 // Display method to show name and age 13 public void display() { 14 System.out.println("Name: " + name + ", Age: " + age); 15 } 16}
In this snippet, the Person
class is defined with private attributes name
and age
. It also includes a constructor to initialize these attributes and a display
method to print the details.
Now, let’s create a Student
class that inherits from the Person
class:
Java1// Define the derived class Student, inheriting from Person 2class Student extends Person { 3 private String major; 4 5 // Constructor to initialize name, age of the base class, and the major of the student 6 public Student(String name, int age, String major) { 7 super(name, age); 8 this.major = major; 9 } 10 11 // Method to display major of the student 12 public void displayMajor() { 13 System.out.println("Major: " + major); 14 } 15}
In the Student
class, we use the extends
keyword to inherit from Person
. The Student
class reuses the name
and age
attributes from the Person
class, and it adds a new attribute, major
. The constructor uses the super
keyword to call the base class constructor and initialize the inherited properties. The Student
class also adds a displayMajor
method to show the student’s major.
Finally, let’s see how we can use these classes in a Main
method:
Java1public class Main { 2 public static void main(String[] args) { 3 // Create a Student object and display its details 4 Student student = new Student("Bob", 25, "Computer Science"); 5 student.display(); // Calls the display method from Person class 6 student.displayMajor(); // Calls the displayMajor method from Student class 7 } 8}
In the Main
class, we create an instance of Student
and use it to call methods from both the Person
class (inherited) and the Student
class (specific to Student
).
In this section, we’ll explore some important aspects and limitations of inheritance in Java that you should be aware of when designing your class hierarchies.
-
Chaining Inheritance: In Java, you can create a chain of inheritance where a class inherits from another class, which in turn can be inherited by yet another class. This allows you to build complex class hierarchies where each class extends the functionality of the previous one.
-
Single Inheritance with Classes: Java supports single inheritance, meaning a class can only inherit from one superclass. This is designed to keep the class hierarchy simple and avoid ambiguity that could arise from inheriting from multiple classes.
-
Multiple Inheritance via Interfaces: Although Java doesn’t support multiple inheritance with classes, it allows a class to implement multiple interfaces. This approach provides the flexibility of multiple inheritance, enabling a class to inherit behavior from multiple sources.
-
Final Keyword: The
final
keyword in Java can be used to prevent a class from being inherited. When a class is declared asfinal
, it cannot be extended by any other class, ensuring that its behavior remains unaltered in subclasses.
Understanding these limitations and specifications of inheritance in Java helps you design effective and maintainable object-oriented systems.
Inheritance is powerful for several reasons:
- Code Reusability: Instead of rewriting common functionalities, you can inherit them from a base class, making maintenance easier and reducing errors.
- Extension: You can extend existing code by adding new features to a derived class without changing the existing base class.
- Hierarchy: It helps in organizing code in a hierarchical manner, which reflects real-world relationships and improves code readability and structure.
Inheritance is a cornerstone of OOP, and understanding it will enable you to design more flexible and scalable applications. It's an essential concept for mastering OOP.
In the next unit, we'll also explore how the concept of inheritance enables (runtime) polymorphism, allowing objects of different classes to be treated as objects of a common base class.
Excited to start practicing? Let's move on and put this theory into action!