In this tutorial, we will discuss Python Object Oriented Programming concept and learn its fundamentals.
What is OOP’s in Python?
As we know that python is a high-level programming language with multi-paradigm, here Object-Oriented Programming is one of those paradigms.
Object-oriented programming is a programming technique that most of the high-level programming language support in this we use class and object to make a program.
An Object has two characteristics:
Here attributes are the variable and behavior can be taken as the function of those variables.
Let’s understand it with an example:
Consider a Car as an Object
So, its color, brand name, considered as its attributes
Where the gear mechanism and speed would be considered as its behavior.
When we use the properties of one class in another class.
All the code is inside a Class block.
Operators performing different operations on different data types
Two Main Component of OPP’s
A class is a Keyword which is used to make the blueprint of an Object. A class has no existence until its object gets created.
#class body which includes its properties
class Car: car_colour = "blue" car_brand = "BMW"
The object of a class is also known as the instance of the class. When we create an object of the class the class come in existence, and with the help of object, we can access the properties of the class. A class has many objects.
Object creation syntax:
obj_name = Class_name()
Suppose Bob and Sam has same car, and if we represent it with OOP’s concept the car would be a class and Bob and Sam will be Objects.
class Car: car_colour = "blue" #Class attributes car_brand = "BMW" #class attributes bob = Car() # Bob is an object here sam = Car() # Sam is the another object of Car print("the brand of Bob's car is:",bob.car_brand) print("the Colour of Sam car is:", sam.car_colour)
the brand of Bob's car is: BMW the Colour of Sam car is: blue
Behind the code:
Here in the above code, we made two objects (sam and bob) of the same class (Car), and we have used the car attributes outside the Class block using its object and dot operator.
Functions inside a Class know as class methods. There are two types of Class methods user-defined methods and magical methods. The python magical methods also known as dunders these methods contain double underscores (__) before and after the method name.
class Car: def __init__(self,car_brand,car_colour): #Class megical method and class Constructor self.car_colour =car_colour self.car_brand = car_brand def car_details(self): #Class method print("The car brand is", self.car_brand, "and it",self.car_colour, "in colour") bob = Car("BMW", "Blue") sam = Car("Ferrari", "Red") bob.car_details() #Calling class method using object sam.car_details()
The car brand is BMW and it Blue in colour The car brand is Ferrari and it Red in colour
Python Object Oriented Programming Properties:
Inheritance is one of the most important properties of OPP’s concept and it used more often as compared to other OOP’s properties.
In inheritance, we can use the properties ( Attributes and Method) of one class in another. The main motto of inheritance is the reusability, code written in one class can be used in another.
Inheritance has two components: base class and derived class. The Base class also known as Parent class and the derived also know as child class. The derived class inherit the properties from the base class.
class base_class: # Base Class block class derived_class (base_class): # here derived class inherit base class #base class block
class Mammals: def __init__(self): print("Mammals Class has been created") def type(self): print("It is a Mammals") class Dog(Mammals): def __init__(self): print('dog class has been created') bob_pet = Dog() #When we create class object it invoke __init__ method bob_pet.type() # Here we call Mammals method using Dog object.
dog class has been created It is a Mammals
Encapsulation deal with data hiding from an Object. In python class, we use single or double underscore before the attributes name to tell the object that particular attribute is private for class only and object not supposed to access those attributes directly.
class make(): def __init__(self): self.item = 200 self.__item = 200 #Private member def itemsvalue(self): print("Total no. of __items",self.__item) print("Total no. of items",self.item) m= make() m.itemsvalue() print("-----------change the value of item and __item using object--------- ") m.item = 300 m.__item = 300 m.itemsvalue()
Total no. of __items 200 Total no. of items 200 -----------change the value of item and __item using object--------- Total no. of __items 200 Total no. of items 300
With polymorphism, we can have the same method name for different classes. Polymorphism applies on operators too for example if we use + operator between two integers it will add both integers, but if we use the same + operator between two string it will concatenate them.
class Cat: def eat(self): print("cat eat fish") def swim(self): print("Cat can't swim") class Dog: def eat(self): print("Dog Food, Meat") def swim(self): print("Dogs can swim") james_pet = Dog() sofi_pet = Cat() james_pet.swim() sofi_pet.swim()
Dogs can swim Cat can't swim
Some Advantages of OOP’s:
- OPP’s provide a modular way to complete the project
- It increases the code reusability
- Give more data security.