Polymorphism in Java

Java Polymorphism

In object-oriented programming, polymorphism in java is an important concept that allows objects of different classes to be treated as if they are objects of the same class.

Java, being an object-oriented programming language, supports polymorphism, which makes it a powerful and flexible language for developing complex applications.

In this article, we will discuss what polymorphism is, how it works in Java, and some examples of its implementation.

What is Polymorphism in Java?

Polymorphism is derived from the Greek words ‘poly’ and ‘morph,’ meaning ‘many’ and ‘forms,’ respectively. In programming, polymorphism refers to the ability of objects of different classes to be treated as if they are objects of the same class. This allows a single interface to be used for different types of objects, making the code more flexible and reusable.

Compile-time Polymorphism

  • Compile-time polymorphism, also known as static polymorphism or method overloading, occurs when the compiler determines which method to invoke based on the number and types of arguments passed to it.
  • The method signatures must be different for the compiler to distinguish between them. This means that the methods have the same name but different parameters.

Runtime Polymorphism

  • Runtime polymorphism, also known as dynamic polymorphism or method overriding, occurs when the method to be invoked is determined at runtime based on the actual type of the object.
  • This means that a method defined in a superclass can be overridden in a subclass to provide a specific implementation.

Polymorphism in Java Implementation

  • Polymorphism in Java is implemented through inheritance and method overriding. To achieve polymorphism, we create a superclass and define methods that can be overridden in the subclass.
  • When we create an object of the subclass, we can call the methods of the superclass on the object, and the overridden method in the subclass will be executed instead.

Advantages of Polymorphism in Java

Polymorphism

Polymorphism Methods In Java 

MethodsDescription
Method Overloading:This is when a class has multiple methods with the same name, but different parameters. 
Method Overriding:This is when a subclass provides its own implementation of a method that is already defined in its parent class. 
Ad-Hoc Polymorphism:  This is when a method can take arguments of different types and behave differently depending on the type of the argument.
Interface Implementation:Interfaces define a set of methods that a class must implement

Example 1: The concept of Compile-time Polymorphism or method overloading.

Run
class ComplitetimePolymorph { 
  
    void operator(String str1, String str2) 
    { 
        String s = str1 + str2; 
        System.out.println("Concatenated String - " + s); 
    } 
  
    void operator(int a, int b) 
    { 
        int c = a + b; 
        System.out.println("Sum = " + c); 
    } 
} 
  
class Main { 
    public static void main(String[] args) 
    { 
        ComplitetimePolymorph obj = new ComplitetimePolymorph(); 
        obj.operator(10, 20); 
        obj.operator("Hey", "Prepster"); 
    } 
} 

Output

Sum = 30

Concatenated String- HeyPrepster

Example 2: Polymorphism in Java through the use of inheritance and method overriding.

Run
class Animal { 
  
    void Print() 
    { 
        System.out.println("parent class"); 
    } 
} 
  
class Dog extends Animal { 
  
    void Print() 
    { 
        System.out.println("Dog barks"); 
    } 
} 
  
class Cat extends Animal { 
  
    void Print() 
    { 
        System.out.println("Cat meows"); 
    } 
} 
  
class Main { 
    public static void main(String[] args) 
    { 
  
        Animal a; 
  
        a = new Dog(); 
        a.Print(); 
  
        a = new Cat(); 
        a.Print(); 
    } 
}

Output

Dog barks
Cat meows

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Question 1. What is the difference between compile-time polymorphism and runtime polymorphism?

Compile-time polymorphism (also known as method overloading) is when multiple methods with the same name but different parameters are defined in a class. The correct method to be called is determined at compile-time based on the method signature.

Runtime polymorphism (also known as method overriding) is when a subclass provides its own implementation of a method that is already defined in its superclass. The correct method to be called is determined at runtime based on the actual object being referenced.

Question 2. What is the difference between method overloading and method overriding?

Method overloading is when multiple methods with the same name but different parameters are defined in a class. The methods can have different return types, access modifiers, and exceptions, but must have a different method signature. Method overloading is determined at compile-time based on the method signature.

Method overriding is when a subclass provides its own implementation of a method that is already defined in its superclass. The method must have the same name, return type, and parameters as the method in the superclass. Method overriding is determined at runtime based on the actual object being referenced.

Question 3. Can a subclass override a static method in its superclass?

Yes, a subclass can override a static method in its superclass. However, the method in the subclass is not considered to be an override, but rather a new method that hides the static method in the superclass.

Question 4. Can you give an example of how you would use Polymorphism in a real-world scenario?

Polymorphism could be used in a real-world scenario is in a payroll system for a company. Each employee could be represented as an object, with different subclasses for different types of employees, such as full-time, part-time, and contract employees. Each subclass could have its own implementation of the calculatePay() method, using different formulas based on the employee type. The payroll system could then call the calculatePay() method on each employee object, and the correct method would be called based on the actual object being referenced, achieving Polymorphism.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription