Class 12
Unit 5
Object Oriented Programming
Procedural paradigm
u It
is derived from structured programming.
u It
is based on the concept of procedure call.
u Program
code is divided into procedures, block of code.
u Procedures
are known as routine, subroutines, methods or functions.
u It
is also known as top-down language
u We can call any procedure at any point during a program execution
Advantages of procedural programming
u It
is excellent for general-purpose programming.
u Easy
to code/ source code is portable
u Code
can be reused in different parts of program, without the need to copy it.
u Needs
less memory
Structural programing
u It
is also known as modular programming.
u It
emphasizes on separating a programs data from its functionality.
u Based
on top-down methodology.
u Focus
on dividing the program into sub parts, hence it simplifies problem solving.
u Eg:
Algol, Pascal, Basic, C
Advantages
u Code
is well organized
u Improved
decision-making power.
u Easy
to code and write program.
Object oriented programming
u Object-Oriented
Programming is a methodology or paradigm to design a program using classes
and objects.
u Object-oriented
programming (OOP) is a programming paradigm based upon objects
(having both data and methods) that aims to incorporate the advantages of
modularity and reusability.
u Objects, which are usually instances of
classes, are used to interact with one another to design applications and computer
programs.
Benefits of object–oriented programming
• Modularity. Encapsulation
enables objects to be self-contained, making troubleshooting and collaborative
development easier.
• Reusability. Code
can be reused through inheritance, meaning a team does not have to write the
same code multiple times.
• Productivity. Programmers
can construct new programs quicker through the use of multiple libraries and
reusable code.
• Easily
upgradable and scalable. Programmers can implement system functionalities
independently.
• Interface
descriptions. Descriptions of external systems are simple, due to
message passing techniques that are used for objects communication.
• Security. Using
encapsulation and abstraction, complex code is hidden, software maintenance is
easier and internet
protocols are protected.
• Flexibility. Polymorphism
enables a single function to adapt to the class it is placed in. Different
objects can also pass through the same interface.
Features of Object oriented Programming
u Any entity that has state and
behavior is known as an object. For example, a chair, pen, table, keyboard,
bike, etc. It can be physical or logical.
u An Object can be defined as an
instance of a class. An object contains an address and takes up some space in
memory.
u
Objects can communicate without knowing the details of each other's data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
u An Object is an identifiable entity
with some characteristics and behaviour. An Object is an instance of a Class.
u When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated
u Let us take an Example of a
car object.
u A car object named “Maruti” can have data such as color; make, model, speed limit, etc. and functions like accelerate.
u For example: in real life, a car is an object. The car has attributes, such as weight and colour, and methods, such as drive and brake.
Example
u Object: Student
u Data: Roll no., Name:, and Grade
u Function: Study(), Sleep(),
Playgame(), Calculate()
Class
u Collection of objects is called class. It is a logical entity.
u A class is a blueprint for the object.
u Before we create an object, we first
need to define the class.
u A class represents a collection of
objects having same characteristic properties that exhibit common behavior.
u for example students, employee, fruits, car.
u Student
class has: rollno, name, faculty, address.
u A
class is a group of common behavior and functions
u If
mobile is a class then Samsung galaxy a32 is a object and its attributes are
ram, memory, text, and calling
Encapsulations
u Encapsulation is the process of
binding both attributes and methods together within a class.
u Binding
(or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped
with different medicines.
u For example: capsule, it is wrapped with different medicines.
u Encapsulation is placing the data and the functions that work on that data in the same place.
u Real time examples, we encapsulate data and methods that operate on it by combining them into a single unit, the class.
u We can hide private information of a class from the outside world and only disclose functionality that is needed to interact with it this way.
u We say a class is properly enclosed when it prevents calling code from accessing its private data directly
u Through encapsulation, the internal
details of a class can be hidden from outside.
u It permits the elements of the class to be
accessed from outside only through the interface provided by the class.
u Encapsulation
means containing all important information inside an object, and only
exposing selected information to the outside world.
Examples
Realtime
Example 1:
u School bag is one of the most real
examples of Encapsulation. School bag can keep our books, pens, etc.
Realtime
Example 2:
u When you log into your email accounts
such as Gmail, Yahoo Mail there is a lot of internal processes taking place in
the backend and you have no control over it.
u When you enter the password for
logging, they are retrieved in an encrypted form and verified, and then you are
given access to your account.
u You do not have control over it that
how the password has been verified. Thus, it keeps our account safe from being
misused.
Abstraction
u Abstraction is the concept of
object-oriented programming that “shows” only essential attributes and “hides”
unnecessary information.
u The main purpose of abstraction is hiding the
unnecessary details from the users.
u Abstraction is selecting data from a larger
pool to show only relevant details of the object to the user.
u It helps in reducing programming
complexity and efforts. It is one of the most important concepts of OOPs.
Real
life examples of abstraction
u Abstraction shows only important
things to the user and hides the internal details,
u for example, when we ride a bike, we
only know about how to ride bikes but can not know about how it work? And also
we do not know the internal functionality of a bike.
u Another real life example of
Abstraction is ATM Machine; All are performing operations on the ATM machine
like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't
know internal details about ATM.
Inheritance
u Inheritance in Java is a
mechanism in which one object acquires all the properties and behaviors of a
parent object.
u The Inheritance has many advantages,
the most important of them being the reusability of code
u The reuse of existing classes saves
time and effort.
u Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.
u Using inheritance object of one class can inherit or acquire the properties of the object of another class. Inheritance provides reusability of code.
u Inheritance is the process of forming a new class from an existing class
u It provides code reusability.
u Example: For instance, we are humans. We inherit certain properties from the class 'human' such as the ability to speak, breathe, eat, drink, etc. We can also take the example of cars. The class 'car' inherits its properties from the class 'Automobiles' which inherits some of its properties from another class 'Vehicles'.
Single inheritance
u When a class extends another one
class only then we call it a single inheritance.
u The below flow diagram shows that
class B extends only one class which is A.
u Here A is a parent class of B and B
would be a child class of A.
u In this inheritance, a derived class
is created from another derived class.
u In the given example, class c
inherits the properties and behavior of class B and class B inherits the
properties and behavior of class A.
u So, here A is the parent class of B
and class B is the parent class of C. So, here class C implicitly inherits the
properties and behavior of class A along with Class B i.e there is a multilevel
of inheritance.
Multiple inheritance
u In
this inheritance, a derived class is created from more than one base class.
This inheritance is not supported by .NET Languages like C#, F#,
etc., and Java Language.
u In
the given example, class c inherits the properties and behavior of class B and
class A at the same level. So, here A and Class B both are the parent classes
for Class C.
Hierarchical Inheritance
u In
this inheritance, more than one derived class is created from a single base
class and further child classes act as parent classes for more than one child
class.
u In
the given example, class A has two children class B and class C. Further, class
B and class C both are having two children - class D and E; class F and G
respectively
Hybrid inheritance
u This is a combination of
more than one inheritance.
u Hence,
it may be a combination of Multilevel and Multiple inheritance or Hierarchical
and Multilevel inheritance Hierarchical and Multipath inheritance, or
Hierarchical, Multilevel and Multiple inheritances.
Multipath inheritance
u In
this inheritance, a derived class is created from other derived classes and the
same base class of other derived classes. This inheritance is not supported by
.NET Languages like C#, F#, etc.
u In
the given example, class D inherits the properties and behavior of class C and
class B as well as Class A.
u Both
class C and class B inherit the Class A. So, Class A is the parent for Class B
and Class C as well as Class D. So, it's making it a Multipath inheritance.
Polymorphism
u Polymorphism means "many
forms", and it occurs when we have many classes that are related to each
other by inheritance.
u In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one
form.
u Polymorphism allows us to perform a
single action in different ways. In other words, polymorphism allows you to define
one interface and have multiple implementations.
u The word “poly” means many and
“morphs” means forms, So it means many forms.
Examples of
polymorphism
u A real-life example of polymorphism
is a person who at the same time can have different characteristics. A man at
the same time is a father, a husband, and an employee.
u So the same person exhibits different
behavior in different situations. This is called polymorphism. Polymorphism is
considered one of the important features of Object-Oriented Programming.
Advantages of Oops |
Disadvantages of Oops |
We can reuse the code multiple times using class |
Bigger program size |
Inherit the class to subclass for data redundancy |
It required a lot of effort to create |
It is easy to maintain and modify |
It is slower than other programs |
It maintains the security of data |
It is not suitable for some sorts of problems |
Low-cost development |
It takes time to get used to it. |
Application
of Oops
•
Real-Time
System design: Real-time system inherits complexities and makes it
difficult to build them. OOP techniques make it easier to handle those
complexities.
•
Hypertext
and Hypermedia: Hypertext is similar to regular text as it can be stored,
searched, and edited easily. Hypermedia on the other hand is a superset of
hypertext. OOP also helps in laying the framework for hypertext and hypermedia.
•
AI
Expert System: These are computer application that is developed to solve
complex problems which are far beyond the human brain. OOP helps to develop
such an AI expert System
•
Office
automation System: These include formal as well as informal electronic
systems that primarily concerned with information sharing and communication to
and from people inside and outside the organization. OOP also help in making
office automation principle.
•
Neural
networking and parallel programming: It addresses the problem of
prediction and approximation of complex-time varying systems. OOP simplifies
the entire process by simplifying the approximation and prediction ability of
the network.
•
Stimulation
and modeling system: It is difficult to model complex systems due to
varying specifications of variables. Stimulating complex systems require
modeling and understanding interaction explicitly. OOP provides an appropriate
approach for simplifying these complex models.
•
Object-oriented
database: The databases try to maintain a direct correspondence between
the real world and database object in order to let the object retain it
identity and integrity.
•
Client-server
system: Object-oriented client-server system provides the IT infrastructure
creating object-oriented server internet(OCSI) applications.
•
CIM/CAD/CAM
systems: OOP can also be used in manufacturing and designing applications as it
allows people to reduce the efforts involved. For instance, it can be used
while designing blueprints and flowcharts. So it makes it possible to produce
these flowcharts and blueprint accurately.