What is polymorphism in Java with realtime example?

Chris 233 Published: 07/29/2024

What is polymorphism in Java with realtime example?

I'm happy to help!

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows for more flexibility and dynamic behavior in your code. In Java, polymorphism enables you to treat objects of different classes as if they were of the same class.

There are two main types of polymorphism: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).

Method Overloading (Compile-time Polymorphism)

Method overloading is when multiple methods with the same name but different parameters can be defined in a single class. This allows for more flexibility in how you interact with an object, as the correct method to call will be determined based on the number and types of parameters passed.

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; }

}

Calculator calc = new Calculator();

int result1 = calc.add(2, 3); // calls the int-based method

double result2 = calc.add(2.5, 3.5); // calls the double-based method

System.out.println(result1); // prints 5

System.out.println(result2); // prints 6.0

In this example, we have a Calculator class with two methods named add, one for integers and one for doubles. The correct method to call is determined based on the types of parameters passed.

Method Overriding (Runtime Polymorphism)

Method overriding is when a subclass provides a different implementation of a method that is already defined in its superclass. This allows for more specialized behavior at runtime, as the correct method to call will be determined based on the object's actual class.

Here's an example:

public abstract class Animal {

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

}

public class Dog extends Animal {

@Override

public void sound() { System.out.println("Dog barks!"); }

}

public class Cat extends Animal {

@Override

public void sound() { System.out.println("Cat meows!"); }

}

Animal myPet = new Dog();

myPet.sound(); // outputs "Dog barks!"

myPet = new Cat();

myPet.sound(); // outputs "Cat meows!"

In this example, we have an Animal abstract class with a sound() method. Two concrete subclasses, Dog and Cat, override the sound() method to provide their own implementation. When we create objects of these classes and call the sound() method, the correct overridden method will be called based on the object's actual class.

In summary, polymorphism in Java allows for more flexibility and dynamic behavior by enabling you to treat objects of different classes as if they were of the same class. Method overloading provides compile-time polymorphism, while method overriding provides runtime polymorphism.

runtime polymorphism in java

I'd be happy to explain runtime polymorphism in Java!

What is Runtime Polymorphism?

In object-oriented programming (OOP), polymorphism refers to the ability of an object to take on multiple forms. In Java, there are two types of polymorphism: compile-time polymorphism (static polymorphism) and runtime polymorphism.

Compile-time polymorphism is achieved through method overriding or overloading, which is determined during compilation. On the other hand, runtime polymorphism occurs when the exact method to be called is determined at runtime, rather than at compile time.

How does Runtime Polymorphism work in Java?

In Java, runtime polymorphism can occur due to two main reasons:

Method Invocation: When a method is invoked on an object, the correct method to be executed is determined based on the actual type of the object at runtime. This is known as dynamic method dispatch. Casting: When you perform a cast operation on an object, its actual class information is obtained, which can trigger the execution of a different method.

Example: Animal Class Hierarchy

To illustrate runtime polymorphism, let's consider a simple example:

// Animal class (superclass)

public abstract class Animal {

public abstract void sound();

}

// Dog and Cat classes (subclasses of Animal)

public class Dog extends Animal {

public void sound() {

System.out.println("Woof!");

}

}

public class Cat extends Animal {

public void sound() {

System.out.println("Meow!");

}

}

Now, let's create a method that takes an Animal object as a parameter:

// AnimalSayer class (consumes Animal objects)

public class AnimalSayer {

public void say(Animal animal) {

// At runtime, determine the actual type of the animal

if (animal instanceof Dog) {

((Dog) animal).sound();

} else if (animal instanceof Cat) {

((Cat) animal).sound();

}

}

}

In this example:

say() is a method that accepts an Animal object as a parameter. At runtime, it determines the actual type of the Animal object using the instanceof keyword. Based on the determined type, the correct method to be called is executed. This is an example of runtime polymorphism.

Conclusion

In Java, runtime polymorphism occurs when the exact method to be called is determined at runtime, rather than at compile time. It's achieved through dynamic method dispatch or casting operations. In this explanation, we explored a simple example of runtime polymorphism using inheritance and method invocation.

I hope this helps you understand runtime polymorphism in Java!