OOP is a way of structuring programs (programming paradigm) around objects. An object is a self-contained entity that groups data (attributes or properties) with the code that works on that data (methods). Imagine an object like a blueprint for a house - it specifies the rooms (data) and how they're connected (methods). You can then use that blueprint to create multiple houses (objects) with the same structure.
Python, being a multi-paradigm language, supports OOP alongside imperative and functional programming.
Classes and Objects
Class: A blueprint for creating objects. A class defines a set of attributes and methods that the objects created from the class can have.
Object: An instance of a class. Objects have states and behaviors, represented by their attributes and methods, respectively.
Attributes and Methods
Attributes: Variables that belong to an object or class. They represent the state or data of the objects.
Methods: Functions that belong to an object or class. They represent the behavior or functionality of the objects.
Encapsulation
Encapsulation is the bundling of data (attributes) and methods that act on the data into a single unit, or class. It restricts direct access to some of an object's components, which can prevent the accidental modification of data. In Python, this is achieved through the use of private attributes and methods, which are denoted by a prefix of two underscores __
.
Inheritance
Inheritance allows a class to inherit attributes and methods from another class. The class that is inherited from is called the parent or superclass, and the class that inherits is called the child or subclass. This is used to reuse code, reduce complexity, and establish relationships between classes.
Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. This means that the same method name can be used for different types. In Python, polymorphism is commonly achieved through method overriding (where a method in a subclass has the same name as a method in its superclass but does a different thing) and duck typing (where the object's suitability is determined at runtime by the presence of certain methods and properties, rather than the type of the object itself).
Abstraction
Abstraction means hiding the complex reality while exposing only the necessary parts. It is the concept of hiding the internal implementation and only showing the features of the object, so that the changes inside the object do not affect other parts of the program. In Python, this can be achieved through the use of abstract classes and methods. Abstract classes are classes that cannot be instantiated and are designed to be subclassed. They can define abstract methods which must be implemented by any subclass.
Here's a simple example of a class in Python that demonstrates some of these concepts:
class Animal:
def __init__(self, name): # Constructor method
self.name = name # Attribute
def speak(self): # Method
raise NotImplementedError("Subclass must implement abstract method")
class Dog(Animal): # Inherits from Animal
def speak(self): # Implementation of the abstract method
return f"{self.name} says Woof!"
class Cat(Animal): # Inherits from Animal
def speak(self): # Implementation of the abstract method
return f"{self.name} says Meow!"
# Create instances of Dog and Cat
dog = Dog("Buddy")
cat = Cat("Whiskers")
# Polymorphism: different classes, same method name, but different behaviors
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
This example illustrates classes (Animal
, Dog
, Cat
), inheritance (Dog
and Cat
inherit from Animal
), polymorphism (both Dog
and Cat
define their own version of the speak
method), and encapsulation (use of methods and attributes within classes). Abstraction is also touched upon with the Animal
class expecting subclasses to implement the speak
method.