Unit 5-Object Oriented Programming
Unit 5-Object Oriented Programming
Programming
Computer Programming &
Programming Languages
• A Program is a collection of instructions that tells the computer
how to solve a particular problem.
• We have already done with algorithms and flowcharts.
• Computer Programming goes a step ahead in problem solving.
• Programming is the process of taking an algorithm and writing it
in a particular programming language so it can be understood
by the computer and executed accordingly.
Generations of Programming
Languages
• There were basically 5 generations of programming languages:
• First Generation: Machine Language
• Second Generation: Assembly Language
• Third Generation: High Level language
• Fourth Generation: Very High level Language
• Fifth Generation Programming language
Programming Paradigms
• A Programming Paradigm is a fundamental style of
programming that defines how the structure and basic elements
of a computer program will be built.
MOV AX, A
ADD AX, B
MOV SUM, AX Sequential code
JMP STOP with JMP
Instructions
…………………………
……
STOP: .EXIT
Monolithic Programming
• They just have one program module as programming languages
do not support the concept of subroutines.
• Therefore everything is clubbed into a single application.
• This makes the program size huge but also very difficult to
debug and maintain.
Procedural Programming
• In procedural languages, a program is divided into n number of
subroutines that access global data.
• To avoid repetition of code, each subroutine performs a
well-defined task.
• A subroutine that needs the service provided by another
subroutine can call that subroutine.
• Therefore, with 'jump', 'goto', and 'call' instructions, the
sequence of execution of instructions can be altered.
• FORTRAN and COBOL are two popular procedural
programming languages.
Procedural Programming
Structured Programming
• Structured programming is also referred to as modular
programming.
• It was specifically designed to enforce a logical structure on the
program to make it more efficient and easier to understand and
modify.
• Structured programming was basically defined to be used in
large programs that require a large development team to
develop different parts of the same program.
• It applies top-down approach.
• This allows codes to be loaded into the memory more efficiently
and can be reused in other programs
Object Oriented Programming
• With the increase in size and complexity of programs, there was a
need for a new programming paradigm that could help to develop
maintainable programs.
• To implement this, the flaws in previous paradigms had to be
corrected.
• Consequently, OOP was developed.
• It treats data as a critical element in the program development and
restricts its flow freely around the system.
• We have seen that monolithic, procedural, and structured
programming paradigms are task-based as they focus on the actions
the software should accomplish.
Object Oriented Programming
• However, the object oriented paradigm is task-based and
data-based.
• In this paradigm, all the relevant data and tasks are grouped
together in entities known as objects.
Features of Object Oriented
Programming
• The object oriented language must support mechanisms to
define, create, store, manipulate objects and allow to
communicate between objects.
• They are as follows:
• Classes
• Inheritance
• Reusability
• Objects
• Polymorphism
Features of Object Oriented
Programming
• Delegation
• Methods
• Containership
• Data Abstraction and Encapsulation
• Message passing
Classes
• Python is an object oriented programming language.
• Every languages has some basic data types such as int, float,
long and so on.
• Almost everything in Python is an object, with its properties and
methods.
• A class is used to describe something in the world such as
occurrences, things, external entities and so on.
• A class provides the template or a blueprint that describes the
structure and behaviour of a set of similar objects.
• Once we create a class, instances can be very easily created.
Classes
• For ex:
• We create a class named student.
• A student has attribute such as roll no, name, course, and
aggregate.
• Therefore we can say that a class describes one or more similar
objects.
• Thus, a class is a collection of objects.
Defining a class
• A Class is like an object constructor, or a "blueprint" for creating
objects.
• Like function definitions begin with the def keyword, class definitions
begin with a class keyword.
• Syntax: class class_name :
class body
• Ex:
• class MyClass:
x=5
Objects
• An object is an instance of a class.
• A class is like a blueprint while an instance is a copy of the class
with actual values.
• The object is an entity that has a state and behaviour associated with it.
• It may be any real-world object like the mouse, keyboard, chair, table,
pen, etc.
• Integers, strings, floating-point numbers, even arrays, and dictionaries,
are all objects
• When we define a class only the description or a blueprint of the object is
created.
• There is no memory allocation until we create its object.
Objects
• An object consists of :
• State: It is represented by the attributes of an object. It also
reflects the properties of an object.
• Behavior: It is represented by the methods of an object. It also
reflects the response of an object to other objects.
• Identity: It gives a unique name to an object and enables one
object to interact with other objects.
Objects
State / Behaviours
Identity Attributes
Name of the Breed Bark
dog Age Sleep
Color Eat
Declaring Objects
• It is also called as instantiating a class.
• When an object of a class is created, the class is said to be
instantiated.
• All the instances share the attributes and the behaviour of the
class.
• But the values of those attributes, i.e. the state are unique for
each object.
• A single class may have any number of instances.
Declaring Objects
Class Dog
Dog 2 Dog 4
Behaviour:
Bark, Eat, Sleep
Declaring Objects
• Syntax: object_name=class_name()
statements
• p1 = MyClass()
print(p1.x)
Sender Receiver
Object Object
Parent+
child’s
features
Inheritance
• Python Inheritance syntax:
class BaseClass:
Body of base class
class DerivedClass(Base Class):
Body of Dervied class
Mil=10
Com=BMW
C C
1 2
Mil = 10 Mil = 10
Com=BMW Com=BMW
Types of variables
CA
R
Mil=10
Com=BMW
C C
1 2
Mil = 8 Mil = 10
Com=BMW Com=BMW
Types of variables
• Class Variables:
• They are also called as static variables
• These are the variables which are common for all.
• If we define a variable outside __init__ method but inside a class ,
it is called as a class variable.
• So we will understand this using an example.
Types of variables
CA
R
Mil=10
Com=BMW
Wheels C C Wheels
=4 1 2 =4
Mil = 8 Mil = 10
Com=BMW Com=BMW
Types of variables
CA
R
Wheels =4 Class Variables
__init__
Mil=10 Instance
Variables
Com=BMW
Class method
• A class method is a method that is bound to a class rather than its
object.
• It doesn't require creation of a class instance.
• The syntax is :
@classmethod
def func(cls,args,…)