D
OOP & SOLIDBeginner10 XP4 min read

What are the four pillars of OOP and how does Dart implement them?

TL;DR: Encapsulation (private fields with _prefix), Inheritance (extends), Polymorphism (override + dynamic dispatch), Abstraction (abstract classes + interfaces via abstract class or interface keyword in Dart 3).

Full Answer

Encapsulation

Dart uses underscore prefix (_fieldName) for library-private access. There are no public/protected/private keywords — private means 'same library file'.

Inheritance

class Dog extends Animal — single inheritance only. Use mixins for multi-inheritance of behavior.

Polymorphism

A Dog can be stored as an Animal reference. Calling .speak() invokes Dog.speak() at runtime via dynamic dispatch.

Abstraction

abstract class defines a contract without implementation. In Dart 3, the interface keyword creates a pure interface (all methods must be implemented, no inheritance of implementation).

Code Examples

dartAll four pillars in Dart
Output
Rex barks!
Rex breathes

Common Mistakes

  • Thinking _ means private to the class — it's private to the library (file). A _field is accessible within the same .dart file by other classes
  • Confusing abstract class (can have implementation) with interface (implementation-free contract)

Interview Tip

💡

Point out Dart's unique privacy model: library-level (file), not class-level. Ask what happens when you put two classes in the same file — they can access each other's _ fields.

#encapsulation#inheritance#polymorphism#abstraction