Java is built around classes and objects, and OOP questions are guaranteed. You need the four pillars — encapsulation, inheritance, polymorphism, abstraction — in concrete Java terms, plus the distinctions that trip people up: method overloading vs overriding, and interface vs abstract class.
Overloading vs overriding
Overloading: same method name, different parameter lists, in the same class — resolved at compile time (static polymorphism). Overriding: a subclass redefines an inherited method with the same signature — resolved at runtime based on the actual object (dynamic polymorphism).
Inheritance and overriding
class Animal { String sound() { return "..."; } }
class Dog extends Animal {
@Override String sound() { return "Woof"; } // overriding
}
Animal a = new Dog();
System.out.println(a.sound()); // "Woof" — runtime picks Dog's method⚡ The edge
- Overloading is compile-time (same name, different parameters); overriding is runtime (subclass redefines the same signature). Calling a.sound() on an Animal reference holding a Dog runs Dog's version — dynamic dispatch.
- Interface vs abstract class: an interface is a pure contract a class implements (a class can implement many); an abstract class can have partial implementation and state, but a class extends only one. Java allows multiple interfaces but single class inheritance.
Worked example
What's the difference between method overloading and overriding?
- Overloading: several methods with the same name but different parameters in one class; the compiler picks which to call (compile-time).
- Overriding: a subclass provides a new body for an inherited method with the same signature; the JVM picks based on the actual object (runtime).
- Overloading is static polymorphism; overriding is dynamic polymorphism.
Answer: Overloading = same name, different params, chosen at compile time; overriding = subclass redefines the same method, chosen at runtime.
Worked example
When do you use an interface vs an abstract class?
- Use an interface to define a contract (what a class can do) that many unrelated classes can implement, and to get multiple inheritance of type.
- Use an abstract class when you want to share common code/state among closely related subclasses, with some methods left abstract.
- A class implements many interfaces but extends only one (abstract) class.
Answer: Interface for a contract many classes implement (multiple allowed); abstract class to share code/state among related subclasses (one only).
⚠ Watch out
- Overloading vs overriding is the #1 OOP mix-up — different params (compile-time) vs same signature in a subclass (runtime).
- Java has no multiple inheritance of classes (diamond problem) — but a class can implement multiple interfaces.
- final methods can't be overridden, static methods are hidden not overridden, and private methods aren't inherited.