Why OOP exists:
Procedural Programming (C, COBOL) organizes the code around functions that uses shared (global) data.
On the other hand OOP bundles data and behavior into objects. The unit of Procedural code is the Function, the unit of OOP is the Class.
Four principles:
- Encapsulation: data and methods are bundled together, and the interface is separated from the implementation. the data is hidden behind that interface.
- Inheritance: a class reuses the attribute and methods of another class (parent class).
- Polymorphism: many implementation sit behind one interface. The same call gives different behavior depending on the object. It relies on encapsulation and generalization.
- Abstraction: expose only the essential features and hid the complexity.
Core vocab:
- Class: a blueprint. Defining a class allocates no memory.
- Object: an instance of a class. Creating one allocates memory. an object has Identity, state, and behavior.
- Attribute: Holds a state.
- Operation: defines behavior. reads/changes states.
Types of Relationship:
- Association: One object knows about another (Citizen - License).
- Aggregation: ‘Part-Of’ relationship, parts can exist alone (Computer - CPU).
- Composition: ‘Part-Of’ relationship, parts owned by the whole (Circle - Point).
- Generalization/Specialization: ‘Kind-Of’ relationship.
- Realization: a class can implement an interface.
Types of Class:
- Abstract class: cannot be instantiated.
- Concrete class: Implements everything and can be instantiated
- Final/Leaf: cannot have a subclass