Independent reviewer · not a CSC or DICT publicationSign in to practice

Study notes · OOP · DICT

Object-Oriented Programming

OOP organises programs around objects that hold state and behaviour. The four pillars — encapsulation, abstraction, inheritance and polymorphism — show up both as definitions and in code-reading items.

2023 FAQ (copy)Scope: Encapsulation, inheritance, polymorphism, abstraction.

§1 The four pillars

PillarMeaningIn code
EncapsulationHide internal state behind methodsprivate fields, public getters/setters
AbstractionExpose what an object does, not howabstract classes, interfaces
InheritanceReuse and extend a base type ('is-a')class Dog extends Animal
PolymorphismOne interface, many implementationsoverriding; overloading

§2 Terms that are often confused

§3 Worked examples

Example 1

Shape s = new Circle(2); s.area(); — which area() runs if Circle overrides it?

  1. The variable's type is Shape, but the object is a Circle.
  2. Overridden methods dispatch on the runtime type.

Answer Circle's area()

! Common traps

  • Thinking a private field is inherited as accessible — subclasses cannot access it directly.