3. BACKGROUND
Procedure Oriented Programming
OOPS concept
Features of OOP
Class
Object
Encapsulation
Inheritance
Polymorphism
Abstraction
4. PROCEDURE ORIENTED PROGRAMMING
Procedural programming uses a list of instructions
to perform computations step by step.
It works on sequential instructions divided into small
parts called functions or procedures.
It is not easy to maintain the codes when the project
becomes lengthy.
Testing consumes a lot of time for enterprise
projects.
Procedural languages do not provide a proper way
for data hiding, so it becomes less secure.
5. OBJECT-ORIENTED PROGRAMMING (OOP)
Object-oriented programming (OOP) is a
programming paradigm based on the concept of
"objects", which can contain data member and
code.
Data member is in the form of fields (often known as
attributes or properties), and Code, is in the form of
methods (relate it to behaviors or functions).
7. WHAT IS CLASS..?
A class can be defined as a template/ blueprint that
describes the state and behavior of an object of its
type.
A class contain fields and methods to describe the
state and behaviour of an object respectively.
Methods are the members of a class that alter the
state of an object by performing business logics.
10. WHAT IS OBJECT?
Objects are the basic run-time real entities in an object-
oriented system.
They may represent a person, a place, a bank account,
a table of data or any item that the program must
handle.
An object has two characteristics: Attributes & Behavior.
Classes are just a logical representation whereas
Objects are physical representation.
Objects upon initialization are loaded in memory.
Syntax: reference variable=classname()
For Example: s1 = Student()
11. VARIABLES IN CLASSES
Data/Fields/Attributes/Properties can be represented
by a variable. A variable by its definition means
something that can be changed.
Types of variable.
• instance (scope: object)
• static (scope: class)
• local(scope: method)
12. INSTANCE VARIABLES
An instance variable of an object is a piece of
information attached to an instance (object).
Instance variables are defined inside the instance
method and constructor by using "self" keyword.
Example: studentId, firstname, lastname etc.
An object can usually have many instance
variables, of many different types.
Each object is given its own private space(Memory)
to hold its instance variable.
Assigning a new value to an instance variable of
one object does not affect the instance variables of
any other object.
14. INSTANCE METHODS
When we define objects, we usually have an idea of
what we want to do with them.
I'm dealing with Person objects in an employee
database... I should be able to ask each Person
object their name, weight, and age.
An action that involves a single object is usually
implemented as a special kind of
function/subroutine attached to that object's class,
called an instance method (or, more commonly, just
a method).
15. __INIT__() METHOD
The __init__() method is the constructor and is
always called when an object is created.
Constructor is a special type of method which is
used to initialize the instance variables of the class.
Syntax of constructor declaration.
def __init__(self):
#body of constructor
This method can take any number of
arguments. However, the first argument "self" in the
definition is special.
16. SELF
The first argument of instance method and
constructor is a reference to the current instance of
the class.
Similar to the keyword "this" in Java, C++ etc.
You do not give a value for this parameter(self)
when you call the method, Python will provide it
automatically.
17. ENCAPSULATION
Encapsulation 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.
It can be achieved by: private members of class
__variable
__method
Access to this data is typically only achieved
through special methods: Getters and Setters By
using solely get() and set() methods
18. PRIVATE DATA
Variable names starting with two underscore
characters cannot be accessed from outside of the
class. At least not directly, but they can be
accessed through public members of class.
class Encapsulation(object):
def __init__(self, a, b, c):
self.public = a
self.__private=b
20. INHERITANCE
Inheritance is the capability of one class to derive or
inherit the properties from some another class.
Benefits of inheritance are:
It represents real-world relationships well.
It provides reusability of a code. We don’t have to write
the same code again and again. Also, it allows us to add
more features to a class without modifying it.
It is transitive in nature, which means that if class B
inherits from another class A, then all the subclasses of
B would automatically inherit from class A.
Syntax:
class derivedclass(base class):
21. TYPES OF INHERITANCE
1. Single inheritance
2. Multiple inheritance
3. Multilevel Inheritance
4. Hybrid Inheritance
5. Hierarchical Inheritance
22. SINGLE INHERITANCE
When a child class inherits from only one
parent class, it is called as single
inheritance.
23. MULTIPLE INHERITANCE
When a child class inherits from multiple parent
classes.
Python supports multiple inheritance. We specify
all parent classes as comma separated list in
bracket.
25. HYBRID INHERITANCE
This form combines more than one form of
inheritance. Basically, it is a blend of more than one
type of inheritance
26. POLYMORPHISM
Polymorphism is ability to use a common interface
for multiple forms (data types).
It refers to the use of a single type entity (method,
operator or object) to represent different types in
different scenarios.
27. POLYMORPHISM CONTD.
We can implement polymorphism by
Overloading
operator
method
constructor
Overriding
method
constructor
28. POLYMORPHISM CONTD.
Method Overloading, a way to create multiple
methods with the same name but different
arguments.
It is achieved by passing different number
of arguments or by passing different types of
arguments.
29. POLYMORPHISM CONTD.
Method Overriding: It allows us to change the
implementation of function in the child class which
is defined in the parent class.
Following conditions must be met for overriding
a function:
Inheritance should be implemented. Method overriding
cannot be done within a class. We need to derive a child
class from a parent class.
The function that is redefined in the child class should
have the same definition/ signature as in the parent
class i.e. same number of parameters.
30. DATA ABSTRACTION
It is the process of hiding the real implementation of an
application from the user and emphasizing only on
usage of it.
E.g. when you use mobile, we need not know how
pressing a key changes the volume level. We just need
to know that pressing up button increases the volume
and the down button reduces the volume.
Why Do We Need Abstraction?
Through the process of abstraction, a programmer can
hide all the irrelevant data/process of an application in
order to reduce complexity, provide security and
increase the efficiency.
31. DATA ABSTRACTION CONTD.
Abstract class
Abstract class are the classes which contain one or
more abstract method.
Abstract Method
Abstract Methods are the methods which only
have declaration and no definition.
For eg: Let's assume, we need to replicate a Zoo in a program. We will list out all the animals and create functions for them or we can make a single function and handle all animals in that function.
Now, animals have distinct behaviors along with some common behaviors and all of them have some pattern.
All animals eat, sleep, walk, make noise and run but their patterns are different.
Also, animals have distinct properties like Elephants have trunk, Rhinos have horns etc.
To address this, we need to pass each and every property as a parameter to the function so that function knows what to do for which animal combination.
Even if we created separate functions for each animal to reduce the complexity, it would still be difficult to complete the process without any glitches(unexpected fault).
So, there is need for an approach which is more realistic.
As we came across the drawbacks of procedure oriented programming, let's learn how oop fixed them and gave us a much simpler and at the same time a robust way to programming which is still in use.