0% found this document useful (0 votes)
138 views8 pages

Python OOPs Concepts Class and Object and Inhertance Abstraction

The document discusses key concepts of object-oriented programming in Python including classes, objects, methods, inheritance, polymorphism, encapsulation, and abstraction; it provides examples of creating classes and objects in Python, inheriting from parent classes, overriding methods, and hiding attributes to achieve abstraction. Major OOP concepts like inheritance, polymorphism, and encapsulation are demonstrated through examples in Python.

Uploaded by

Sumit Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
138 views8 pages

Python OOPs Concepts Class and Object and Inhertance Abstraction

The document discusses key concepts of object-oriented programming in Python including classes, objects, methods, inheritance, polymorphism, encapsulation, and abstraction; it provides examples of creating classes and objects in Python, inheriting from parent classes, overriding methods, and hiding attributes to achieve abstraction. Major OOP concepts like inheritance, polymorphism, and encapsulation are demonstrated through examples in Python.

Uploaded by

Sumit Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Like Python OOPs Concepts other general purpose languages,

python is also an object-oriented language since its beginning. Python is an object-


oriented programming language. It allows us to develop applications using an Object
Oriented approach. In Python, we can easily create and use classes and objects.

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

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

Object
The object is an entity that has state and behavior. It may be any real-world object like
the mouse, keyboard, chair, table, pen, etc.

Syntax
class ClassName:   
       <statement-1>   
      .   
       .    
      <statement-N>   

Method` ``
The method is a function that is associated with an object. In Python, a method is not
unique to class instances. Any object type can have methods.

Inheritance
Inheritance is the most important aspect of object-oriented programming which
simulates the real world concept of inheritance. It specifies that the child object acquires
all the properties and behaviors of the parent object.

By using inheritance, we can create a class which uses all the properties and behavior of
another class. The new class is known as a derived class or child class, and the one
whose properties are acquired is known as a base class or parent class.

It provides re-usability of the code.


Abstraction is used to hide internal details and show only functionalities.
Abstracting something means to give names to things so that the name

captures the core of what a Polymorphism


Polymorphism contains two words "poly" and "morphs". Poly means many and Morphs
means form, shape. By polymorphism, we understand that one task can be performed in
different ways. For example You have a class animal, and all animals speak. But they
speak differently. Here, the "speak" behavior is polymorphic in the sense and depends
on the animal. So, the abstract "animal" concept does not actually "speak", but specific
animals (like dogs and cats) have a concrete implementation of the action "speak".

Encapsulation
Encapsulation is also an important aspect of object-oriented programming. It is used to
restrict access to methods and variables. In encapsulation, code and data are wrapped
together within a single unit from being modified by accident.

Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly
synonym because data abstraction is achieved through encapsulation.

function or a whole program does.

Python Class and Objects


Creating classes in python
In python, a class can be created by using the keyword class followed by the class name.
The syntax to create a class is given below.

Syntax
class ClassName:  
    #statement_suite

Example
class Employee:  
    id = 10;  
    name = "ayush"  
    def display (self):  
        print(self.id,self.name) 

Creating an instance of the class


A class needs to be instantiated if we want to use the class attributes in another class or
method. A class can be instantiated by calling the class using the class name.
The syntax to create the instance of the class is given below.

<object-name> = <class-name>(<arguments>)   

The following example creates the instance of the class Employee defined in the above
example.

Example
class Employee:  
    id = 10;  
    name = "John"  
    def display (self):  
        print("ID: %d \nName: %s"%(self.id,self.name))  
emp = Employee()  
emp.display()  

Output:

ID: 10
Name: ayush

Python Inheritance
Inheritance is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).

Syntax
class derived-class(base class):  
    <class-suite>   

A class can inherit multiple classes by mentioning all of them inside the bracket. Consider the following syntax.
How Dijango tutorial contents How Dijango tutorial contents

d.speak()  

Output:

dog barking
Animal Speaking

Python Multi-Level inheritance


Multi-Level inheritance is possible in python like other object-oriented languages. Multi-level inheritance is archived
when a derived class inherits another derived class. There is no limit on the number of levels up to which, the multi-
level inheritance is archived in python.

class Animal:  
    def speak(self):  
        print("Animal Speaking")  
#The child class Dog inherits the base class Animal  
class Dog(Animal):  
    def bark(self):  
        print("dog barking")  
#The child class Dogchild inherits another child class Dog  
class DogChild(Dog):  
    def eat(self):  
        print("Eating bread...")  
d = DogChild()  
d.bark()  
d.speak()  
d.eat()  

Output:

dog barking
Animal Speaking
Eating bread

Python Multiple inheritance


Python provides us the flexibility to inherit multiple base classes in the child class

Example
class Calculation1:  
    def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(d.Summation(10,20))  
print(d.Multiplication(10,20))  
print(d.Divide(10,20))  

Output:

30
200
0.5

The issubclass(sub,sup) method


The issubclass(sub, sup) method is used to check the relationships between the specified classes. It returns true if the
first class is the subclass of the second class, and false otherwise.

Consider the following example.


Example  
    class Calculation1:
def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(self,a,b):  
        return a/b;  
d = Derived()  
print(issubclass(Derived,Calculation2))  
print(issubclass(Calculation1,Calculation2))  

Output:

True
False

The isinstance (obj, class) method


The isinstance() method is used to check the relationship between the objects and classes. It returns true if the first
parameter, i.e., obj is the instance of the second parameter, i.e., class.

Consider the following example.

Example
class Dog(Animal):  
    def speak(self):  
        print("Barking")  
d = D
og()  
d.speak()  
Output:     def Summation(self,a,b):  
        return a+b;  
class Calculation2:  
    def Multiplication(self,a,b):  
        return a*b;  
class Derived(Calculation1,Calculation2):  
    def Divide(class Calculation1:  
self,a,b):  
        return a/b;  
d = Derived()  
print(isinstance(d,Derived))  

Output:

True

Method Overriding
We can provide some specific implementation of the parent class method in our child class. When the parent class
method is defined in the child class with some specific implementation, then the concept is called method overriding.
We may need to perform method overriding in the scenario where the different definition of a parent class method is
needed in the child class.
Consider the following example to perform method overriding in python.

Example
class Animal:  
    def speak(self):  
        print("speaking")  

Barking

Real Life Example of method overriding


class Bank:      
def getroi(self):  
     return 10;  
class SBI(Bank):  
    def getroi(self):  
        return 7;  
  
class ICICI(Bank):  
    def getroi(self):  
        return 8;  
b1 = Bank()  
b2 = SBI()  
b3 = ICICI()  
print("Bank Rate of interest:",b1.getroi());  
print("SBI Rate of interest:",b2.getroi());  
print("ICICI Rate of interest:",b3.getroi());  

Output:

Bank Rate of interest: 10


SBI Rate of interest: 7
ICICI Rate of interest: 8

Data abstraction in python


Abstraction is an important aspect of object-oriented programming. In python, we can also perform data hiding by
adding the double underscore (___) as a prefix to the attribute which is to be hidden. After this, the attribute will not
be visible outside of the class through the object.

Consider the following example.

Example
class Employee:  
    __count = 0;  
    def __init__(self):  
        Employee.__count = Employee.__count+1  
    def display(self):  
        print("The number of employees",Employee.__count)  
emp = Employee()  
emp2 = Employee()  
try:  ''class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(isinstance(d,Derived))

class Animal:
def speak(self):
print("speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d = Dog()
d.speak()'''
    print(emp.__count)  
finally:  
    emp.display()  

Output:

The number of employees 2


AttributeError: 'Employee' object has no attribute '__count'

You might also like