Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsObject-Oriented Programming
✓ FreeBeginner· 8 min read

Polymorphism, Overloading & Overriding — Interview Questions

Compile-time vs runtime polymorphism, how the compiler picks an overload, overriding rules (covariant returns, exceptions, access), dynamic method dispatch and @Override.

Published September 25, 2026


How to use this lesson

Polymorphism questions come in a predictable sequence: define it → overloading vs overriding → the rules → "what does this code print?". Being precise about the rules is what separates a good answer from an average one.

Q1. What is polymorphism in Java?

Short answer: Polymorphism ("many forms") means one interface or method name can behave differently depending on the actual object or the arguments. Java has two kinds:

  • Compile-time (static): method overloading, resolved by the compiler.
  • Runtime (dynamic): method overriding, resolved by the JVM based on the object's real class.
abstract class Shape { abstract double area(); }
class Circle extends Shape { double r = 1; double area() { return Math.PI * r * r; } }
class Square extends Shape { double s = 2; double area() { return s * s; } }

List<Shape> shapes = List.of(new Circle(), new Square());
for (Shape s : shapes) System.out.println(s.area());   // same call, different behaviour per object

Key points to cover:

  • The payoff: code written against Shape works with shapes that don't exist yet (open/closed principle).

Learn it in depth → Inheritance and Polymorphism

Q2. What is method overloading?

Short answer: Declaring several methods with the same name but different parameter lists in the same class (or inherited into it). The compiler decides which one to call from the arguments. That's compile-time polymorphism.

class PriceFormatter {
    String format(long paise)                    { return format(paise, "INR"); }
    String format(long paise, String currency)   { return currency + " " + paise / 100.0; }
    String format(BigDecimal amount)             { return "INR " + amount.toPlainString(); }
}

Key points to cover:

  • Common uses: convenience variants with default arguments (Java has no default parameter values), and support for different input types. Examples include println(int), println(String) and List.of(...).

Q3. How does overloading relate to polymorphism?

Short answer: It's the static form of polymorphism. One method name has many forms, and the right one is chosen at compile time from the declared types of the arguments.

Key points to cover:

  • Because the choice happens at compile time, it uses the static (declared) type, not the runtime type:
void print(Object o) { System.out.println("object"); }
void print(String s) { System.out.println("string"); }

Object x = "hello";
print(x);     // prints "object": the declared type is Object, so print(Object) is chosen

Q4. How does the compiler decide which overloaded method to call?

Short answer: It finds every method whose name matches and whose parameters can accept the arguments, then picks the most specific one. It does this in phases:

  1. An exact match, or widening of primitives (int → long), without boxing or varargs.
  2. Allowing autoboxing and unboxing.
  3. Allowing varargs.
void f(long x)      { System.out.println("long"); }
void f(Integer x)   { System.out.println("Integer"); }
void f(int... x)    { System.out.println("varargs"); }

f(5);   // "long": widening (phase 1) beats boxing (phase 2) and varargs (phase 3)

Common trap: if two candidates are equally specific, such as f(null) with f(String) and f(Integer), the call is ambiguous, and it's a compile error.

Q5. Can methods be overloaded by return type alone?

Short answer: No. int total() and long total() in the same class is a compile error. The compiler chooses an overload from the call's arguments, and a call like total(); gives it nothing to decide with.

Q6. What are the rules for method overloading?

Short answer: The method name must be the same, and the parameter lists must differ in the number, types or order of the parameters.

Key points to cover:

  • The return type, access modifier and throws clause may differ, but they don't count towards telling overloads apart.
  • Static and instance methods can overload each other.
  • Generic methods that erase to the same signature clash: void m(List<String>) and void m(List<Integer>) is a compile error.

Q7. What is method overriding?

Short answer: A subclass provides its own implementation of an instance method inherited from a parent class or an interface, with the same name and parameter list. The implementation that runs is chosen at runtime, from the object's actual class.

class Notifier { void send(String msg) { System.out.println("log: " + msg); } }
class SmsNotifier extends Notifier {
    @Override void send(String msg) { System.out.println("SMS: " + msg); }
}
Notifier n = new SmsNotifier();
n.send("OTP 4821");    // "SMS: OTP 4821": the runtime type decides

Learn it in depth → Inheritance and Polymorphism

Q8. What are the rules for method overriding?

Short answer:

  • The name and parameter list must be the same.
  • The return type must be the same or a subtype (a covariant return).
  • Access can't be more restrictive (public can't become protected).
  • It can't throw new or broader checked exceptions.
  • static, final and private methods can't be overridden.
RuleAllowedNot allowed
Return typeAnimal get() → Dog get() (covariant)Dog → Animal, or a primitive change
Accessprotected → publicpublic → protected
Checked exceptionsSame, narrower, or noneNew or broader (IOException → Exception)
Unchecked exceptionsAny—
final method—Can't be overridden
static methodCan be hiddenNot overridden (no dynamic dispatch)
private method—Not visible, so a same-name method is a new method

Common trap: saying the return type must be "exactly the same". Covariant returns have been allowed since Java 5. clone() overrides commonly return the subclass type.

Q9. What does the @Override annotation do?

Short answer: It tells the compiler that you intend to override a method. If no matching method exists in a supertype, for example because of a typo or a wrong parameter type, compilation fails instead of silently creating a new overload.

class Money {
    @Override
    public boolean equals(Money other) { … }   // compile error: this overloads equals(Object), it doesn't override it
}

Key points to cover:

  • The annotation is optional, but you should always use it. The bug above is classic: HashSet would silently ignore that equals.
  • It also works when implementing interface methods.

Q10. What is dynamic method dispatch?

Short answer: The JVM's mechanism for choosing which overridden method to run at runtime, based on the real class of the object rather than the reference type. It's how runtime polymorphism is implemented.

Key points to cover:

  • Fields are not polymorphic. Field access uses the reference type:
class A { String name = "A"; String who() { return "A"; } }
class B extends A { String name = "B"; @Override String who() { return "B"; } }
A obj = new B();
System.out.println(obj.who());   // "B": method dispatched at runtime
System.out.println(obj.name);    // "A": fields are resolved by the declared type
  • HotSpot implements dispatch with virtual method tables, and the JIT often inlines calls when only one implementation is actually used, so the cost is usually negligible.

Q11. What happens when several subclasses override the same method?

Short answer: Each subclass has its own version, and a call through a parent reference runs the version belonging to the object's actual class. That's exactly what lets one loop handle Circle, Square and Triangle differently.

Key points to cover:

  • In a multilevel chain (A → B → C), C inherits B's override unless it overrides the method again. The most specific override in the object's class hierarchy wins.

Q12. Can constructors be polymorphic or overridden?

Short answer: No. Constructors aren't inherited, so they can't be overridden, and they don't take part in dynamic dispatch. They can be overloaded, since a class can have several constructors with different parameters.

Common trap: calling an overridable method from a constructor. The subclass override runs before the subclass's fields are initialised:

class Base { Base() { init(); } void init() { } }
class Child extends Base {
    private List<String> items = new ArrayList<>();
    @Override void init() { items.add("x"); }   // NPE: items is still null when Base() runs
}

Follow-up questions this topic invites — and their answers

Q: Overloading vs overriding in one line each? A: Overloading means the same name with different parameters in one class, resolved at compile time. Overriding means the same signature in a subclass that replaces inherited behaviour, resolved at runtime.

Q: Can we overload a static method, and can we override one? A: Overloading, yes. Overriding, no. A subclass static method with the same signature hides the parent's method, and the version called depends on the reference type used at compile time.

Q: Can an overriding method be synchronized or final when the parent's isn't? A: Yes. synchronized, strictfp and final can be added in an override. Adding final stops further overriding down the hierarchy.

Q: Can an interface's default method be overridden? A: Yes. The implementing class can override it like any other inherited method, and can call the original with InterfaceName.super.method().

Previous

Inheritance, Composition, this & super — Interview Questions

Next

Abstraction, Interfaces & Encapsulation — Interview Questions

AI Tutor

Lesson: Polymorphism, Overloading & Overriding — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.