Object-oriented programming
Object-oriented programming
- OOP builds programs from objects — units that combine data and operations.
- Objects are instances of classes, and a few core ideas (the "four pillars") organise everything.
- It suits large systems, GUIs, simulations and games.
Objects and classes
- An object combines attributes (data) and methods (operations).
- A class is the blueprint; an object is an instance of it.
- A constructor is a special method run when an object is created, to set up its attributes.
An object combines:
An object bundles its data (attributes) with the operations (methods) that act on it.
A constructor is a special method that:
The constructor initialises a new object's attributes at creation time.
The four pillars
- Encapsulation — an object's data is hidden behind its methods; outside code uses the public methods, not the data directly (so internals can change safely).
- Inheritance — a subclass specialises a superclass, inheriting its members and adding/overriding them. Models "is-a" (a Manager is an Employee).
- Polymorphism — different objects respond to the same method call in their own way (every
ShapehasArea();CircleandRectangleeach implement it). - Abstraction — show a simple interface, hide the implementation.
Encapsulation means:
Encapsulation hides the data; outside code uses the public methods, so internals can change safely.
Inheritance models which relationship?
A subclass "is-a" specialised superclass, inheriting and extending its members.
Polymorphism allows:
The same call (e.g. Area()) behaves correctly for each type — the caller need not know the exact class.
Match each OOP pillar to its meaning.
Encapsulation = hiding; inheritance = is-a specialisation; polymorphism = same interface, varied behaviour.
You've got it
- an object = attributes (data) + methods (operations); a class is its blueprint
- a constructor runs at creation to set up the object
- four pillars: encapsulation (hide data), inheritance (is-a, subclass), polymorphism (same call, different behaviour), abstraction (simple interface)