World 9 of 26 · Intermediate

🧬 Inheritance & Polymorphism

18 Java concepts, each explained step by step with real code, a common trap and practice questions.

  1. extends & is-aA subclass inherits non-private members; single class inheritance.
  2. supersuper.method() and super(...) constructor calls.
  3. Constructor order in hierarchiesParent constructor runs before child; implicit super().
  4. Method overridingSame signature, covariant return, cannot reduce visibility or widen checked exceptions.
  5. @OverrideLets the compiler catch accidental overloads and typos.
  6. Dynamic dispatchThe runtime object type decides which overridden method runs.
  7. Overloading vs overridingOverloads are chosen at compile time by declared types; overrides at runtime.
  8. Upcasting & downcastingImplicit upcasts, explicit downcasts and ClassCastException.
  9. instanceof & pattern matchingif (o instanceof String s) binds a typed variable.
  10. Field hiding & static method hidingFields and static methods are not polymorphic.
  11. final classes & methodsPreventing extension and overriding.
  12. Abstract classesCannot be instantiated; may mix abstract and concrete methods and hold state.
  13. The Object classRoot of all classes: equals, hashCode, toString, getClass, clone, wait/notify.
  14. equals() contractReflexive, symmetric, transitive, consistent, x.equals(null) is false.
  15. hashCode() contractEqual objects must have equal hash codes; override both together.
  16. Composition over inheritancehas-a vs is-a, fragile base class problem.
  17. Liskov SubstitutionSubtypes must be usable wherever the parent is expected.
  18. Calling overridable methods in constructorsSubclass fields are not yet initialized — a classic trap.