Object oriented programming in Python
1. Introduction
Object-oriented programming (OOP) is a programming paradigm that is widely used in software development. OOP involves creating classes and objects that encapsulate data and functionality, allowing developers to write more modular and reusable code.
2. Classes and Objects
At the heart of OOP is the concept of a class. A class is a blueprint or template for creating objects that share common characteristics and behaviors. A class defines the properties and methods that an object will have, but does not actually create the object itself.
Here is an example of a basic class in Python:
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 this example, we define a class called Person
that has two properties (name
and age
) and one method (greet
). The __init__
method is a special method that is called when an object is created from the class. It takes two parameters (name
and age
) and assigns them to the object's name
and age
properties.
The greet
method is a simple method that prints a greeting message to the console, using the object's name
and age
properties.
To create an object from the Person
class, we can use the following code:
person = Person("John", 30)
person.greet()
This code creates a new Person
object called person, with the name of "John" and an age of 30. It then calls the greet
method on the person
object, which prints a greeting message to the console.
3. Inheritance
Inheritance is a fundamental concept in object-oriented programming that allows developers to create new classes based on existing ones. Inheritance is used to model the relationship between classes that share common properties and methods.
Here is an example of a Student
class that inherits from the Person
class:
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def study(self):
print(f"{self.name} is studying.")
In this example, we define a Student
class that inherits from the Person
class. The Student
class has an additional property (student_id
) and a new method (study
). We use the super()
function to call the __init__
method of the Person
class, which sets the name
and age
properties of the Student
object.
To create a new Student
object, we can use the following code:
student = Student("Jane", 20, "123456")
student.greet()
student.study()
This code creates a new Student
object called student, with the name of "Jane", an age of 20, and a student_id of "123456". It calls the greet
method on the student
object, which prints a greeting message to the console, and the study
method, which prints a message indicating that the student is studying.
4. Encapsulation
Encapsulation is a fundamental principle of object-oriented programming that refers to the concept of hiding the internal workings of a class from the outside world. Encapsulation helps to prevent data from being modified or accessed accidentally and allows developers to control how the data is manipulated.
Python provides two types of access modifiers to enforce encapsulation: public and private. Public attributes and methods can be accessed from outside the class, while private attributes and methods can only be accessed from within the class itself.
To define a private attribute in Python, we use the double underscore (__
) prefix. For 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 this example, we define private attributes for name and age using the __
prefix. This means that these attributes can only be accessed from within the Person
class itself. To access these attributes from outside the class, we need to define getter and setter methods.
Here is an example of getter and setter methods for the Person
class:
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.")
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
In this example, we define getter and setter methods for name and age. The get_name
and get_age
methods return the value of the name
and age
attributes, respectively. The set_name
and set_age
methods set the value of the name and age attributes, respectively.
5. Polymorphism
Polymorphism is a fundamental concept in object-oriented programming that refers to the ability of objects of different classes to be used interchangeably. Polymorphism allows developers to write code that works with objects of different types, without having to write specific code for each type.
In Python, polymorphism is achieved through the use of duck typing. Duck typing is a programming concept that refers to the practice of determining an object's type based on its behavior, rather than its class. This means that Python does not require that objects be of a specific class to be used in a certain way.
Here is an example of polymorphism in Python:
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
def speak(animal):
print(animal.speak())
dog = Dog()
cat = Cat()
speak(dog) # Output: Woof!
speak(cat) # Output: Meow!
In this example, we define two classes (Dog
and Cat
) that have a speak
method. We also define a speak
function that takes an animal
parameter and calls its speak
method. We then create instances of the Dog
and Cat
classes, and pass them to the speak
function. Because both classes have a speak
method, they can be used interchangeably with the speak
function.
6. Conclusion
Object-oriented programming is a powerful paradigm that can help developers write more modular and reusable code. In Python, classes and objects are at the heart of OOP and can be used to encapsulate data and functionality, as well as to model the relationships between different types of objects.
Inheritance allows developers to create new classes based on existing ones, while encapsulation allows developers to control access to class attributes and methods, and polymorphism enables objects of different types to be used interchangeably.