Skip to content
C

Java Interview Questions

Polymorphism Interview Questions

Compile-time vs runtime polymorphism, dynamic dispatch, and upcasting/downcasting.

Question 1: What is polymorphism in Java?

Ans

Polymorphism means "many forms" — the ability for the same method call or the same reference type to behave differently depending on the actual object involved. In Java, it lets you write code against a general type, such as a parent class or interface, while the specific behavior that runs is determined by whatever concrete object is actually being used.

Example

java
Animal a = new Dog(); a.eat(); // runs Dog's version of eat(), decided at runtime

Important Point

Java has two forms of polymorphism: overloading (compile-time) and overriding (runtime).

Question 2: What is method overloading, and how does Java decide which overload to call?

Ans

Method overloading means defining multiple methods with the same name but different parameter lists in the same class. The compiler decides which overload to call by matching the number, types, and order of the arguments at the call site before the program even runs, so the return type alone can never distinguish two overloads.

Example

java
void print(int x) { } void print(String x) { } print(10); // calls print(int) print("hi"); // calls print(String)

Important Point

If no exact match exists, Java can still widen a primitive argument or autobox it to find a compatible overload, following a defined precedence order.

Question 3: What is method overriding, and what rules must an overriding method follow?

Ans

Method overriding lets a subclass supply its own implementation of an inherited instance method with the same name and parameter list. The overriding method must have a compatible return type (the same type or a covariant subtype), cannot reduce the method's access level, and cannot throw new or broader checked exceptions than the method it overrides.

Example

java
class Animal { void sound() throws Exception { } } class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } // narrower exception, that's fine }

Important Point

The @Override annotation isn't required, but it lets the compiler catch a typo in the method signature that would otherwise silently create a new method instead of overriding one.

Question 4: What is upcasting, and why is it always safe?

Ans

Upcasting means treating a subclass object as an instance of its superclass, by assigning it to a superclass-typed reference. It's always safe and can even happen implicitly, because every subclass object genuinely is an instance of its superclass too.

Example

java
Dog d = new Dog(); Animal a = d; // upcasting, always safe, no cast needed

Important Point

After upcasting, you can only directly call methods declared on the superclass type through that reference — but overridden methods still run the subclass's version thanks to dynamic dispatch.

Question 5: What is downcasting, and why can it fail at runtime?

Ans

Downcasting means converting a superclass reference back to a specific subclass type, which requires an explicit cast because the compiler can't guarantee the object really is that subclass. If the object isn't actually an instance of the target subclass at runtime, Java throws a ClassCastException.

Example

java
Animal a = new Dog(); Dog d = (Dog) a; // safe here, a really holds a Dog Cat c = (Cat) a; // compiles, but throws ClassCastException at runtime

Important Point

Use instanceof (or Java's pattern-matching instanceof) to check the actual type before downcasting when you're not certain.

Question 6: Can static methods be overridden?

Ans

No. Static methods belong to the class and are hidden when a subclass declares a static method with the same signature.

The method chosen depends on the reference/class type rather than runtime object polymorphism.

Example

java
class A { static void show() {} } class B extends A { static void show() {} }

Important Point

Calling a static method through an object reference is legal in some cases but discouraged because it can confuse readers.

Question 7: Can private methods be overridden?

Ans

No. Private methods are not accessible to subclasses and are not inherited as overridable methods.

A child class can declare a method with the same name, but it is a separate method.

Example

java
class A { private void test() {} } class B extends A { private void test() {} }

Important Point

Do not describe this as runtime overriding.

Question 8: Overloading vs overriding?

Ans

Overloading uses the same method name with different parameter lists and is resolved mainly at compile time. Overriding replaces inherited behavior with a subclass implementation and is selected dynamically for virtual instance calls.

Use overloading for related operations with different inputs; use overriding when a subtype needs different behavior.

Example

text
add(int,int) -> overloading Dog.sound() -> overriding

Important Point

Static methods are hidden rather than overridden; private methods are not overridden because they are not inherited in the usual polymorphic sense.

Question 9: What is instanceof?

Ans

instanceof checks whether an object reference is compatible with a specified type.

It is useful before a potentially unsafe cast and can also use Java's pattern matching syntax in modern versions.

Example

java
if (obj instanceof String s) { System.out.println(s.length()); }

Important Point

The expression is false for null.

Question 10: What is covariant return type?

Ans

When overriding a method, Java allows the subclass method to return a subtype of the original method's return type.

This lets overriding methods return a more specific type without breaking polymorphism.

Example

java
class Animal { Animal copy() { return new Animal(); } } class Dog extends Animal { @Override Dog copy() { return new Dog(); } }

Important Point

Primitive return types cannot use covariant return rules.

Question 11: What is type casting?

Ans

Type casting converts a value or reference to another compatible type. Primitive casts can change numeric types; reference casts change the view of an object within its inheritance hierarchy.

It is useful when an API returns a broader type but a narrower type is known.

Example

java
double d = 10.8; int n = (int) d; // 10

Important Point

Narrowing primitive conversion can lose information.

Continue Your Preparation