OE-3.2 Inheritance, Interfaces, Packages & Strings

Standard Java inheritance and packaging, as in the NPTEL IIT Kharagpur course — written September 2026

What this is and why it exists

One mechanism in this unit is the one worth being able to explain out loud in an interview: dynamic method dispatch. It is what makes polymorphism real at run time rather than a diagram.

One distinction is where both the exam and the interview probe: overloading against overriding. Same name with different parameters, against the same signature in a different class.

And one practical lesson closes it. Strings in Java cannot be changed, so building one in a loop makes garbage. That is not trivia; it is a real performance defect people ship.

The vocabulary

  • Superclass — the class being extended.
  • Subclass — the class doing the extending.
  • `super` — a reference to the superclass part of this object.
  • Overriding — replacing an inherited method with the same signature.
  • Dynamic dispatch — choosing the method at run time from the object's actual class.
  • Abstract class — one that cannot be instantiated and may leave methods unimplemented.
  • Interface — a set of method signatures a class promises to provide.
  • Package — a named grouping of classes.
  • Immutable — cannot be changed after creation.
  • Wrapper class — an object holding a primitive value.

The mental model

A subclass takes on its superclass's members, subject to access rules, and super is how it reaches the superclass part of itself. That much is bookkeeping.

Dynamic dispatch is not bookkeeping. Call a method through a superclass reference, and the method that runs is chosen at run time from the object's actual class. Not from the type of the reference. That single sentence is what makes polymorphism work. It is why a list of a general type can hold elements that each behave as their own specific kind.

Keep overloading and overriding separated. Overloading is several methods in one class sharing a name with different parameters — resolved at compile time. Overriding is a subclass replacing a method with an identical signature — resolved at run time. Say those two definitions aloud until the difference is automatic, because that is exactly where you will be probed.

Abstract classes cannot be instantiated and may leave methods for subclasses to supply. final on a class blocks extension entirely. And every class descends from one root class, which is why every object has a small set of methods you never wrote.

Packages group classes and give them a namespace, with an import mechanism and a search path. Interfaces state what a class promises without saying how, which is the abstraction idea from the previous topic given a keyword.

Then strings. Strings are immutable. Every change produces a new one. So concatenating in a loop creates a fresh string each time round, and the discarded ones pile up. The mutable alternative exists precisely for that case, and knowing when to reach for it is the practical content of this section. Wrapper classes are the other conversion story: an object holding a primitive, so primitives can live in structures that only hold objects.

What you should now be able to explain or do

Build a class hierarchy with correct member access, and use super. Explain dynamic method dispatch out loud, in one or two sentences. State the difference between overloading and overriding, and when each is resolved. Say what an abstract class offers and what final blocks. Organise classes into packages and import across them. Define and implement an interface. Say why building a string in a loop is a defect, and what to use instead.

Check yourself

Choosing the method at run time from the object's actual class rather than from the type of the reference holding it.

Overloading is one name with different parameters in a class, resolved at compile time. Overriding replaces an inherited method with the same signature, resolved at run time.

Define shared behaviour while leaving some methods for subclasses to supply. It cannot itself be instantiated.

Strings cannot be changed, so each concatenation creates a new one and discards the old. The mutable alternative exists for this case.

Holding a primitive as an object, so primitive values can be stored in structures that accept only objects.

Go deeper

Back to Inheritance, Interfaces, Packages & Strings: work through the checklist