Java Documentation

Chapter 02: Object Oriented Principles

An overview of the core object-oriented principles in Java: inheritance, polymorphism, abstraction, and encapsulation, and how they shape class design.

Diagram of the four main object-oriented principles in Java: inheritance, polymorphism, abstraction, and encapsulation
generated by NotebookLM

Why Object-Oriented Principles Matter

Object-Oriented Programming (OOP) organizes software around objects rather than just functions. In Java, these principles help us to design reusable, maintainable, and secure applications by modeling real-world entities and relationships.

The four main pillars of OOP are inheritance, polymorphism, abstraction, and encapsulation. The rest of this chapter explores each of them in detail with examples.

Inheritance

Inheritance allows one class (the subclass) to reuse and extend the behavior of another class (the superclass). It represents an"is-a" relationship between two types.

  • Promotes code reuse and reduces duplication.
  • Enables hierarchical class structures.
  • Implemented in Java using the extends keyword.

Polymorphism

Polymorphism means "many forms". It allows the same operation to behave differently depending on the actual object that receives the call.

  • Compile-time polymorphism via method overloading.
  • Runtime polymorphism via method overriding and dynamic binding.
  • Helps design flexible APIs that work with base types.

Abstraction

Abstraction focuses on exposing only essential features of an object while hiding unnecessary details. In Java, this is implemented using abstract classes and interfaces.

  • Separates what an object does from how it does it.
  • Encourages clean, high-level designs.
  • Supports plug-and-play implementations via interfaces.

Encapsulation

Encapsulation is the idea of bundling data and methods that operate on that data into a single unit (a class), and restricting direct access to internal details.

  • Achieved using access modifiers like private and getters/setters.
  • Protects invariants and improves security.
  • Makes it easier to change internal implementation later.