Professional Documents
Culture Documents
UNIT NO 5
Prepared By :
Pallavi S Joshi
Major Focus is on Object
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.
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
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
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.
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.
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
• When we overload the + operator to be used with strings, then Fraction1 + Fraction2
Polymorphism is mainly divided into two types:
50
Compile time polymorphism:
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.
objects
Runtime Polymorphism
54
Class Animal
Speak()
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
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.
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