1719853367

Fundamentals of Object-Oriented Programming (OOP)


Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" and their interactions to design and program software applications. Objects are instances of classes, which can contain data (attributes) and methods (functions or procedures) that operate on that data. ## <br>Basic principles of OOP --- **Class**: A class is a description of a set of objects with the same types of attributes and methods. It defines a data type that encapsulates data and methods that operate on that data. ```py class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year ``` **Object**: An object is an instance of a class. It represents an entity in the real world with attributes and behaviors. ```py my_car = Car('Toyota', 'Corolla', 2021) ``` **Encapsulation**: Encapsulation is the principle of grouping data (attributes) and methods that operate on this data into a single unit or class. It also involves restricting access to certain components to protect the integrity of the object. ```py class Car: def __init__(self, make, model, year): self.__make = make self.__model = model self.__year = year def get_details(self): return f'{self.__make} {self.__model}, {self.__year}' ``` **Inheritance**: Inheritance allows a class to derive from another class, inheriting its attributes and methods. It facilitates code reuse and the creation of class hierarchies. ```py class Vehicle: def __init__(self, type): self.type = type def move(self): print(f'The {self.type} is moving') class Car(Vehicle): def __init__(self, make, model, year): super().__init__('car') self.make = make self.model = model self.year = year def get_details(self): return f'{self.make} {self.model}, {self.year}' ``` **Polymorphism**: Polymorphism allows methods or functions to treat objects from different classes in a uniform way. In OOP, polymorphism is often achieved through overridden methods and interfaces. ```py class Animal: def make_sound(self): pass class Dog(Animal): def make_sound(self): return 'Bark' class Cat(Animal): def make_sound(self): return 'Meow' def make_animal_sound(animal): print(animal.make_sound()) make_animal_sound(Dog()) make_animal_sound(Cat()) ``` ## <br>Examples in Different Languages --- **<span style='color: #56ac31'>Python</span>** ```py class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): return f'Hello, my name is {self.name} and I am {self.age} years old.' ``` **<span style='color: #56ac31'>Php</span>** ```php class Person { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function greet() { return "Hello, my name is $this->name and I am $this->age years old."; } } ``` **<span style='color: #56ac31'>C++</span>** ```cpp class Person { private: std::string name; int age; public: Person(std::string name, int age) : name(name), age(age) {} std::string greet() { return "Hello, my name is " + name + " and I am " + std::to_string(age) + " years old."; } }; ``` **<span style='color: #56ac31'>Lua</span>** ```lua Person = {} Person.__index = Person function Person:new(name, age) local obj = {name = name, age = age} setmetatable(obj, Person) return obj end function Person:greet() return "Hello, my name is " .. self.name .. " and I am " .. self.age .. " years old." end local person = Person:new("John", 30) print(person:greet()) ``` Object-Oriented Programming helps organize and structure code, making it more modular, reusable, and maintainable. Understanding basic concepts like class, object, encapsulation, inheritance, and polymorphism is essential for effectively applying this paradigm in different programming languages.

(0) Comments