What is an example of polymorphism in Java?

Isabella Rivera | 2023-06-09 06:34:27 | page views:1828
I'll answer
Earn 20 gold coins for an accepted answer.20 Earn 20 gold coins for an accepted answer.
40more

Ethan Turner

Works at Tesla, Lives in Austin.
As a domain expert in software development, I have extensive experience with object-oriented programming (OOP) concepts, including polymorphism. Polymorphism is a fundamental principle in OOP that allows objects to be treated as instances of their parent class rather than their actual class. This capability is what enables a single interface to be used for a general class of actions. In Java, polymorphism can be achieved in several ways, but the most common are method overloading and method overriding.

Method Overloading is a compile-time polymorphism where multiple methods with the same name but different parameters are defined within a class. This allows the same method name to be used for different types of data and different numbers of parameters.

Method Overriding is a runtime polymorphism where a subclass provides a specific implementation of a method that is already provided by its parent class. This is often used to change or extend the behavior of a method in the subclass.

Here's an example to illustrate polymorphism in Java using method overriding:

```java
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat meows");
}
}

public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();

myAnimal.makeSound(); // Outputs: Animal makes a sound
myDog.makeSound(); // Outputs: Dog barks
myCat.makeSound(); // Outputs: Cat meows

// Polymorphism in action
makeThemSound(myAnimal);
makeThemSound(myDog);
makeThemSound(myCat);
}

public static void makeThemSound(Animal animal) {
animal.makeSound();
}
}
```

In this example, the `Animal` class has a method `makeSound()` that prints a generic message. The `Dog` and `Cat` classes extend `Animal` and override the `makeSound()` method to provide their own specific messages. When we call `makeSound()` on an `Animal` reference that actually points to a `Dog` or `Cat` object, the overridden method is called, demonstrating polymorphism.

The `makeThemSound` method in the `Main` class accepts an `Animal` reference and calls the `makeSound()` method. Because of polymorphism, the correct overridden method is called depending on the actual object type, even though the reference type is `Animal`.

This is a simple but powerful demonstration of how polymorphism allows for flexibility and reusability in code. It enables developers to write more generic and maintainable code by allowing a single interface to interact with different underlying forms (or classes).


2024-05-12 12:15:32

Harper Davis

Studied at the University of Oxford, Lives in Oxford, UK.
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.
2023-06-14 06:34:27

Benjamin Brown

QuesHub.com delivers expert answers and knowledge to you.
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.
ask:3,asku:1,askr:137,askz:21,askd:152,RedisW:0askR:3,askD:0 mz:hit,askU:0,askT:0askA:4