Types of polymorphism in Java: Java has two types of polymorphism: compile-time polymorphism, achieved through method overloading and resolved by the compiler, and runtime polymorphism, achieved through method overriding and resolved by the JVM during execution. Compile-time is also called static polymorphism; runtime is also called dynamic polymorphism.
Knowing which type to use, and why, is one of the most tested Java interview topics at companies like TCS, Infosys and Wipro. This guide covers both types of polymorphism in Java with examples, a comparison table, JVM internals and the interview traps that trip up even experienced developers.
Key Takeaways
- Compile-time polymorphism uses method overloading; the compiler picks the right method based on the argument list.
- Runtime polymorphism uses method overriding; the JVM picks the right method based on the actual object type at execution time.
- Static methods, private methods and final methods cannot be overridden, only hidden.
- The JVM resolves runtime polymorphism using a virtual method table (vtable), a key detail interviewers love to probe.
- Understanding both types of polymorphism in Java is foundational for OOP design, secure coding and backend development roles.
Two Types of Polymorphism in Java
Polymorphism, from the Greek for “many forms,” lets one interface represent different underlying data types or behaviours. In Java, every polymorphic call is resolved at one of two moments: when the compiler processes your source code, or when the JVM executes the bytecode. That single distinction separates the two types of polymorphism in Java completely.
According to the Oracle Java documentation, polymorphism is one of the three pillars of object-oriented programming alongside encapsulation and inheritance. The Stack Overflow Developer Survey 2024 found that 30.3% of professional developers use Java, keeping it among the top five most-used languages for the twelfth consecutive year. You can find deeper context in our breakdown of Stack Overflow developer survey insights.
The JetBrains State of Developer Ecosystem 2024 reported that 60% of Java developers work in organisations with more than 1,000 employees, underscoring why enterprise-grade OOP concepts like the types of polymorphism in Java remain critical hiring criteria. According to the TIOBE Index (April 2025), Java holds a 15.16% rating, ranking it second globally in language popularity, reflecting its dominance in enterprise backend, Android and financial systems.
If you are just getting started with programming, it helps to first get comfortable with software development tools to learn first before digging into OOP concepts. Once your environment is set up, the two types of polymorphism in Java become much easier to experiment with hands-on.
Static Binding vs Dynamic Binding
Static binding (or early binding) means the compiler links a method call to a specific method definition at compile time. It happens when the method is private, static, or final, because those cannot be overridden. Dynamic binding (or late binding) means the JVM waits until runtime to decide which method to call, based on the actual object in memory, not the reference type declared in code.
Think of static binding as the compiler making a reservation at a specific restaurant table. Dynamic binding is more like arriving at the door and being seated wherever there is room based on who actually shows up.
Compile-Time (Static) Polymorphism in Java
Compile-time polymorphism in Java is achieved through method overloading. You define multiple methods in the same class with the same name but different parameter lists (different number, type or order of arguments). The compiler reads the call site and picks the best matching method before generating bytecode.
Here is a realistic example. A payment processing class in a fintech app might have a calculateTax(double amount) method and a calculateTax(double amount, String state) method. Both have the same name. The compiler chooses between them based on how many arguments you pass. No ambiguity, no runtime cost.
Rules the Compiler Follows
- The method name must be identical.
- The parameter list must differ in type, number or both. Return type alone is not enough to overload.
- Access modifiers can differ between overloaded versions.
- Autoboxing and varargs are considered last in the resolution order, which trips up many beginners.
What Interviewers Actually Ask About Compile-Time Polymorphism
A common trick question: “Can you overload a method by changing only the return type?” The answer is no. The compiler uses the method signature (name plus parameter list) to resolve calls, not the return type. If you try it, you will get a compile error. This is the kind of detail that separates candidates who have read about the types of polymorphism in Java from those who have actually coded it.
Operator overloading, common in C++, does not exist in Java. Java’s designers made that choice deliberately to keep the language simpler. If you are coming from a C background and want to see how compilation itself works, our guide on how to write, compile and run a C program shows the compile-link-execute chain clearly.
Runtime (Dynamic) Polymorphism in Java
Runtime polymorphism in Java is resolved by the JVM during program execution. It is achieved through method overriding, where a subclass provides its own implementation of a method already defined in its parent class. The method signature must match exactly, and the access modifier in the subclass cannot be more restrictive than in the parent.
This is the more powerful of the two types of polymorphism in Java. It is what makes frameworks like Spring, Android and Hibernate work at scale. When you write code against an interface or abstract class, the JVM figures out at runtime which concrete implementation to call.
A Real Runtime Polymorphism Example
Imagine an Indian banking app, similar to those built for SBI or HDFC Bank, with a parent class Account that has a method calculateInterest(). Two subclasses, SavingsAccount and FixedDepositAccount, each override that method with their own interest logic. Now write this:
Account acc = new SavingsAccount();
At compile time, the compiler sees type Account and confirms calculateInterest() exists there. At runtime, the JVM looks at the actual object, a SavingsAccount, and calls that version’s method. That is dynamic dispatch in action. This pattern is used extensively in Indian fintech platforms and enterprise Java applications built by companies like Infosys BPM and Wipro Technologies.
How the JVM Actually Resolves Runtime Polymorphism
The JVM uses a virtual method table (vtable) for each class. When a class is loaded, the JVM builds a table of method pointers. When you call an overridden method on a reference, the JVM looks up the vtable of the actual object’s class and calls the correct pointer. This lookup adds a tiny overhead compared to static binding, but it is negligible in practice and completely worth the flexibility you gain.
Runtime polymorphism also means you can run into Java exceptions and common runtime errors like NullPointerException or ClassCastException if your object references are not what you think they are. Knowing how dynamic binding works helps you debug those faster.
Can Static Methods Be Overridden in Java?
No, and this is one of the most reliable interview traps. Static methods belong to the class, not the instance. If a subclass defines a static method with the same signature as the parent’s static method, that is called method hiding, not overriding. The call is resolved at compile time based on the reference type, not the actual object. It looks like polymorphism but does not behave like it.
Students in BSc Computer Science programmes often encounter this in their OOP papers. It is equally relevant for MCA graduates targeting Java developer roles, where interviewers test exactly this distinction. In India, Java developer roles at product companies like Zoho, Freshworks and Razorpay routinely include questions on both types of polymorphism in Java at the screening stage.
Comparison Table: Compile-Time vs Runtime Polymorphism in Java
| Feature | Compile-Time (Static) | Runtime (Dynamic) |
|---|---|---|
| Also called | Static polymorphism, early binding | Dynamic polymorphism, late binding |
| Achieved by | Method overloading | Method overriding |
| Resolved by | Compiler | JVM at runtime |
| Binding type | Static binding | Dynamic binding |
| Inheritance required? | No | Yes |
| Performance | Faster (no runtime lookup) | Slight vtable overhead |
| Flexibility | Lower | Higher |
| Works with static methods? | Yes (overloading) | No (static methods are hidden, not overridden) |
| Works with private methods? | Yes (within class) | No |
| Real-world use | Utility methods, constructors | Frameworks, plugins, dependency injection |
Three Interview Traps to Know Cold
- Return-type-only overloading: It does not compile. The compiler needs different parameter lists to distinguish overloaded methods.
- Static method overriding: It is method hiding, not overriding. The resolved method depends on the reference type, not the object type.
- Covariant return types: Since Java 5, an overriding method can return a subtype of the parent’s return type. That is still valid overriding, and interviewers use it to test how well you know the JLS (Java Language Specification).
Practical Tips From the Field
One mistake developers make early in their careers: they override a method in a subclass but forget the @Override annotation. If they accidentally change the signature slightly (say, a parameter type mismatch), the compiler treats it as a new overloaded method instead of an override. The bug shows up at runtime, not compile time, and it can be painful to track down. Always use @Override. It costs nothing and catches that error immediately.
A second real-world tip: when you are designing a class hierarchy, prefer runtime polymorphism for behaviour that genuinely varies by type, and use overloading for convenience methods that handle different input formats. Mixing them up leads to bloated classes that are hard to maintain and test.
Where you go from here matters. If you understand both types of polymorphism in Java well, you are ready to design better class hierarchies, write cleaner interfaces and approach framework internals with confidence. Here are three concrete next steps:
- Write a small Java program this week with at least three overloaded methods and two overriding methods. Run it, add print statements inside each method, and confirm which one the JVM calls at runtime.
- Read the Java Language Specification section on method invocation to see exactly how the compiler and JVM resolve calls. It is dense but worth one read-through.
- If you are aiming for roles in cybersecurity, ethical hacking or AI, strong programming fundamentals like these are the entry point. Explore 3.0 University’s online certification courses in Cybersecurity, AI and Blockchain, or go straight to the CEH v13 AI online course, where understanding how code executes at runtime is genuinely useful when you are analysing vulnerabilities and writing exploits.
Frequently Asked Questions
What are the types of polymorphism in Java?
Java has two types of polymorphism: compile-time polymorphism (static polymorphism) and runtime polymorphism (dynamic polymorphism). Compile-time polymorphism is achieved through method overloading, where the compiler resolves which method to call based on the argument list. Runtime polymorphism is achieved through method overriding, where the JVM resolves the call at execution time based on the actual object type.
What is runtime polymorphism in Java with an example?
Runtime polymorphism in Java happens when a parent class reference points to a child class object, and the overridden method in the child class is called at runtime. For example, if Animal has a sound() method and Dog overrides it, writing Animal a = new Dog(); a.sound(); calls Dog’s version at runtime, not Animal’s. The JVM resolves this using the virtual method table of the actual object.
Compile-time polymorphism is achieved by what?
Compile-time polymorphism is achieved by method overloading. You define multiple methods in the same class with the same name but different parameter lists (different number or types of parameters). The compiler selects the correct method at compile time based on the arguments passed. Return type alone cannot distinguish overloaded methods in Java.
What is the difference between static and dynamic polymorphism in Java?
Static polymorphism is resolved by the compiler before the program runs, uses method overloading, and does not require inheritance. Dynamic polymorphism is resolved by the JVM during execution, uses method overriding, and requires an inheritance relationship between classes. Static binding is faster but less flexible; dynamic binding adds a small runtime lookup cost but enables far more extensible designs.
Can static methods be overridden in Java?
No. Static methods in Java cannot be overridden because they belong to the class, not to any instance. If a subclass defines a static method with the same signature as a parent’s static method, it is called method hiding, not overriding. The call is resolved at compile time based on the reference type, so dynamic dispatch does not apply to static methods.
What is method hiding in Java?
Method hiding in Java occurs when a subclass defines a static method with the same name and signature as a static method in its parent class. Unlike method overriding, method hiding is resolved at compile time based on the reference type, not the actual object. It is one of the most common interview traps when discussing the types of polymorphism in Java.
What is the role of vtable in Java polymorphism?
The virtual method table (vtable) is a data structure the JVM builds for each class when it is loaded. It contains pointers to the actual method implementations for that class. When runtime polymorphism resolves a method call, the JVM looks up the vtable of the actual object’s class to find and invoke the correct overridden method. This is how dynamic dispatch works internally in Java.
Last updated: May 2025. Reviewed by the 3University editorial team.