Introduction of Object Oriented Programming

Introduction of Object Oriented Programming

What is an Object-Oriented Language?

An Object-Oriented Language (OOL) is a programming language that follows the principles of Object-Oriented Programming (OOP).

It organizes code using objects — just like how we think about real-world things!

What is Object-Oriented Programming (OOP)?

OOP is a method of programming where real-world concepts (like a person, car, or app) are represented as objects.

Each object has:

  • Data (properties) → Like color, name, size
  • Behavior (methods) → Like walk(), drive(), play()
Real-Life Example: Let’s say we’re coding about a Dog :
class Dog {
  String name;
  int age;

  void bark() {
    print("Woof! ");
  }
}

Dog is a class (the blueprint).

name and age are properties (data).

bark() is a method (action).

1. Class

Definition:A class is a blueprint or template for creating objects. It defines the properties (variables) and behaviors (functions/methods) that objects created from it will have.

class Car {
  String color = 'Red';
  void drive() {
    print('Car is moving ');
  }
}

2. Object

Definition: An object is an instance of a class. It’s like a real-world version made from the blueprint.

void main() {
  Car myCar = Car();
  print(myCar.color); 
  myCar.drive();
}

3. Inheritance

Definition: Inheritance allows one class to inherit properties and methods from another class. It promotes code reuse.

class Animal {
  void sound() {
    print('Animal makes a sound');
  }
}

class Dog extends Animal {
  void bark() {
    print('Dog barks');
  }
}

void main() {
  Dog dog = Dog();
  dog.sound(); // Inherited from Animal
  dog.bark();  // Dog's own method
}

4. Polymorphism

Definition: Polymorphism means "many forms." A method can behave differently based on the object that is calling it.

class Animal {
  void speak() {
    print('Animal speaks');
  }
}

class Cat extends Animal {
  @override
  void speak() {
    print('Cat meows ');
  }
}

class Dog extends Animal {
  @override
  void speak() {
    print('Dog barks ');
  }
}

void main() {
  Animal myAnimal1 = Cat();
  Animal myAnimal2 = Dog();

  myAnimal1.speak(); // Output: Cat meows
  myAnimal2.speak(); // Output: Dog barks
}
  

5. Encapsulation

Definition: Encapsulation means keeping the internal details hidden and exposing only what's necessary. It’s done using private variables and getters/setters.

class BankAccount {
  double _balance = 0; // private variable

  void deposit(double amount) {
    if (amount > 0) _balance += amount;
  }

  double get balance => _balance; // public getter
}

void main() {
  BankAccount acc = BankAccount();
  acc.deposit(500);
  print(acc.balance); // Output: 500
}
  

6. Abstraction

Definition:Abstraction means showing only the essential features and hiding the complex logic behind it.

abstract class Phone {
  void call(); // abstract method
  void message(); // abstract method
}

class SmartPhone extends Phone {
  @override
  void call() {
    print('Making a call ');
  }

  @override
  void message() {
    print('Sending a message ');
  }
}

void main() {
  Phone myPhone = SmartPhone();
  myPhone.call();    // Output: Making a call
  myPhone.message(); // Output: Sending a message
}
  

Popular Object-Oriented Languages

  • Java
  • Python
  • C++
  • C#
  • Dart (used in Flutter )
  • JavaScript (partially OOP)

Why Use Object-Oriented Languages?

  • Easy to manage large code
  • Reusable code (using classes)
  • Real-world modeling
  • Easier to test and debug
  • Good for teamwork and large projects

Summary

  • Object-Oriented Language organizes code using classes and objects.
  • It supports modularity, reusability, and real-world structure.
  • Key concepts: class, object, inheritance, polymorphism, encapsulation, abstraction.
أحدث أقدم