Python OOPs Concepts - Important Points
Python is a powerful object-oriented programming language that supports various programming paradigms, including procedural, functional, and object-oriented programming. In this write-up, we will focus on the object-oriented programming (OOP) concepts in Python.
Object-oriented programming is a programming paradigm that revolves around the concept of objects, which are instances of classes. A class is a blueprint or a template that defines the properties and behaviors of an object. An object is an instance of a class that can have its own set of properties and behaviors. OOP in Python has many advantages, including code reuse, encapsulation, and abstraction.
Classes and Objects: In Python, classes are created using the 'class' keyword, followed by the class name and a colon. Class properties are defined using instance variables, and class behaviors are defined using methods. An object is created by instantiating a class using the class name and parentheses.
Encapsulation: Encapsulation is the concept of wrapping up data and code into a single unit. In Python, encapsulation is achieved through access modifiers such as public, protected, and private. Public variables and methods can be accessed by anyone, protected variables and methods can only be accessed within the class and its subclasses, and private variables and methods can only be accessed within the class.
Inheritance: Inheritance is the concept of creating a new class from an existing class. The new class inherits the properties and behaviors of the existing class and can also add its own properties and behaviors. In Python, inheritance is achieved using the 'extends' keyword.
Polymorphism: Polymorphism is the concept of having multiple forms or behaviors for a single object. In Python, polymorphism is achieved through method overriding and method overloading. Method overriding is the concept of providing a different implementation of a method that is already defined in the parent class. Method overloading is the concept of defining multiple methods with the same name but different parameters.
Abstraction: Abstraction is the concept of hiding the implementation details and exposing only the essential features to the user. In Python, abstraction is achieved using abstract classes and interfaces. Abstract classes are classes that cannot be instantiated and must be subclassed. Interfaces are classes that define a set of methods that must be implemented by any class that implements the interface.
To better understand the OOP concepts in Python, let's take a look at some examples.
Example 1: Creating a Class and Object
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def print_info(self):
print("Name:", self.name)
print("Age:", self.age)
person1 = Person("John", 30)
person1.print_info()
In this example, we created a Person class that has two properties, name and age, and one behavior, print_info(), which prints out the person's name and age. We then instantiated an object person1 of the Person class with the name "John" and age 30, and called the print_info() method on it to print out the person's information.
Example 2: Inheritance
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
dog = Dog("Buddy")
cat = Cat("Luna")
dog.make_sound()
cat.make_sound()
In this example, we created an Animal class with a property name and a behavior make_sound() that is not implemented. We then created two subclasses, Dog and Cat, which inherit from the Animal class and implement their own make_sound() methods. We instantiated two objects, dog and cat, and called the make_sound() method on them to make them produce their respective sounds.
Example 3: Polymorphism
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
shapes = [Rectangle(4, 5), Circle(3)]
for shape in shapes:
print("Area:", shape.area())
In this example, we created a Shape class with an abstract area() method. We then created two subclasses, Rectangle and Circle, which inherit from the Shape class and implement their own area() methods. We then created a list of Shape objects containing one Rectangle and one Circle object, and iterated through the list to call the area() method on each object and print out the area.