OOPS CONCEPT

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

OOPS CONCEPT

The main concept of OOPs is to bind the data and the functions
that work on that together as a single unit so that no other part of
the code can access this data.
OOPs Concepts in Python
 Class
 Objects
 Polymorphism
 Encapsulation
 Inheritance
 Data Abstraction

Python
Class
A class is a collection of objects. A class contains the blueprints or
the prototype from which the objects are being created. It is a
logical entity that contains some attributes and methods.

Class Definition Syntax:


class ClassName:
# Statement-1
.
.
.
# Statement-N

Python Objects
The object is an entity that has a state and behavior associated
with it. It may be any real-world object like a mouse, keyboard,
chair, table, pen, etc. Integers, strings, floating-point numbers,
even arrays, and dictionaries, are all objects.

Creating an Object

obj = Dog()
Python Inheritance
Inheritance is the capability of one class to derive or inherit the
properties from another class. The class that derives properties is
called the derived class or child class and the class from which the
properties are being derived is called the base class or parent
class.

Types of Inheritance
 Single Inheritance: Single-level inheritance enables a
derived class to inherit characteristics from a single-parent
class.
 Multilevel Inheritance: Multi-level inheritance enables a
derived class to inherit properties from an immediate
parent class which in turn inherits properties from his
parent class.
 Hierarchical Inheritance: Hierarchical-level inheritance
enables more than one derived class to inherit properties
from a parent class.
 Multiple Inheritance: Multiple-level inheritance enables
one derived class to inherit properties from more than one
base class.

# Python code to demonstrate how parent constructors


# are called.
# parent class
class Person(object):

# __init__ is known as the constructor


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

def display(self):
print(self.name)
print(self.idnumber)

def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))

# child class
class Employee(Person):
def __init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post

# invoking the __init__ of the parent class


Person.__init__(self, name, idnumber)

def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post))

# creation of an object variable or an instance


a = Employee('Rahul', 886012, 200000, "Intern")

# calling a function of the class Person using


# its instance
a.display()
a.details()
output:
Rahul
886012
My name is Rahul
IdNumber: 886012
Post: Intern

Python Polymorphism
Polymorphism simply means having many forms. For example, we
need to determine if the given species of birds fly or not, using
polymorphism we can do this using a single function.
class Bird:
def intro(self):
print("There are many types of birds.")

def flight(self):
print("Most of the birds can fly but some
cannot.")

class sparrow(Bird):

def flight(self):
print("Sparrows can fly.")

class ostrich(Bird):

def flight(self):
print("Ostriches cannot fly.")

obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()

obj_bird.intro()
obj_bird.flight()

obj_spr.intro()
obj_spr.flight()

obj_ost.intro()
obj_ost.flight()
Output
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

Python Encapsulation
Encapsulation is one of the fundamental concepts in object-
oriented programming (OOP). It describes the idea of wrapping
data and the methods that work on data within one unit.

You might also like