OOPS CONCEPT
OOPS CONCEPT
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.
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.
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
def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post))
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.