What is OOP

Object-Oriented Programming

So far all our code lived in main and static methods. Real programs model things from the real world (students, products, orders, accounts). Object-oriented programming (OOP) groups the data and the behaviour of such a thing together.

  • A class is a blueprint or template: it says what data and actions a thing has.
  • An object is one real instance made from that blueprint, with its own data.
Class: Car (blueprint)               Objects (instances)
   +--------------------+       +-------------------+  +-------------------+
   | brand, color, speed| ----> | brand = "Tata"    |  | brand = "Maruti"  |
   | accelerate()       |       | color = "Red"     |  | color = "White"   |
   | brake()            |       | speed = 40        |  | speed = 0         |
   +--------------------+       +-------------------+  +-------------------+

The Four Pillars

PillarIdea (one line)Level
Encapsulationhide data inside the object, expose safe methodsmedium
Inheritancea class reuses another class (extends)medium
Polymorphismone interface, many behavioursmedium
Abstractionshow what an object does, hide howmedium

Note: This module gives you a solid first look. The medium level covers constructors, this, static vs instance, access modifiers and the four pillars in depth.