Python is an object-oriented programming (OOP) language, which means it follows the principles of object-oriented programming to structure code around objects and classes. This approach allows developers to create reusable code and model real-world entities.
1. Class: A class is a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data. For example, a Car class might have attributes like color and model and methods like start() and stop().
class ClassName:
attributes and methods
2. Object: An object is an instance of a class. It is the actual entity that exists in memory and has a state and behavior defined by its class.
class Car:
def __init__(self, color, model):
self.color = color
self.model = model
car1 = Car("Red", "Toyota")
3. Method: A method is a function that is associated with an object. Methods define the behaviors of the objects created from the class.
class Car:
def start(self):
print("Car is starting")
4. Inheritance:Inheritance allows one class to inherit the attributes and methods of another class. The class that inherits is called the child or derived class, and the class from which it inherits is called the parent or base class.
class Vehicle:
def __init__(self, brand):
self.brand = brand
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model
5. Polymorphism: Polymorphism allows methods to do different things based on the object it is acting upon. It allows the same method name to be used for different types.
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
def make_sound(animal):
print(animal.speak())
make_sound(Dog()) # Output: Woof!
make_sound(Cat()) # Output: Meow!
6. Encapsulation: Encapsulation involves bundling the data and the methods that operate on the data within one unit (class) and restricting access to some of the object's components.
class Car:
def __init__(self):
self.__speed = 0
def set_speed(self, speed):
self.__speed = speed
def get_speed(self):
return self.__speed
7. Data Abstraction: Abstraction means hiding the complex implementation details and showing only the essential features of an object. It is achieved using abstract classes and methods in Python.
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Woof!"
dog = Dog()
print(dog.sound())
| Index | Object-Oriented Programming (OOP) | Procedural Programming |
|---|---|---|
| 1 | OOP is a programming paradigm that organizes code into objects, which encapsulate data and behaviors. It is used to model real-world entities and their interactions. | Procedural programming is a paradigm that organizes code into procedures or functions that operate on data. It follows a step-by-step approach to solving problems. |
| 2 | It simplifies development and maintenance by structuring code into reusable and modular objects. | It can become challenging to maintain as projects grow larger due to the lack of modularity and encapsulation. |
| 3 | It models real-world phenomena effectively by representing entities as objects, making it easier to manage complex systems. | It does not inherently model real-world entities but focuses on sequences of instructions and functions to perform tasks. |
| 4 | It promotes data encapsulation and abstraction, enhancing security by restricting direct access to internal data. | It generally lacks built-in mechanisms for data encapsulation, making it less secure compared to OOP. |
| 5 | Popular OOP languages include C++, Java, Python, C#, and others. | Common procedural languages include C, Fortran, Pascal, and Visual Basic (VB). |