Encapsulation is the OOP principle of bundling an object's data (attributes) and the methods that operate on that data into a single unit (a class), while restricting direct external access to the internal state. By declaring attributes as private and exposing them only through public getter and setter methods, encapsulation enforces data integrity and hides implementation details. This protects objects from unintended interference and simplifies maintenance since internal changes do not affect external code.
| Access Modifier | Within Class | Subclass (same pkg) | Other Package |
|---|---|---|---|
| private | Yes | No | No |
| default (none) | Yes | Yes | No |
| protected | Yes | Yes | No (subclass only) |
| public | Yes | Yes | Yes |
Wikimedia Commons, CC BY-SA
Object-Oriented Programming (OOP) is a programming paradigm that organises software design around data, called objects, rather than functions and logic. Each object encapsulates data (attributes) and behaviour (methods), and objects interact with one another to build complex systems. The four core principles of OOP — encapsulation, inheritance, polymorphism, and abstraction — promote code reusability, modularity, and maintainability.
Abstraction in OOP is the principle of exposing only the essential features of an object to the outside world while hiding the complex implementation details. It is achieved through abstract classes and interfaces, which define a contract specifying what methods a class must implement without dictating how they are implemented. Abstraction reduces complexity for the programmer using the class and allows internal implementations to change without affecting dependent code.
A class in object-oriented programming is a blueprint or template that defines the attributes (data fields) and methods (functions) common to all objects of that type. When a class is instantiated, it produces an object — a concrete instance that holds its own copy of the class's attributes. Classes enable code organisation by grouping related data and behaviour together, forming the foundational building blocks of OOP design.
From Latin "capsula" meaning "small container" or "case", a diminutive of "capsa" (box). In programming, the concept was central to Simula 67's modules and became explicit in Smalltalk's design philosophy. The term emphasises wrapping data as if in a protective capsule.