OOP in Java
Java is an object-oriented language with single class inheritance and multiple interface implementation. Most interview questions turn not on the definitions of "encapsulation, inheritance, polymorphism" but on one boundary: what the compiler decides at compile time versus what the JVM decides at runtime. Overloading is picked by argument types at compile time; overriding by the object's actual runtime type. Blur those two mechanisms and it is easy to explain @Override wrong and fail the static-method question.
The topic's second pillar is contracts and the relationships between types: how an abstract class (holds state, extended once) differs from an interface (a pure contract, implemented many times), how this and super address the current and parent object, what kinds of nested class exist, and why some hold a reference to the enclosing instance and others do not. The full map lives in the layers below.
Topic map
- Method Overriding — a subclass replaces a parent method with the same signature; the JVM picks the version by the object's actual type.
- Method Overloading — several methods share a name but differ in parameters; the compiler picks by argument types.
- Abstract Class vs Interface — an
abstractclass holds state and is extended once, aninterfaceis a contract implemented many times. - The super Keyword — reaching the parent's version of a method and invoking the superclass constructor as the first statement.
- The this Keyword — a reference to the current object, resolving a field shadowed by a parameter, and constructor chaining.
- Inner Classes — static nested, inner, local, and anonymous; who holds a reference to the outer object.
- Marker Interfaces — an empty tag interface like
SerializableandCloneable, checked at runtime. - Double-Brace Initialization — the
{{ ... }}anti-pattern: an anonymous subclass plus an initializer block and a hidden leak.
Common traps
| Mistake | Consequence |
|---|---|
| Assuming overloading is resolved at runtime like overriding | Overloading is fixed at compile time by the reference type, not the object type |
| Thinking a different return type alone overloads a method | It does not compile — only the parameter list overloads |
Expecting polymorphism from a subclass static method | It hides rather than overrides the parent; the call goes by reference type |
Putting super(...) or this(...) after another statement | Compile error — both must be the first statement |
| Assuming a static nested class sees the outer instance's fields | It holds no outer reference — only a non-static inner class does |
| Expecting a marker interface to declare methods | It is empty; a missing tag (Cloneable) throws at runtime |
| Using double-brace init for "brevity" | An extra class file plus a hidden this$0 reference to the outer object → a leak |
Interview relevance
OOP is the foundation of almost any Java interview, but the check is not the memorized "four principles" — it is your model of who decides the call, and when. A candidate who cleanly separates "the compiler picks overloading by the reference type" from "the JVM picks overriding by the object type" immediately sounds solid.
Typical checks:
- Overriding (same signature, runtime, polymorphism) versus overloading (different parameters, compile time).
- Why
private,static, andfinalmethods are not overridden, and thatstaticis merely hidden. - The difference between an
abstractclass and aninterface: state, number of inherited types,defaultmethods since Java 8. - The roles of
thisandsuper, and the "first statement" rule forthis(...)/super(...). - The kinds of nested class and who carries a reference to the outer object.
Common wrong answer: "a subclass static method overrides the parent's." That opens the discussion of method hiding: such a call is resolved by the reference type at compile time, so there is no polymorphism — unlike a true instance override.