DEV Community

velvizhi Muthu
velvizhi Muthu

Posted on

Module-3:Polymorphism

  1. What is Polymorphism in Java?
    Polymorphism in Java is a fundamental concept in object-oriented programming that allows one interface or method to represent multiple forms or behaviours.

  2. How many types of Polymorphism?
    There are 2 Types of polymorphism :

    1. Method Overloading or Static Polymorphism
    2. Method Overriding or Dynamic Polymorphism
  3. What is Method Overloading?

Method overloading is the same method name and different types of arguments or a different number. argument in the same class.
Also known as static polymorphism due to compile time polymorphism.
Achieved through method overloading.
Resolved by the compiler (at compile time).

  1. What is Method overriding?
    Method overriding is the same method name and the same type of argument, and the same no. of argument in a different class.
    Also known as dynamic polymorphism.
    Resolved by the JVM at runtime.

  2. Why Use Polymorphism in Java?
    Code reusability-write one method, use it in many ways.
    The flexibility program can decide at runtime which method to call.
    Extensibility-new classes can be added without changing old code.

  3. When do we use polymorphism?
    We use polymorphism in Java when we want to write code that works with different types of objects in a consistent way, especially when those objects share a common interface or superclass.

Use polymorphism when you want different classes to be treated as one type and behave differently depending on which class is actually used.

  1. Where do we use the Polymorphism?
    1. In Method Parameters and Return Types

We use polymorphism to pass different object types (that share a common superclass or interface) into the same method or return them from a method.

02. In Collections (e.g., List, Set, Map)
Enter fullscreen mode Exit fullscreen mode

Java collections often store a group of objects of a common supertype (like an interface), allowing polymorphic behaviour.

  1. In Abstract Classes and Interfaces

Whenever you define an interface or abstract class, you’re using polymorphism by design. You can refer to all subclasses through the base type.

 04. In Overriding Methods (Runtime Polymorphism )
Enter fullscreen mode Exit fullscreen mode

Polymorphism is used when a subclass overrides a method from the parent class, and you call it using the superclass reference.

 05. In Design Patterns
Enter fullscreen mode Exit fullscreen mode

Many common design patterns in Java rely on polymorphism.

  06. In Frameworks and Libraries
Enter fullscreen mode Exit fullscreen mode

Java frameworks (like Spring, Hibernate) and APIs use polymorphism extensively to let developers work with general types like interfaces (List, Map, Service, etc.).

  07. In GUI Applications
Enter fullscreen mode Exit fullscreen mode

Polymorphism is used in graphical applications (like Java Swing or JavaFX), where UI components inherit from a base class.

Top comments (0)