Object Oriented Programming (Python)
Published: 2023-02-15 11:32:21

# 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
```{python}
class classname:
```
## 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.
```{python}
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
```
\__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.
```{python}
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
```
#### 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:
```{python}
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()
```
#### 3. Multiple Inheritance
Inherit multiple base classes in the child class is called Multiple Inheritance
```{python}
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
"""
```
## 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.
```{python}
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
```

## 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 
```{python}
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.
```
## 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

```{python}
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'
```
### Resources
- [Javatpoint](https://www.javatpoint.com/python-tutorial)