/    /  Python OOPS

Python OOPS

In this tutorial, we will learn python in detail.

 

Python is an object-oriented language since its beginning which allows us to develop applications using an Object-Oriented approach. In Python, it is easy to create and use classes and objects.

OOP’s uses the concept of objects to represent the data and methods. This approach used for creating neat and reusable code instead of a redundant one. The program is separated into self-contained objects or some mini-programs. Every Individual object signifies a different part of the application having its own logic and data to communicate within themselves.

 

Difference between Object-Oriented and Procedural Oriented Programming

 

Object-Oriented Programming (OOP)

Procedural-Oriented Programming (Pop)

It is a bottom-up approach

It is a top-down approach

Program is divided into objects

Program is divided into functions

Makes use of Access modifiers

‘public’, private’, protected’

Doesn’t use Access modifiers

It is more secure

It is less secure

Object can move freely within member functions

Data can move freely from function to function within programs

It supports inheritance

It does not support inheritance

 

 

Major principles of object-oriented programming system are listed below.

  • Class
  • Object
  • Method
  • Inheritance
  • Polymorphism
  • Data Abstraction
  • Encapsulation

 

Classes

 

Classes are used to create user-defined data structures and they define functions called methods.

Class is a blueprint of objects defining the common attributes and behavior. It doesn’t actually contain any data. Object that is built from a class is called as instance and it contains real data.

Every class definition start with the class keyword, which is followed by the name of the class and a colon. Any line that is written with indentation below the class definition is considered part of the class’s body.

 

Example:

 

class Dog:
   def __init__(self, name, age):
       self.name = name
        self.age = age

 

In the above example, the Dog class is defined with ._init_() method. You can give .__init__() any number of parameters, but the first parameter should always be a variable called self.

 

Objects

 

Object is an entity that has behavior and state. It is an instance of class. When we define a class, it has to create an object to allocate the memory.

 

Example:

 

class Dog:

   def __init__(self, name, age):
       self.name = name
       self.age = age

   def display(self):
       print(self.name, self.age)

buddy = Dog("Buddy", 9)
buddy.display()

 

Output:

 

Buddy 9

 

In the above code, you created a new Dog object and assigned them to the variable buddy. You can create any number of objects for the same class and they represent two different objects in memory.

 

Method

 

Any object type may have methods. Methods in objects are functions that belong to the object.

 

Example:

 

class Person:
def __init__(self, name, age):
   self.name = name
   self.age = age

def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)

p1.myfunc()

 

Output:

 

John 36

 

Inheritance

In programming inheritance means acquiring or transferring properties and behavior of parent class to child class without any modification. The new class is the derived class which is derived from parent class.

There are different types of inheritances, they are single inheritance, multilevel inheritance, hierarchical inheritance, multiple inheritance.

 

Example:

 

class employee1()://This is a parent class
   def __init__(self, name, age, salary): 
       self.name = name
       self.age = age
        self.salary = salary


class childemployee(employee1)://This is a child class
    def __init__(self, name, age, salary,id):
        self.name = name
       self.age = age
       self.salary = salary
        self.id = id

emp1 = employee1('harsha',22,1000)

print(emp1.age)

 

 

Output:

 

22

 

Above is an example for single inheritance.

 

Polymorphism

 

Polymorphism consists of two words “poly” and “morphs”. Poly means many, and morph means shape. By polymorphism, it can be understood that one task can be performed in different ways. In programming polymorphism means same function being used for different types.

 

Example polymorphism with class methods:

 

class India():

   def capital(self):
        print("New Delhi is the capital of India.")

   def language(self):
        print("Hindi is the most widely spoken language of India.")

   def type(self):
        print("India is a developing country.")


class USA():

   def capital(self):
        print("Washington, D.C. is the capital of USA.")

   def language(self):
        print("English is the primary language of USA.")

   def type(self):
        print("USA is a developed country.")


obj_ind = India()
obj_usa = USA()

for country in (obj_ind, obj_usa):
   country.capital()
   country.language()
    country.type()

 

Output:

 

New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

 

You can also implement polymorphism with inheritance and also Polymorphism with a Function and objects:

 

 

Encapsulation

 

Encapsulation is one of the essential concepts in object-oriented programming (OOP). It defines the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and hence prevent the accidental modification of data. 

 

Data Abstraction

 

Data abstraction and encapsulation both synonyms because data abstraction is achieved through encapsulation. Abstraction shows only functionalities and hides internal details.

 

Example:

 

from abc import ABC,abstractmethod

class employee(ABC):

   def emp_id(self,id,name,age,salary):    //Abstraction
        pass


class childemployee1(employee):

   def emp_id(self,id):
        print("emp_id is 12345")

emp1 = childemployee1()

emp1.emp_id(id)

Output:

 

emp_id is 12345