Inheritance is a crucial feature of Java that allows a class to inherit properties and methods from another class. It establishes a relationship between a parent class (superclass) and a child class (subclass). Think of it as a parent passing on traits to their offspring. Let's delve deeper into this concept.
Basic Inheritance Structure
At its core, inheritance forms an "is-a" relationship between classes.
This means that a subclass is a specialized version of its superclass.
For example, you can have a superclass called "Vehicle" and a subclass called "Car." A car is a type of vehicle, and that's where inheritance comes into play.
Types of Inheritance
1. Single Inheritance
In single inheritance, a class can inherit from only one superclass. This is a straightforward form of inheritance, and it's used when a class has a clear, singular parent.
2. Multiple Inheritance
Multiple inheritance allows a class to inherit from more than one superclass. While it offers flexibility, Java doesn't support it directly to avoid ambiguity in method and property resolution.
3. Multilevel Inheritance
In multilevel inheritance, a class acts as a subclass for another class, which, in turn, is a subclass for a third class. This forms a chain of inheritance, making the relationship more complex.
4. Hierarchical Inheritance
In hierarchical inheritance, multiple classes inherit from a single superclass. Each subclass shares common properties and methods, but they can also have their unique features.
Example of Inheritance
Let's illustrate inheritance with a practical example. Imagine we have a superclass called "Animal" with properties like "name" and "species." Now, we want to create a subclass called "Cat" that inherits these properties from "Animal" and adds its specific properties, such as "furColor" and "sound."
In this example, "Cat" inherits the properties "name" and "species" from "Animal." This showcases how inheritance simplifies code by reusing existing classes.
Understanding the "extends" Keyword in Java
The "extends" keyword in Java is primarily used to create a subclass (or child class) that inherits properties and methods from a superclass (or parent class). It sets up what's known as an "is-a" relationship between the two classes. This means that the subclass is a specialized version of the superclass, and it can access and use the members (fields and methods) of the superclass.
Creating a Subclass
To use the "extends" keyword, you need to declare a class and specify the superclass from which you want to inherit.
Here's a basic syntax example:
In this example, "Subclass" is the name of the new class that you're creating, and "Superclass" is the name of the class you want to inherit from.
Inheriting Fields and Methods
When a subclass extends a superclass, it automatically inherits all the fields and methods defined in the superclass. This means that you can access these members directly within the subclass without having to redefine them. Here's an illustration:
Method Overriding
While a subclass inherits methods from its superclass, it also has the option to provide its implementation for those methods. This process is called "method overriding." It allows the subclass to customize the behavior of the inherited method while preserving the method signature from the superclass.
In this case, the "Dog" class overrides the "makeSound" method from the "Animal" class to provide a more specific behavior.
Why Use "extends"?
The "extends" keyword and inheritance in general offer several advantages:
Code Reusability: Inheritance allows you to reuse code from existing classes, reducing redundancy and making your codebase more efficient.
Method Overriding: Subclasses can override methods from their superclasses, enabling customization while preserving the core functionality.
Polymorphism: Inheritance is a key element of polymorphism, where objects can take on multiple forms and be used interchangeably.
Single Inheritance
Single inheritance is the simplest form of inheritance, where a subclass extends a single superclass.
In other words, a class inherits the properties and behaviours of only one class.
0 Comments
If you have any doubts, Please let me know