Java Inheritance Example
Table of Contents
- Introduction to Inheritance in Java
- Key Concepts of Inheritance
- Example of Inheritance: Animal Kingdom
- Code Walkthrough with Output
- Conclusion
1. Introduction to Inheritance in Java
Inheritance stands as a fundamental pillar of Object-Oriented Programming (OOP) in Java. It enables a class, known as the subclass, to inherit fields and methods from another class, the superclass. This mechanism fosters code reuse and establishes hierarchical relationships among classes. In this article, we will delve into inheritance through a practical Java inheritance example involving animals such as birds, fish, and reptiles, utilizing a hands-on project.
Why is Inheritance Important?
- Code Reusability: By inheriting properties and methods from existing classes, you can eliminate redundant code.
- Organizational Structure: It allows developers to create a structured hierarchy of classes, making the codebase easier to maintain and scale.
- Polymorphism: Inheritance facilitates polymorphism, enabling different objects to respond to the same method call, which we will demonstrate in our code example.
Feature | Description |
---|---|
Superclass | The parent class that shares properties and methods with its subclasses. |
Subclass | The child class that extends and customizes the behavior of the superclass. |
Overriding | The subclass redefines a method from the superclass to provide specialized functionality. |
2. Key Concepts of Inheritance
In Java, a subclass inherits both fields (attributes) and methods (behaviors) from its superclass. Additionally, it can override methods to offer a more specialized version of the inherited behavior. This capability is essential for creating flexible and maintainable code.
Concept | Description |
---|---|
Superclass | The parent class from which the subclass inherits. |
Subclass | The child class that extends the superclass. |
Overriding | Redefining a superclass method in a subclass. |
For instance, in our project, we establish the following relationships:
- Animal (Superclass) is extended by the subclasses Bird, Fish, and Reptile.
- The
move()
method is overridden in each subclass to represent the specific movement of each animal.
3. Example of Inheritance: Animal Kingdom
This example leverages inheritance to model an animal hierarchy. The Animal
class serves as the superclass, while Bird
, Fish
, and Reptile
are subclasses that inherit properties and behaviors from Animal
, while also introducing their own specific behaviors.
Superclass: Animal.java
The Animal
class defines common properties such as name
, age
, and methods like move()
and eat()
. These elements are inherited by the subclasses, promoting code reuse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package animal; public class Animal { protected String name; protected int age; public Animal(String name, int age) { this.name = name; this.age = age; } public void move() { System.out.println(name + " is moving"); } public void eat() { System.out.println(name + " is eating"); } } |
Subclass: Bird.java
The Bird
class extends Animal
and introduces a new method fly()
. It also overrides the move()
method to reflect a bird’s specific movement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package animal.bird; import animal.Animal; public class Bird extends Animal { private String featherColor; public Bird(String name, int age, String featherColor) { super(name, age); this.featherColor = featherColor; } public void fly() { System.out.println(name + " is flying with " + featherColor + " feathers."); } @Override public void move() { System.out.println(name + " is flying"); } } |
Subclass: Fish.java
The Fish
class extends Animal
and adds a method swim()
. Similar to Bird
, it overrides the move()
method to represent fish-specific movement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package animal.fish; import animal.Animal; public class Fish extends Animal { public Fish(String name, int age) { super(name, age); } public void swim() { System.out.println(name + " is swimming."); } @Override public void move() { System.out.println(name + " is swimming"); } } |
Subclass: Reptile.java
The Reptile
class extends Animal
and adds a method crawl()
, overriding move()
to depict reptile-specific movement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package animal.reptile; import animal.Animal; public class Reptile extends Animal { public Reptile(String name, int age) { super(name, age); } public void crawl() { System.out.println(name + " is crawling."); } @Override public void move() { System.out.println(name + " is crawling"); } } |
4. Code Walkthrough with Output
Now, let’s create instances of the Bird
, Fish
, and Reptile
classes to observe how the code operates in practice. Each class has its unique behavior for the move()
method, showcasing polymorphism.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { Animal eagle = new Bird("Eagle", 5, "brown"); eagle.move(); // Output: Eagle is flying Animal eel = new Fish("Eel", 2); eel.move(); // Output: Eel is swimming Animal crocodile = new Reptile("Crocodile", 12); crocodile.move(); // Output: Crocodile is crawling } } |
Explanation of Output:
The move()
method is overridden in each subclass. Consequently, the correct version is invoked based on the object’s type, even though the method is called on an Animal
reference.
- Eagle: The overridden
move()
method inBird
outputs: “Eagle is flying”. - Eel: The overridden
move()
method inFish
outputs: “Eel is swimming”. - Crocodile: The overridden
move()
method inReptile
outputs: “Crocodile is crawling”.
Full Output:
1 2 3 |
Eagle is flying Eel is swimming Crocodile is crawling |
This output illustrates the core principle of polymorphism in Java. Each subclass of Animal
defines its own version of the move()
method, while the program treats all objects as instances of the superclass Animal
.
5. Conclusion
In this article, we explored how inheritance operates in Java through a practical Java inheritance example using an animal hierarchy. The Animal
class functions as the superclass, while specific animals like Bird
, Fish
, and Reptile
inherit and override methods to provide customized behaviors. This example underscores the benefits of inheritance, including code reuse, a simplified structure, and the ability to tailor methods in subclasses.
Additionally, the code output demonstrated how polymorphism allows different subclasses to implement their own versions of a method (in this case, move()
) while being treated uniformly as Animal
objects. For more detailed information on Java inheritance, you can visit the official Oracle Java documentation.
For further reading on related topics, check out our articles on Java Polymorphism and Object-Oriented Programming in Java.