13.5 Inheritance and Polymorphism in Java: Class Hierarchy
Object-oriented programming (OOP) is a software development paradigm that is based on the concept of "objects", which can contain data in the form of fields, often known as attributes, and code, in the form of procedures, often known as methods. Two of the fundamental pillars of OOP are inheritance and polymorphism, which allow the creation of more reusable and flexible code.
Inheritance in Java
Inheritance is a mechanism that allows a new class to be created based on an existing class. The new class, known as a subclass or derived class, inherits fields and methods from the existing class, which is called a superclass or base class. Inheritance is a way of reusing code and establishing a type relationship between classes, creating a hierarchy.
In Java, inheritance is performed with the extends
keyword. For example:
class Animal {
void eat() {
System.out.println("This animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks");
}
}
Here, the Dog
class extends the Animal
class, which means that Dog
inherits the eat()
method. code> from Animal
. This means that a Dog
object can call the eat()
method, even if it has not been explicitly defined in the Dog
class.
Java only supports simple inheritance, which means that a class can only extend a single class. However, a class can implement multiple interfaces, which is a different topic and also a way to achieve polymorphy.
Polymorphism in Java
Polymorphism is the ability of a method to have multiple forms. In Java, this is mainly achieved in two ways: overloading polymorphism and overriding.
Overload polymorphism happens when two or more methods in the same class have the same name but different parameters. For example:
class Example {
void demo(int a) {
System.out.println("a: " + a);
}
void demo(int a, int b) {
System.out.println("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
Superscript polymorphism, on the other hand, is when a subclass has a method with the same signature (name and parameters) as a method in the superclass. When overriding a method, the subclass provides its own implementation. An example of this would be:
class Animal {
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks");
}
}
With overriding polymorphism, when a method is invoked, the subclass's method is called, not the superclass's. Using the @Override
annotation is optional, but it is a good programming practice, as it helps to avoid errors and makes the code more readable.
Class Hierarchy
Class hierarchy is the organization of classes in a tree structure where the base class is the root node and the subclasses are the child nodes. This structure allows for code reuse and complexity management in large systems. In a class hierarchy, a subclass inherits the state and behavior of the superclass, being able to add new fields and methods or override existing ones.
A classic example of class hierarchy is biological classification. For example, the Mammal
class can extend the Animal
class, and the Dog
class can extend the Mammal
class. Each level of the hierarchy adds specific characteristics that are inherited by the levels below.
Final Considerations
Inheritance and polymorphism are essential concepts in object-oriented development. They allow you to create a code structure that is both flexible and easy to maintain. Inheritance allows you to establish an "is-a" relationship between classes, while polymorphism allows objects from different classes to be treated in a uniform way. Together, they provide powerful tools for developing software in Java and many other object-oriented programming languages.
Understanding and correctly applying inheritance and polymorphism can be challenging for beginning programmers, but it is essential for creating robust and scalable systems. With practice and study of these concepts, developers can master the art of object-oriented programming and create efficient, high-quality Java applications.