S06L14 – Inheritance 07 – Inheritance example

Java Inheritance Example

Table of Contents

  1. Introduction to Inheritance in Java
  2. Key Concepts of Inheritance
  3. Example of Inheritance: Animal Kingdom
  4. Code Walkthrough with Output
  5. 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.

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.

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.

Subclass: Reptile.java

The Reptile class extends Animal and adds a method crawl(), overriding move() to depict reptile-specific movement.

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.

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 in Bird outputs: “Eagle is flying”.
  • Eel: The overridden move() method in Fish outputs: “Eel is swimming”.
  • Crocodile: The overridden move() method in Reptile outputs: “Crocodile is crawling”.

Full Output:

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.