Java polymorphism example

Dick 211 Published: 11/18/2024

Java polymorphism example

I'd be happy to provide an example of Java polymorphism!

Polymorphism is a fundamental concept in object-oriented programming (OOP). In Java, it allows you to define methods that can work with different data types without knowing the specific type at compile-time.

Here's a simple example:

// Define a superclass Animal with a method sound()

class Animal {

public void sound() {

System.out.println("The animal makes a generic sound");

}

}

// Define a subclass Dog that extends Animal

class Dog extends Animal {

@Override

public void sound() {

System.out.println("The dog barks");

}

}

// Define another subclass Cat that also extends Animal

class Cat extends Animal {

@Override

public void sound() {

System.out.println("The cat meows");

}

}

public class Main {

public static void main(String[] args) {

// Create instances of Dog and Cat

Dog myDog = new Dog();

Cat myCat = new Cat();

// Create an array of Animal type

Animal[] animals = new Animal[2];

animals[0] = myDog;

animals[1] = myCat;

// Loop through the array and call sound() on each animal

for (Animal animal : animals) {

animal.sound();

}

}

}

When we run this code, it will output:

The dog barks

The cat meows

As you can see, even though we only defined a single method sound() in the superclass Animal, our subclasses Dog and Cat were able to override that method with their own implementation.

This is an example of method overriding, which is one type of polymorphism. In this case, the sound() method behaves differently depending on the actual type of the object (i.e., whether it's a Dog or a Cat).

Another type of polymorphism in Java is method overloading. This involves defining multiple methods with the same name but different parameter lists.

Here's another example:

public class Calculator {

public int add(int x, int y) {

return x + y;

}

public double add(double x, double y) {

return x + y;

}

}

In this case, we've defined two add() methods with different parameter types. We can then create an instance of the Calculator class and call one or both of these methods:

public class Main {

public static void main(String[] args) {

Calculator calculator = new Calculator();

int result1 = calculator.add(5, 3); // calls the int version

double result2 = calculator.add(5.5, 3.4); // calls the double version

System.out.println("Result 1: " + result1);

System.out.println("Result 2: " + result2);

}

}

When we run this code, it will output:

Result 1: 8

Result 2: 9.0

As you can see, Java polymorphism allows us to write more flexible and reusable code by defining methods that can work with different data types or parameter lists.

Java polymorphism example

I'd be happy to provide a Java polymorphism example. Here's a detailed explanation:

Polymorphism is one of the fundamental concepts in object-oriented programming (OOP). It is the ability of an object to take on multiple forms, depending on the context or situation it is in. In Java, polymorphism can be achieved through method overriding and method overloading.

Method Overriding:

Method overriding is a way to provide a specific implementation for a method that has already been defined in a superclass. This allows subclasses to provide their own implementation of an inherited method.

Here's an example:

public class Animal {

public void sound() {

System.out.println("The animal makes a sound.");

}

}

public class Dog extends Animal {

@Override

public void sound() {

System.out.println("The dog barks.");

}

}

public class Cat extends Animal {

@Override

public void sound() {

System.out.println("The cat meows.");

}

}

In this example, the Animal class has a method called sound() that prints a generic message. The Dog and Cat classes override the sound() method to provide their own implementation.

Now, let's see how polymorphism comes into play:

public class Main {

public static void main(String[] args) {

Animal animal = new Dog();

animal.sound(); // Output: The dog barks.

animal = new Cat();

animal.sound(); // Output: The cat meows.

}

}

In the main() method, we create an instance of Dog and assign it to the animal variable, which is of type Animal. We then call the sound() method on this object. Since we are dealing with a Dog, the overridden sound() method in the Dog class is called.

Next, we reassign the animal variable to an instance of Cat and call the same sound() method again. This time, since we are dealing with a Cat, the overridden sound() method in the Cat class is called.

This demonstrates polymorphism because the sound() method behaves differently depending on the type of object it's called on. The same method name is used, but its behavior changes based on the object's runtime type.

Method Overloading:

Method overloading is a way to provide multiple definitions for a method with the same name, as long as they have different parameter lists. This allows us to write methods that can perform different tasks based on the number and types of parameters passed to them.

Here's an example:

public class Calculator {

public int add(int x, int y) {

return x + y;

}

public double add(double x, double y) {

return x + y;

}

}

In this example, we have a Calculator class with two methods named add(). The first one takes two int parameters and returns their sum. The second one takes two double parameters and also returns their sum.

Now, let's see how method overloading comes into play:

public class Main {

public static void main(String[] args) {

Calculator calculator = new Calculator();

int result1 = calculator.add(2, 3); // Output: 5

double result2 = calculator.add(2.5, 3.7); // Output: 6.2

}

}

In the main() method, we create a Calculator object and call its add() methods with different parameter types. The first time, we pass two int values to the add(int, int) method. The second time, we pass two double values to the add(double, double) method.

This demonstrates polymorphism because the add() method behaves differently depending on the types of parameters passed to it. The same method name is used, but its behavior changes based on the input.

In this example, we've demonstrated both method overriding and method overloading in Java, showcasing how polymorphism allows us to write more flexible and reusable code.