Object Oriented Programming (OOP)
Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to design and build applications. It is based on the concept of objects, which can contain data in the form of fields (attributes or properties), and code in the form of procedures (methods or functions).
Classes and Objects
A class is a blueprint for creating objects. It defines the properties and behaviors of the objects that will be created from it. An object is an instance of a class, and it can be used to access the properties and behaviors defined by the class.
# Example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old")
In the example above, we define a class called Person
with two attributes name
and age
, and a method greet
that prints a greeting message using the values of the attributes.
Creating Objects
You can create objects from a class using the class name followed by parentheses. You can then access the attributes and methods of the object using the dot operator.
# Example
person1 = Person("Alice", 25)
person1.greet() # Output: Hello, my name is Alice and I am 25 years old
In the example above, we create an object called person1
from the Person
class and call the greet
method to print a greeting message.
Constructors and Destructors
A constructor is a special method that is called when an object is created. It is used to initialize the attributes of the object. In Python, the constructor method is called __init__
.
In the example above, we define a constructor method that takes two parameters name
and age
and initializes the attributes of the object.
A destructor is a special method that is called when an object is destroyed. It is used to perform cleanup operations before the object is removed from memory. In Python, the destructor method is called __del__
.
In the example above, we define a destructor method that prints a message when the object is destroyed.
Inheritance
Inheritance is a mechanism that allows a class to inherit properties and behaviors from another class. The class that inherits from another class is called a subclass, and the class that is inherited from is called a superclass.
# Example
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
def study(self):
print(f"{self.name} is studying")
In the example above, we define a subclass called Student
that inherits from the Person
class. The Student
class has an additional attribute grade
and a method study
.
Method Overriding
Method overriding is a mechanism that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This allows you to customize the behavior of the method for the subclass.
# Example
class Student(Person):
def greet(self):
print(f"Hello, my name is {self.name} and I am a student")
In the example above, we define a subclass called Student
that overrides the greet
method of the Person
class to provide a different greeting message.
Encapsulation
Encapsulation is a mechanism that restricts direct access to some of the object's components. It prevents the accidental modification of data and allows the object to control its state and maintain its integrity.
In Python, encapsulation is achieved by using private attributes and methods, which are denoted by a leading double underscore __
.
# Example
class Person:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
In the example above, we define a class called Person
with private attributes __name
and __age
, and methods get_name
and set_name
to access and modify the attributes.
Polymorphism
Polymorphism is a mechanism that allows objects of different classes to be treated as objects of a common superclass. This allows you to write code that can work with objects of different types and classes.
# Example
class Dog:
def speak(self):
print("Woof!")
class Cat:
def speak(self):
print("Meow!")
def make_sound(animal):
animal.speak()
dog = Dog()
cat = Cat()
make_sound(dog) # Output: Woof!
make_sound(cat) # Output: Meow!
In the example above, we define two classes Dog
and Cat
with a method speak
, and a function make_sound
that takes an object of any class with a speak
method and calls the method.
Abstract Classes and Interfaces
An abstract class is a class that cannot be instantiated and is used to define methods that must be implemented by its subclasses. An interface is a collection of abstract methods that define a contract for the behavior of a class.
In Python, abstract classes and interfaces can be defined using the abc
module.
# Example
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
def perimeter(self):
return 2 * 3.14 * self.radius
In the example above, we define an abstract class Shape
with abstract methods area
and perimeter
, and a subclass Circle
that implements the methods.