Entri Blog
No Result
View All Result
Sunday, February 5, 2023
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
FREE GK TEST: SIGNUP NOW
Entri Blog
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
No Result
View All Result
Entri Blog
Free GK Test
banner top article banner top article
Home Articles

Understanding Polymorphism In Java

by Feeba Mahin
November 23, 2022
in Articles
Understanding Polymorphism In Java
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • Polymorphism in Java
  • Types of Polymorphism
  • 1. Compile-time Polymorphism:
  • 2. Run-time Polymorphism:
  • Usages and Advantages of Polymorphism
  • Differences Between Compile-time and Run-time Polymorphism

 

What is Polymorphism? In simple words, it means “Many Forms”. What this means in the object-oriented programming language like Java is that, a single line of code (Here, a single line of code can refer to the name of a method, or a variable, etc.) can have multiple meanings, and which meaning is to be used depends upon various factors.

Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding. For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time. Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.

Note: The print () method is also an example of polymorphism. It is used to print values of different types like char, int, string, etc. We can achieve polymorphism in Java using the following ways: During inheritance in Java, if the same method is present in both the superclass and the subclass.

Polymorphism in Java

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

If you overload a static method in Java, it is the example of compile time polymorphism.

Learn to code from industry experts! Enroll here

Types of Polymorphism

Java supports two types of polymorphism namely:

  1. Compile-time polymorphism
  2. Run-time polymorphism.

1. Compile-time Polymorphism:

Compile-time polymorphism is the type of polymorphism occurs when the compiler compiles a program. Java supports compile-time polymorphism through method overloading. The other names of Compile-time polymorphism are static polymorphism or early binding.

Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface.

In method overloading, two or more methods in a class can use the same name as long as their parameter declarations are different.

When the compiler encounters a call to an overloaded method, it identifies the correct version of the overloaded method by comparing the type and the number of arguments.

Example:

public class OverloadingDemo {
    public void meth(int value) {
        System.out.println("int value : " + value);
    }
    public void meth(String name) {
        System.out.println("String value : " + name);
    }
    public void meth(int value, String name) {
        System.out.println("Name with value : " + value+" "+name);
    }
    public static void main(String[] args) {
        OverloadingDemo demo = new OverloadingDemo();
        demo.meth(10);
        demo.meth("online tutorials point");
        demo.meth(20, "Overloading Demo");
    }
}
int value : 10
String value : online tutorials point
Name with value : 20 Overloading Demo

2. Run-time Polymorphism:

Run-time Polymorphism is the type of polymorphism that occurs dynamically when a program executes. Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding.

For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time.

Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.

interface Car {
    void speed(int speed);
}
class Maruti implements Car {
    public void speed(int speed) {
        System.out.println("Maruti Speed is :" + speed);
    }
}
class Tata implements Car {
    public void speed(int speed) {
        System.out.println("Tata Speed is :" + speed);
    }
}
public class PolymorphismDemo {
    public static void main(String[] args) {
        Car car;
        car = new Maruti();
        car.speed(200);
        car = new Tata();
        car.speed(300);
    }
}

Output:

Console
Maruti Speed is :200 
Tata Speed is :300

In the above example – Maruthi and Tata are two different classes, and both were two different implementations of the Car Interface. In other words, these two classes are types of Car.

Since because these two classes are the same type, we can refer these two objects to a single type called Car as above. Here, based on the type of the object being assigned with the reference, polymorphism is resolved at runtime.

Grab the opportunity to learn Java with Entri! Click Here

Usages and Advantages of Polymorphism

  • Method overloading allows methods that perform similar or closely related functions to be accessed through a common name. For instance, a program performs operations on an array of numbers which can be int, float, or double type. Method overloading allows you to define three methods with the same name and different types of parameters to handle the array of operations.

  • Constructors allowing different ways to initialize objects of a class can implement method overloading. This enables you to define multiple constructors for handling different types of initializations.

  • Method overriding allows a subclass to use all the general definitions that a superclass provides and add specialized definitions through overridden methods.

  • Method overriding works together with inheritance to enable code-reuse of existing classes without the need for re-compilation.

Differences Between Compile-time and Run-time Polymorphism

  • Method overloading occurs at Compile-time whereas method overriding occurs at Run-time.

  • Method overloading occurs when the type signatures of the two methods are different whereas method overriding occurs only when the type signatures of the two methods are the same.

  • In method overloading, the compiler calls the correct method by comparing the type signatures. However, in method overriding, the JVM determines the correct method based on the object that the invoking variable is referring to.

  • Method overloading is possible on methods with private, static, and final access modifiers. On the other hand, method overriding is not possible on these access modifiers.

Share61SendShare
Feeba Mahin

Feeba Mahin

Related Posts

How to Introduce Yourself in an Interview- Self Introduction Tips and Tricks
Articles

How to Introduce Yourself in an Interview- Self Introduction Tips and Tricks

February 1, 2023
JKPSC CCE Admit Card 2023 Out: Download Link, Check Exam Date
Admit Card

JKPSC CCE Admit Card 2023 Out: Download Link, Check Exam Date

February 1, 2023
UKPSC Patwari Exam Date 2022 Declared - Admit Card, Download Link
Admit Card

UKPSC Patwari Exam Date 2022 Declared – Admit Card, Download Link

February 1, 2023
Next Post
bpsc assistant cutoff 2022

BPSC Assistant Town Planning Supervisor Cutoff 2022

Discussion about this post

Latest Posts

  • Learn Continuous Tense Examples
  • Examples of Adverb in English Sentences
  • Slogans on World Environment Day
  • Top CSS Tools for Web Developer in 2023
  • TNPSC Inspector of Fisheries Admit Card 2023 Out: Download Here

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages 2023 – PDF Download

    149828 shares
    Share 59928 Tweet 37455
  • List of Government Banks in India 2023: All you need to know

    61097 shares
    Share 24439 Tweet 15274
  • TNPSC Group 2 Posts and Salary Details 2022

    39459 shares
    Share 15784 Tweet 9865
  • New Map of India with States and Capitals 2023

    28565 shares
    Share 11426 Tweet 7141
  • Odisha Police Recruitment 2023 PDF Download for 4790 Posts – Eligibility, Selection Process

    863 shares
    Share 345 Tweet 216

Company

  • Become a teacher
  • Login to Entri Web

Quick Links

  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET

© 2021 Entri.app - Privacy Policy | Terms of Service