OOP kya hai? Real World Examples
Object-Oriented Programming (OOP) Foundation
Object-Oriented Programming (OOP) is a programming paradigm that uses Objects and Classes to model real-world concepts, structures, and processes.
OOP helps developers write code that is modular, reusable, and easy to maintain, especially in large enterprise applications.
What is a Class?
A Class is a blueprint, template, or schema for creating objects. It defines the state (data) and behavior (actions) that objects created from it will possess.
For example, a Car class is a blueprint that defines properties like model, color, and speed, and behaviors like accelerate() and brake().
What is an Object?
An Object is an instance of a Class. It is a concrete entity created based on the blueprint defined by the class.
If Car is the class, then:
- Alice's Red Tesla is an object.
- Bob's Blue Ford Mustang is another object.
Each object has its own unique state values, but shares the same behaviors defined by the class template.
+--------------------------------------+
| CLASS: Car | ◄─── Class (Blueprint)
| Properties: color, speed |
| Methods: accelerate() |
+------------------┬-------------------+
│
┌─────────┴─────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ OBJECT: Tesla │ │ OBJECT: Mustang │ ◄─── Objects (Instances)
│ color: Red │ │ color: Blue │
│ speed: 120 km/h │ │ speed: 80 km/h │
└─────────────────┘ └─────────────────┘
The Four Pillars of OOP
An OOP-compliant language like Dart supports four core concepts:
1. Encapsulation
Restricting direct access to some of an object's components. This is achieved by declaring fields private and exposing them through public getter and setter methods.
2. Inheritance
A mechanism where a new class (subclass) inherits properties and methods from an existing class (superclass), promoting code reuse.
3. Polymorphism
The ability of different classes to respond to the same method call in unique ways. For example, a Shape class might define an draw() method, but Circle and Square subclasses will implement that drawing logic differently.
4. Abstraction
Hiding complex implementation details and exposing only the essential features of an object. This is implemented using abstract classes and interfaces.
OOP in the Flutter Framework
Flutter uses OOP principles for almost everything:
Widgetis a class. When you writeText("Hello"), you are creating an instance (object) of theTextclass.- State Management uses classes and objects to store and notify UI elements of data changes.
- Custom Components are built by extending (inheriting from)
StatelessWidgetorStatefulWidget.
Summary
- Classes are templates; Objects are the concrete instances created from those templates.
- OOP makes code easier to map to real-world business models.
- Dart is a pure object-oriented language — even basic types like
intandboolare classes under the hood.