Exploring the Basics of Object-Oriented Programming in Python: Part 1

Exploring the Basics of Object-Oriented Programming in Python: Part 1

Object-Oriented Programming (OOP) is a popular programming paradigm that has been widely used in the development of software applications. OOP enables us to organize code into reusable and self-contained objects that have properties and methods to perform specific tasks. Python is one of the most widely used programming languages that supports OOP. In this article, we will discuss the basics of OOP in Python, including classes, objects, inheritance, polymorphism, encapsulation, and abstraction.

Classes

In OOP, a class is a blueprint or a template that defines the properties and methods of an object. In Python, we can define a class using the class keyword followed by the name of the class. For example, let's create a simple class called Person:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def say_hello(self):
        print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")        

In the above code, we define a class called Person that has two properties (name and age) and one method (say_hello). The __init__ method is a special method in Python that is called when an object is instantiated from the class. The self parameter refers to the object being created.

Objects

An object is an instance of a class. We can create an object of a class by calling the class name followed by parentheses. For example, let’s create an object of the Person class

person1 = Person("John", 30)        

In the above code, we create an object called person1 of the Person class with the name "John" and age 30.

No alt text provided for this image
Photo by JustDataPlease on Unsplash

Inheritance

Inheritance is a mechanism in OOP that allows us to define a new class based on an existing class. The new class is called the derived class, and the existing class is called the base class. The derived class inherits all the properties and methods of the base class, and we can add new properties and methods to the derived class as well. For example, let’s create a new class called Student that inherits from the Person class:

class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self.grade = grade
    
    def say_hello(self):
        print("Hello, my name is " + self.name + " and I am a student in grade " + str(self.grade) + ".")        

In the above code, we define a new class called Student that inherits from the Person class. The super() function is used to call the __init__ method of the base class, and we add a new property (grade) and a new method (say_hello) to the derived class.

Polymorphism

Polymorphism is a concept in OOP that allows us to use the same method or function with different types of objects. For example, let’s create a function called introduce that takes an object of the Person class as a parameter and calls the say_hello method:

def introduce(person):
    person.say_hello()        

In the above code, we define a function called introduce that takes an object of the Person class as a parameter and calls the say_hello method. We can use this function with objects of both the Person

Hello Muhammad... We post 100's of job opportunities for developers daily here. Candidates can talk to HRs directly. Feel free to share it with your network. Visit this link - https://jobs.hulkhire.com And start applying.. Will be happy to address your concerns, if any

To view or add a comment, sign in

More articles by Muhammad Abu Bakar

Others also viewed

Explore content categories