STUDYECRAT Java : OOP - POLYMORPHISM
60s
Java OOP - POLYMORPHISM
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★ Java Polymorphism: Key Interview Points
1. Runtime Polymorphism (Method Overriding)
class Animal {
void sound() { System.out.println("Generic animal sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark!"); }
}
// JVM decides method call at runtime based on object type
void sound() { System.out.println("Generic animal sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark!"); }
}
// JVM decides method call at runtime based on object type
- Tip: Always use @Override annotation to prevent accidental method signature mismatches
- Real Use: Used in frameworks like Spring where dependency injection resolves implementations at runtime
2. Compile-time Polymorphism (Method Overloading)
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
String add(String a, String b) { return a.concat(b); }
}
// Method resolution happens at compile time
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
String add(String a, String b) { return a.concat(b); }
}
// Method resolution happens at compile time
- Tip: Overloaded methods should perform similar operations to avoid confusion
- Real Use: Common in utility classes like Math with abs() methods for different types
3. Interface Polymorphism
interface Payment {
void processPayment(double amount);
}
class CreditCard implements Payment {
public void processPayment(double amt) {
System.out.println("Processing card: $" + amt);
}
}
// Can hold any implementing class instance
void processPayment(double amount);
}
class CreditCard implements Payment {
public void processPayment(double amt) {
System.out.println("Processing card: $" + amt);
}
}
// Can hold any implementing class instance
- Tip: Use interfaces for dependency injection to enable easy testing/mocking
- Real Use: Payment gateways where implementation can vary (PayPal, Stripe, etc.)
4. Covariant Return Types
class Parent {
Parent get() { return this; }
}
class Child extends Parent {
@Override
Child get() { return this; }
}
// Allowed since Java 5 - subtype can be returned
Parent get() { return this; }
}
class Child extends Parent {
@Override
Child get() { return this; }
}
// Allowed since Java 5 - subtype can be returned
- Tip: Useful in builder pattern implementations where subclass methods return 'this'
- Real Use: Cloneable implementations often use covariant returns for type safety
5. Polymorphic Collections
List<Shape> shapes = new ArrayList<>();
shapes.add(new Circle());
shapes.add(new Rectangle());
for(Shape s : shapes) {
s.draw(); // Calls appropriate implementation
}
// Collection holds different subclass instances
shapes.add(new Circle());
shapes.add(new Rectangle());
for(Shape s : shapes) {
s.draw(); // Calls appropriate implementation
}
// Collection holds different subclass instances
- Tip: Use generics (List<ParentType>) for type safety in polymorphic collections
- Real Use: GUI frameworks storing different UI components in a single collection
💡 Pro Interview Tip
When asked about polymorphism, always differentiate between compile-time (overloading) and runtime (overriding) polymorphism. Interviewers often test this fundamental understanding. For bonus points, mention how polymorphism enables the Open/Closed Principle in SOLID design.