Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Mukesh
Mukesh

Posted on

Polymorphism in Java – Understanding Compile-time and Runtime Behavior

Polymorphism in Java is one of the core concepts of Object-Oriented Programming (OOP). It allows us to perform a single action in different ways. Polymorphism means"many forms". In simple terms, it allowsone interface to be used for multiple implementations, making our code flexible and reusable.

Types of Polymorphism in Java

In Java, polymorphism is classified into two main types:

  1. Compile-time Polymorphism (Method Overloading)
  2. Runtime Polymorphism (Method Overriding)

1. Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism occurs when the method call is resolved atcompile time. The most common example of this isMethod Overloading.

What is Method Overloading?

If a class has multiple methods with thesame name but different parameter lists (different number or type of parameters) it is calledMethod Overloading.

Example:

classCalculator{intadd(inta,intb){returna+b;}doubleadd(doublea,doubleb){returna+b;}}publicclassMain{publicstaticvoidmain(String[]args){Calculatorcalc=newCalculator();System.out.println(calc.add(5,10));// calls int versionSystem.out.println(calc.add(5.5,10.2));// calls double version}}
Enter fullscreen modeExit fullscreen mode

Why use it?

  • Increasesreadability of the program
  • Same method name with different argument list iseasy to remember

Ways to Achieve Overloading

  1. By changing thenumber of arguments
  2. By changing thedata type of arguments

2. Runtime Polymorphism (Method Overriding)

Runtime polymorphism, also known asDynamic Method Dispatch, is achieved throughMethod Overriding. In this case, the method call is resolvedat runtime, not at compile time.

What is Method Overriding?

If asubclass (child class) providesspecific implementation of method that is already defined in itsparent class, it is calledMethod Overriding.

Rules for Overriding:

  1. The method in the child class must have the same name, return type, and parameters as in the parent class.
  2. It requires inheritance (usingextends).

Example:

classAnimal{voidsound(){System.out.println("Animal makes a sound");}}classDogextendsAnimal{@Overridevoidsound(){System.out.println("Dog barks");}}publicclassMain{publicstaticvoidmain(String[]args){Animala=newDog();// Upcastinga.sound();// Output: Dog barks (resolved at runtime)}}
Enter fullscreen modeExit fullscreen mode

Why use it?

  1. To provide a specific implementation in the child class
  2. To achieve runtime polymorphism.

Conclusion

  1. Polymorphism makes Java programs more flexible, extensible, and easier to maintain.
  2. UseOverloading for compile-time decisions and code clarity.
  3. UseOverriding for dynamic behavior and runtime flexibility.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hey,I am Software Engineer from India. I am interested in developing software solutions using Java, JavaScript, HTML, CSS.
  • Location
    India
  • Work
    Senior Software Engineer - Full-stack Java | JavaScript | HTML | CSS
  • Joined

More fromMukesh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp