Introduction
In this lesson, we will explore the concept of inheritance in Java, a fundamental principle of Object-Oriented Programming (OOP). Inheritance allows one class (subclass) to inherit fields and methods from another class (superclass), promoting code reusability and establishing a hierarchical relationship between classes. Through practical code examples, you will learn how to implement inheritance, utilize method overriding, and understand the benefits it brings to your Java programs.
What is Inheritance?
Inheritance is a mechanism wherein a new class, called a subclass, derives properties and behavior (methods) from an existing class, known as the superclass. This relationship fosters code reuse and can simplify program maintenance. The subclass can retain or override the behaviors of the superclass to tailor its functionality.
Superclass and Subclass
- Superclass: This is the class whose properties and methods are inherited by another class. For example, if we have a class
Animal, it can be a superclass. - Subclass: This is the class that inherits from another class. If we create a class
Dogthat extendsAnimal, thenDogis the subclass ofAnimal.
Implementing Inheritance in Java
To implement inheritance in Java, we use the extends keyword. Below is an example demonstrating this:
/* Superclass */
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
/* Subclass */
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Calls the method from Dog class
/* Calling method from superclass */
Animal myAnimal = new Dog();
myAnimal.sound(); // Calls overridden method in Dog class
}
}
Explanation: In the above program, the Dog class inherits the sound method from the Animal superclass. However, Dog overrides the sound method to provide its own implementation. Thus, when we instantiate a Dog object and invoke the sound method, the output is specific to the subclass.
Output:
Dog barks
Dog barks
Utilizing Method Overriding in Subclasses
Method overriding is a powerful feature of inheritance that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. In the example above, the Dog class overrides the sound method of the Animal class. Notice how the method signature remains the same; this is crucial for method overriding.
Benefits of Inheritance in Code Reusability
- Code Reusability: Inheritance allows subclasses to use methods and attributes of the superclass, reducing redundancy.
- Maintenance: Changes made in the superclass will propagate to subclasses, enhancing ease of maintenance.
- Polymorphism: Inheritance facilitates polymorphism, where a subclass can be treated as an instance of its superclass. This is useful in designing frameworks.
- Hierarchy: It enables creating a hierarchy of classes based on shared characteristics, making the application more organized.
Summary / Key Takeaways
In this lesson on Exploring Inheritance, you have learned:
- Definition of Inheritance: Inheritance allows classes to derive properties and behavior from other classes, enhancing code reuse.
- Classes Relationship: Understanding the distinction between superclass and subclass is essential for navigating class hierarchies.
- Java Implementation: Using the
extendskeyword enables class inheritance in Java syntax effectively. - Method Overriding: Subclasses can override methods of superclasses to define specific behavior, promoting customization.
- Benefits of Inheritance: Key advantages include code reuse, maintenance ease, polymorphism, and structured class hierarchy.