OOP stands for Object-Oriented Programming. It is a way of writing code that uses "objects" — just like real things — to make programming easier and organized. Think of it like playing with LEGO blocks, where each block (object) has its own shape and function, and you can build cool stuff by combining them!
Full Forms & Simple Meanings:
class Animal {
String name = 'Dog';
void speak() {
print('Woof!');
}
}
void main() {
var pet = Animal();
print(pet.name);
pet.speak();
}
Explanation: We created a class called Animal. Then we made an object called pet using that class. The object can talk (run the method) and has a name!
class Animal {
void sound() {
print('Animal makes a sound');
}
}
class Dog extends Animal {
void bark() {
print('Dog barks!');
}
}
void main() {
var dog = Dog();
dog.sound();
dog.bark();
}
Explanation: Dog gets features of Animal. This is called inheritance — like kids inheriting traits from parents.
class BankAccount {
double _balance = 0; // private variable
void deposit(double amount) {
_balance += amount;
}
double get balance => _balance; // getter
}
void main() {
var account = BankAccount();
account.deposit(100);
print(account.balance);
}
Explanation: Data is hidden inside the class and only accessed through functions. This is called encapsulation.
class Animal {
void speak() {
print("Animal sound");
}
}
class Cat extends Animal {
@override
void speak() {
print("Meow");
}
}
void main() {
Animal a = Cat();
a.speak();
}
Explanation: speak() behaves differently based on the object. This is polymorphism — same method, different actions.
abstract class Shape {
void draw();
}
class Circle extends Shape {
void draw() {
print("Drawing Circle");
}
}
void main() {
Shape shape = Circle();
shape.draw();
}
Explanation: Abstract class hides complex code and lets subclasses fill in the details. Just like giving someone a sketch and they color it in.