Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is Polymorphism in Java? Compile-Time vs Runtime

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 29, 2026
6 min read

Polymorphism in Java means that one name or one reference type can behave in many ways. There are two kinds: compile-time polymorphism, achieved by method overloading and resolved by the compiler, and runtime polymorphism, achieved by method overriding and resolved by the JVM according to the actual object type.

This guide explains both kinds with runnable examples, shows how dynamic dispatch picks a method, and lists the traps, such as fields and static methods not being polymorphic. Code targets Java 21 and Java 25 LTS.

Table of Contents

  • Two kinds of polymorphism
  • Compile-time polymorphism with overloading
  • Runtime polymorphism with overriding
  • Upcasting, downcasting and instanceof
  • What is not polymorphic
  • Frequently Asked Questions
    • What is the difference between overloading and overriding?
    • Can constructors be overridden?
    • What is dynamic method dispatch?
    • Is polymorphism possible with interfaces?
  • Summary
  • Related Articles

Two kinds of polymorphism

The word comes from Greek for many forms. In Java it shows up in two places.

AspectCompile-timeRuntime
MechanismMethod overloadingMethod overriding
Also calledStatic binding, early bindingDynamic dispatch, late binding
Decided byCompiler, from argument typesJVM, from the object's real class
Needs inheritanceNoYes (or an interface)

Compile-time polymorphism with overloading

Overloading means several methods share a name but differ in the number or types of parameters. The compiler picks one by looking at the arguments you pass.

java
java
text

A different return type alone does not make an overload; the parameter list must differ. When no exact match exists, Java widens primitives (int to long to double) or boxes them, which can sometimes pick a method you did not expect, so keep overloads clearly distinct.

Runtime polymorphism with overriding

When a subclass overrides a method, a call through a parent reference runs the child version. The JVM chooses the method at runtime using the real class of the object, not the declared type of the variable.

java
java
text

The loop code knows only Shape, yet each call runs the right area(). You can add Triangle later without changing the loop. This is the main benefit: code written against a general type works with every future subtype.

Upcasting, downcasting and instanceof

Assigning a child to a parent variable is upcasting and is always safe. Going back down needs a cast and may fail. Pattern matching for instanceof (standard since Java 16) makes it safe and short.

java

Since Java 21 you can also use pattern matching in switch, which works well with sealed hierarchies:

java

Casting to the wrong type throws ClassCastException, so prefer pattern matching over blind casts.

What is not polymorphic

Dynamic dispatch applies to instance methods only. Three things surprise beginners:

  1. Fields are resolved by the declared type, not the object type. A field with the same name in a subclass hides, not overrides.
  2. Static methods are hidden, not overridden, and the call is bound at compile time.
  3. private and final methods cannot be overridden, so no dynamic dispatch happens for them.
java

Overloading is also decided by the declared type of the argument. Calling print(Object) with a String held in an Object variable picks the Object overload.

Frequently Asked Questions

What is the difference between overloading and overriding?

Overloading is several methods with the same name and different parameters, chosen at compile time. Overriding is a subclass replacing a parent method with the same signature, chosen at runtime.

Can constructors be overridden?

No. Constructors are not inherited, so they cannot be overridden. You can overload them with different parameter lists.

What is dynamic method dispatch?

It is the JVM process of selecting which overridden method to run based on the real class of the object at the moment of the call.

Is polymorphism possible with interfaces?

Yes. A variable of an interface type can hold any implementing object, and calls run the implementation of that object's class.

Summary

Compile-time polymorphism is overloading, resolved by the compiler from argument types. Runtime polymorphism is overriding, resolved by the JVM from the object's actual class. Program against general types such as abstract classes and interfaces, and remember that fields, static methods and private methods do not dispatch dynamically.

Related Articles

  • What is the purpose of the super keyword in Java?
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • What Is Composition vs Inheritance in Java and Which Should You Prefer?
  • What Is the equals and hashCode Contract in Java?
  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • What Is Inheritance in Java and How Does extends Work?
  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • What Are Access Modifiers in Java: public, private, protected, default?
  • What Is the static Keyword in Java? Variables, Methods, Blocks