You are on page 1of 81

PROGRAMMING AND PROBLEM SOLVING

UNIT NO 5

LECTURE – 2.1- INTRODUCTION TO OBJECT ORIENTED


PROGRAMMING

Prepared By :
Pallavi S Joshi
 Major Focus is on Object

 OOP is Design Methodology

 Represented in terms of Objects

 The object oriented language must support mechanisms to

define, create, store, manipulate objects, and allow

communication between objects.


FEATURES OF OBJECT ORIENTED PROGRAMMING
Features of OOPS:

1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance
6) Polymorphism
7) Containership
8) Reusability
9) Delegation
10)Data Abstraction and Encapsulation
5
CLASS

A Car
Plan or structure (wheels, engine, windows, head lights , tail lights etc)
New car manufactured= new instance of car is created
Car = Example of Class

6
7
1) Classes
• A class is used to describe something in the world, such as occurrences, things, external
entities, and so on.
• A class provides a template or a blueprint that describes the structure and behavior of a set of
similar objects.
• Once we have the definition for a class, a specific instance of the class can be easily created.
• Definition: A class represents a collection of objects having same characteristic properties that
have common behavior.
• Class is blue print of an Object.
• Example: mango, apple and orange are member of class fruit.
Class  name, attributes and operations.

Class Mobile
Name iPhone , One Plus , Samsung , Nokia
Attributes IMEI Number, processor, camera etc.
Operations  Dial, Receiving call, Send message etc.

Class Person
Name  Rohan, Sumit, Geeta
Attribute  Height, weight , color etc.
Operations  Walk , talk , Sing , eat , dance etc. 9
OBJECT

11
2) Objects
• Once we have the definition for a class, a specific instance of the class can be easily created.
• A class can have multiple instances or objects.

Objects
2) Objects
• Objects can be an entity which may represented a person, a place, a bank account or any other item that the
program must handle.
• When program is executed, the objects interact by sending message to an another.
• Example: If customer and account are two objects in a program, then the customer object may send a message to
the account object requesting for the bank balance.
• Definition: An Object are the components of a program that known how to perform certain actions and how to
interact with the elements of the program.

• An object can be something in the physical world or even


just an abstract idea.

• An airplane, for example, is a physical object that can be


manipulated by a computer.
2) Objects

Two way of representing Object


Syntax of creating object:
1. Class name object name
2. object name = class name()
To create object of fruit class we can write like this:
fruit mango; mango is object of class fruits

17
PROGRAMMING AND PROBLEM SOLVING
UNIT 5
LECTURE – 2.3- INTRODUCTION TO OBJECT
ORIENTED PROGRAMMING

Prepared By :
Pallavi S Joshi
FEATURES OF OBJECT ORIENTED PROGRAMMING
Features of OOPS:
1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance
6) Polymorphism
7) Containership
8) Reusability
9) Delegation
10)Data Abstraction and Encapsulation
Features of OOPS:
1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance
6) Polymorphism
7) Containership
8) Reusability
9) Delegation
10)Data Abstraction and Encapsulation
3) Methods
• Every object contains some data and procedures.
• A method is a function or procedure associated with a class.
• It defines the operations that the object can execute when it
receives a message.
• In OOP language, only methods of the class can access and
manipulate the data stored in an instance of the class (or object).
Example of class Object and Method
4) Message Passing

• Two objects can communicate with each other through messages.


• An object asks another object to invoke one of its methods by sending it a
message.
Receive
r Object Message – get details of
Sender student with roll no 1
object
Reply- Roll no 1, Ram,
Btech, 92

26
PROGRAMMING AND PROBLEM SOLVING
UNIT 5
LECTURE 2.4 INTRODUCTION TO OBJECT
ORIENTED PROGRAMMING
INHERITANCE

Prepared By :
Pallavi S Joshi
Inheritance

What is Inheritance?
Inheritance is a mechanism in which one class acquires the property
of another class.
For example, a child inherits the traits of his/her parents.
With inheritance, we can reuse the fields and methods of the existing
class.
Hence, inheritance facilitates Reusability and is an important concept
29

of OOPs.
In this tutorial, you will
learn-
 Examples of Inheritance
 Types of Inheritance

30
Inheritance
• Inheritance is a concept of OOP in which a new
class is created from an existing class.
• The new class, often known as a sub-class,
contains the attributes and methods of the
parent class
• The new class, known as sub-class or derived
class, inherits the attributes and behavior of the
pre-existing class, which is referred to as super-
class, base class or parent class.
32
Class Animal
Attributes Methods
Color Run
Tail Eat
Leg Sleep
Weight

Class Dog Class Cat Class Crow

Attrib. Methods Attrib. Methods Attrib. Methods


Color Run Color Run Color
Tail Eat Tail Eat Tail Eat
Leg Sleep Leg Sleep Leg Sleep
Weight Barks Weight mew Weight Fly
More about Inheritance
• The inheritance relationship of sub- and super classes generates a hierarchy.
• Therefore, inheritance relation is also called ‘is-a’ relation.
• A sub-class not only has all the states and behaviors associated with the super-class but
has other specialized features (additional data or methods) as well.
• The main advantage of inheritance is the ability to reuse the code.
• When we want a specialized class, we do not have to write the entire code for that class
from scratch.
• We can inherit a class from a general class and add the specialized code for the sub-
class.
Inheritance

Example of Inheritance
Inheritance

Example of Inheritance
Types of Inheritance

38
Single Inheritance:
In Single Inheritance one class extends another class (one class only).
      

39
Multiple Inheritance:
In Multiple Inheritance, one class extending more than one
class.

As per above diagram, Class C extends Class A and Class B


both.
Multilevel Inheritance:
In Multilevel Inheritance, one class can inherit from a derived class.
Hence, the derived class becomes the base class for the new class.

41
Hierarchical Inheritance:
As you can see in the diagram that when a class has more than one child
classes (sub classes) or in other words more than one child classes have the
same parent class

42
Hybrid Inheritance:
Hybrid inheritance is a combination of Single and Multiple
inheritance.

As per above example, all the public and protected members


of Class A are inherited into Class D, first via Class B and 43

secondly via Class C.


PROGRAMMING AND PROBLEM SOLVING
UNIT 5
LECTURE – 2.5- INTRODUCTION TO OBJECT ORIENTED
PROGRAMMING
POLYMORPHISM

Prepared By :
Pallavi S Joshi
Features of OOPS:
1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance
6) Polymorphism
7) Containership
8) Reusability
9) Delegation
10)Data Abstraction and Encapsulation
Features of OOPS:
1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance

6)Polymorphism
7) Containership
8) Reusability
9) Delegation
Polymorphism

• Polymorphism refers to having several different forms. It is related to methods and


operators.
• Polymorphism is a concept that enables the programmers to assign a different
meaning or usage to a method in different contexts.
• Polymorphism can also be applied to operators.
• For example, we know that operators can be applied only on basic data types that the
programming language supports.
• Therefore, a + b will give the result of adding a and b. If a = 2 and b = 3, then a + b =
5. 49

• When we overload the + operator to be used with strings, then Fraction1 + Fraction2
Polymorphism is mainly divided into two types:

 Compile time Polymorphism


 Runtime Polymorphism

50
Compile time polymorphism:

This type of polymorphism is achieved by 


 Function overloading or 
 Operator overloading.

51
Function Overloading

When there are multiple functions with same name but different
parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments
or/and change in type of arguments.

Python does not supports method overloading. We may overload the


methods but can only use the latest defined method (but it can be
still done in a diff way)
Operator Overloading
 There is also an option to overload operators.
 For example, we can make the operator ‘+’ for string class to
concatenate two strings.
 We know that this is the addition operator whose task is to add to
operands. So a single operator ‘+’ when placed between integer
operands , adds them and when placed between string operands,
concatenates them.

 Here Operator + can add two operands which can be


 numbers
 string or 53

 objects
Runtime Polymorphism

 This type of polymorphism is achieved by Function Overriding.


 Function overriding on the other hand occurs when a derived class
has a definition for one of the member functions of the base class.
 That base function is said to be overridden.

54
Class Animal
Speak()

Class Duck Class Dog


Class Human Speak() Speak() Speak()
Op- quack Op- Woof

Class Cat
Speak()
Op- meow
Polymorphism

In Figure
A single function name can be used
to handle different number and
different type of arguments.

56
PROGRAMMING AND PROBLEM SOLVING
UNIT 5
LECTURE – 2.6- INTRODUCTION TO OBJECT ORIENTED
PROGRAMMING

Prepared By :
Pallavi S Joshi
Features of OOPS:
1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance
6) Polymorphism
7) Containership
8) Reusability
9) Delegation
10)Data Abstraction and Encapsulation
Features of OOPS:
1) Classes
2) Objects
3) Methods
4) Message passing
5) Inheritance
6) Polymorphism
7) Containership
8) Reusability
9) Delegation
10)Data Abstraction and Encapsulation
Containership

• Containership is the ability of a class to contain object(s) of one or more


classes as member data.
• For example, class One can have an object of class Two as its data
member.
Class B
Class A ---
--- Contained --- Container
--- class Aa Class
--- aa=A()
--- --- 61
• This would allow the object of class One to call the public functions
of class Two.
• Here, class One becomes the container, whereas class Two becomes
the contained class.
• Containership is also called composition.
• Containership represents a ‘has-a’ relationship.

62
Difference between containership and inheritance

Containership
-> When features of existing class are wanted inside your new class, but, not its interface
for eg->
1)computer system has a hard disk
2)car has an Engine, chassis, steering wheels.

Inheritance
-> When you want to force the new type to be the same type as the base class.
for eg->
1)computer system is an electronic device
2)Car is a vehicle
63
64
Reusability

• Reusability means developing codes that can be reused either in the same
program or in different programs.
• Python gives due importance to building programs that are reusable.
• Reusability is attained through inheritance, containership, and
polymorphism.

65
Delegation
• To provide maximum flexibility to programmers and to allow them to generate a
reusable code, object oriented languages also support delegation.
• In composition, an object can be composed of other objects and thus the object
exhibits a ‘has-a’ relationship.
• In delegation, more than one object is involved in handling a request.
• The object that receives the request for a service, delegates it to another object called
delegate.

66
Delegation
• The property of delegation emphasizes on the ideology that a complex object is made
of several simpler objects.
• For example, our body is made up brain, heart, hands, eyes, ears, etc.
• The functioning of the whole body as a system rests on correct functioning of the parts
it is composed of.
• Similarly, a car has a wheel, brake, gears, etc. to control it.
• Delegation differs from inheritance in the way that two classes that participate in
inheritance share an ‘is- a’ relationship; however, in delegation, they have a ‘has- a’
67

relation.
PROGRAMMING AND PROBLEM SOLVING
UNIT 5
LECTURE – 2.7- INTRODUCTION TO OBJECT ORIENTED
PROGRAMMING

Prepared By :
Pallavi S Joshi
Data Abstraction

• Data abstraction refers to the process by which data and functions are
defined in such a way that only essential details are revealed and the
implementation details are hidden.
• The main focus of data abstraction is to separate the interface and the
implementation of a program.
Data Encapsulation
• Data encapsulation, also called data hiding, is the technique of packing data and
functions into a single component (class) to hide implementation details of a class from
the users.
• Users are allowed to execute only a restricted set of operations (class methods) on the
data members of the class.
• Therefore, encapsulation organizes the data and methods into a structure that
prevents data access by any function (or method) that is not specified in the class.
• This ensures the integrity of the data contained in the object.
10) Data Abstraction and Encapsulation
Encapsulation: The wrapping up of data and functions into a single unit(called class) is known
as encapsulation.
The data is not accessible to the outside world, and only those functions which are wrapped in
the class can access it.
These function provides the interface between the object’s data and the programs.
This insulation of the data from direct access by the program is called Data Hiding or
Information Hiding.
Sr. No Abstraction Encapsulation

1 In abstraction, problems are solved at the While in encapsulation, problems are solved
design or interface level. at the implementation level.
2
Whereas encapsulation is a method to hide
Abstraction is the method of hiding the
the data in a single entity or unit along with a
unwanted information.
method to protect information from outside.

3 Abstraction is the process or method of While encapsulation is the process or


gaining the information. method to contain the information.
4 Outer layer used in terms of design Inner layer used in terms of implementation
Ex: outer layer of mobile phone has Ex: inner layer of implementation has details
mobile screen, keypad to dial a number. of how keypad buttons and display screen are
connected with circuits.

74
MERITS AND DEMERITS OF OBJECT ORIENTED PROGRAMMING
Merits of OOP Languages
• Elimination of redundant code through inheritance (by extending existing classes).
• Higher productivity and reduced development time due to reusability of the existing modules.
• Secure programs as data cannot be modified or accessed by any code outside the class.
• Real world objects in the problem domain can be easily mapped objects in the program.
• A program can be easily divided into parts based on objects.
• The data-centered design approach captures more details of a model in a form that can be easily
implemented.
• Programs designed using OOP are expandable as they can be easily upgraded from small to large
systems.
Merits of OOP Languages
• Message passing between objects simplifies the interface descriptions with external systems.
• Software complexity becomes easily manageable.
• With polymorphism, behavior of functions, operators, or objects may vary depending upon the
circumstances.
• Data abstraction and encapsulation hides implementation details from the external world.
• OOP enables programmers to write easily extendable and maintainable programs.
• OOP supports code reusability to a great extent.
Demerits of OOP Languages

• Programs written using object oriented languages have greater processing overhead as
they demand more resources.
• Requires more skills to learn and implement the concepts.
• Beneficial only for large and complicated programs.
• Even an easy to use software when developed using OOP is hard to be build.
• OOP cannot work with existing systems.
• Programmers must have a good command in software engineering and programming
methodology.
APPLICATION OF OBJECT ORIENTED PROGRAMMING
Applications of OOP Languages
• Designing user interfaces such as work screens, menus, windows, and so on.
• Real-time systems • Simulation and modelling
• Compiler design • Client server system
• Object oriented databases • Object oriented distributed database
• Parallel programming • Decision control systems
• Office automation systems • Hypertext and hypermedia
• Computer-aided design (CAD) systems • Computer-aided manufacturing (CAM) systems
• Computer animation • Developing computer games
• Artificial intelligence—expert systems and neural networks
• Networks for programming routers, firewalls, and other devices

You might also like