DEV Community

shiva yada
shiva yada

Posted on

Object Oriented Programming (Python)

Python Object Oriented Programming

Object-oriented programming is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.

Class:

A class is a collection of instance variables and methods that define a particular object. Class is object's template.

class is created like

class classname:
Enter fullscreen mode Exit fullscreen mode

Object:

A class instance with a defined set of properties is called an object. We can create as many objects as needed for a same class.

class student:
    def __init__(self,name,rollno):
        self.name=name
        self.rollno= rollno
st= student('shiva',54)
# st is a object calling student class constructor
Enter fullscreen mode Exit fullscreen mode

_init_ is constructor, is used to initialize the student class with attributes such as name,rollno.

Inheritance

  • Inheritance specifies that the child object acquires all the properties and behaviors of the parent object.
  • Using inheritance we can create a class which uses all the properties and behavior of another class.
  • The new class is know as a derived class or child class.
  • The one whose properties are acquired is known as a base class or parent class.
  • Provides the reuse of the code

Types of Inheritance

1. Single Inheritance

A child class acquires the properties and can access all the data from a parent class is called Single Inheritance.

class Laptop:
    def run(self):
        print("laptop working")
class Lenovo(Laptop):
     def work(self):
         print("lenovo working")
l= Lenovo()
l.work()
l.run()
# lenovo working
# laptop working
Enter fullscreen mode Exit fullscreen mode

2. Multi-Level Inheritance

A derived class inherits another derived class is called multi-level inheritance.

  • No limit on the no.of levels up syntax:
class Animal:
    def speck(self):
       print("animal speaking")
class Dog(Animal):
    def bark(self):
      print("dog barking")
class DogChild(Dog):
    def eat(self):
      print("eating bread")
d = DogChild()
d.bark()
d.speak()
Enter fullscreen mode Exit fullscreen mode

3. Multiple Inheritance

Inherit multiple base classes in the child class is called Multiple Inheritance

class Calculation:
  def Sum(self,a,b):
    return a+b
class Calculation2:
  def mul(self,a,b):
    return a*b
class Derived(Calculation, Calculation2):
   def Divide(self,a,b)
     return a/b
d= Derived()
print(d.Sum(20,30))
print(d.mul(10,20))
print(d.Divide(10,20))
""" 
60
200
0.5
"""
Enter fullscreen mode Exit fullscreen mode

Polymorphism:

Polymorphism means ' something that takes on multiple forms'.
A subclass can use a method from its super class as is or modify it as needed.

class sum2:
    def add(self,a,b):
        return a+b
class sum3:
    def add(self, a, b,c):
        return a+b+c
s= sum2()
s3=sum3()
two=s.add(2,3)
three= s3.add(2,3,4)
print(two)
print(three)
#5
#9
Enter fullscreen mode Exit fullscreen mode

Abstraction:

  • Abstraction isn't supported directly in Python. Calling a magic method, on the other hand, allows for abstraction.

  • If an abstract method is declared in a super class, sub-classes that inherit from the super class must have their own implementation of the method. It hides the unnecessary info from the user and reduce complexity and increase the efficiency of the program.

  • Using abstract classes and methods we can achieve abstraction.

-abc module to use the abstraction

from abc import ABC
class type_shape(ABC): 
  def area(self): 
    #abstract method
    pass
class Rectangle(type_shape):
  length = 6
  breadth = 4
  def area(self):
    return self.length * self.breadth
class Circle(type_shape):
  radius = 7
  def area(self):
    return 3.14 * self.radius * self.radius
r = Rectangle() 
c = Circle() 
print("Area of a rectangle:", r.area()) # call to 'area' method defined inside the class.
print("Area of a circle:", c.area()) # call to 'area' method defined inside the class.
Enter fullscreen mode Exit fullscreen mode

Encapsulation

The process of preventing clients from accessing certain properties,which can only be accessed through specific methods is called Encapsulation.

Attributes can be inaccessible attributes using two underscores to declare private characteristics.
example : __phonenumber

class student:
    def __init__(self,name,class):
        self.name= name
        self.class=class
        self.__phonenumber=232342342
s=student('shiva','8th')
print(s.name) # shiva
print(s.class) # 8th
print(s.__phonenumber)
 # Attributeerror : no attribute '__phonenumber'
Enter fullscreen mode Exit fullscreen mode

Resources

Top comments (0)