OBJECT ORIENTED
PROGRAMMING
UNIT 1
CONTENT
Introduction to OOPS, Characteristics of OOPS, Object oriented
languages, comparison between procedural and object oriented
programming, basic principles of Object Orientation-class, object,
abstraction, encapsulation, inheritance, polymorphism, modularity, and
message passing. Features of object orientation - attributes, state,
identity, operation, behaviour.
• Object-Oriented Programming is a paradigm that provides many concepts,
such as inheritance, data binding, polymorphism, etc.
• Simula is considered the first object-oriented programming language. The
programming paradigm where everything is represented as an object is
known as a truly object-oriented programming language.
• Smalltalk is considered the first truly object-oriented programming
language.
• The popular object-oriented languages are Java,PHP,C#,Python etc
• The main aim of object-oriented programming is to implement real-world
entities, for example, object, classes, abstraction, inheritance,
polymorphism,etc.
• OOP is an approach to program organization and development, which
attempts to eliminate some of the drawbacks of conventional
programming methods by incorporating the best of structured
programming features with several new concepts.
• OOP allows us to decompose a problem into number of entities called
objects and then build data and methods (functions) around these
entities.
• The data of an object can be accessed only by the methods associated
with the object
OOPs (Object-Oriented Programming System)
• Object-Oriented Programming is a methodology or paradigm to
design a program using classes and objects. It simplifies software
development and maintenance by providing some concepts:
Features of OOP
• Objects
• Class
• Inheritance
• Abstraction
• Polymorphism
• Encapsulation
• Message passing
[Link]
• Any entity that has state and behavior is known as an object. For
example, Person, a chair, pen, table, keyboard, bike, etc. It can be
physical or logical.
• An object contains an address and takes up some space in memory.
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.
• Example: A dog is an object because it has states like color, name,
breed, etc. as well as behaviors like wagging the tail, barking, eating,
etc.
[Link]
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint from which you can create
an individual object. Class doesn't consume any space.
• Characteristics of an object are represented in a class as Properties.
The actions that can be performed by objects become functions of
the class and is referred to as Methods.
• A class is thus a collection of objects of similar type . for example:
mango, apple, and orange are members of the class fruit . ex: fruit
mango; will create an object mango belonging to the class fruit
3. Inheritance
• When one object acquires all the properties and behaviors of a parent
object, it is known as inheritance.
• Inheritance allows to reuse classes by deriving a new class from an
existing one.
• The existing class is called the parent class, or superclass, or base
class.
• The derived class is called the child class or subclass or derived class.
• The child class inherits characteristics of the parent class(i.e the child
class inherits the methods and data defined for the parent class
• It provides code reusability.
Different types of inheritance
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
Single inheritance
• In this inheritance, a derived class is created from a single base
class.
• In the given example, Class A is the parent class and Class B is
the child class since Class B inherits the features and behavior of
the parent class A.
Multi-level inheritance
• In this inheritance, a derived class is created from another
derived class.
• In the given example, class c inherits the properties and
behavior of class B and class B inherits the properties and
behavior of class B. 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
• In this inheritance, a derived class is created from more than one
base class. This inheritance is not supported by Java Language.
• 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
• 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.
• In the given example, class A has two children class B and class
D. Further, class B and class C both are having two children -
class D and E; class F and G respectively.
4. Polymorphism
• Ability to take more than one form is known as polymorphism. For
example: to convince the customer differently, to draw something, for
example, shape, triangle, rectangle, etc.
• In Java, we use method overloading and method overriding to achieve
polymorphism.
• Two types
• Static polymorphism- Compile time –method overloading
• Dynamic polymorphism – Run time – Method overriding.
5. Abstraction
• Hiding internal details and showing functionality is known as abstraction.
For example phone call, we don't know the internal processing.
• In Java, we use abstract class and interface to achieve abstraction.
• Two ways of achieving abstraction
• 1. Functional abstraction
- Refers to function that can be used without considering how its
implemented
• 2. Data abstraction
- Refers to data that can be used without considering how its stored
6. Encapsulation
• 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.
• A java class is the example of encapsulation. Java bean is the fully
encapsulated class because all the data members are private here.
7. Coupling
• Coupling refers to the knowledge or information or dependency of
another class.
• It arises when classes are aware of each other. If a class has the
details information of another class, there is strong coupling.
• In Java, we use private, protected, and public modifiers to display the
visibility level of a class, method, and field.
8. Cohesion
• Cohesion refers to the level of a component which performs a single
well-defined task.
• A single well-defined task is done by a highly cohesive method.
• The weakly cohesive method will split the task into separate parts.
The [Link] package is a highly cohesive package because it has I/O
related classes and interface.
• However, the [Link] package is a weakly cohesive package because
it has unrelated classes and interfaces.
9. Message passing
• Interaction between objects
• The objects are communicate to each other by mechanism called
message passing.
• It will change the state of an object.
• The object may communicate through the methods.
• The messages are send and receive by passing various variables
among specific methods.
Advantage of OOPs over Procedure-oriented
programming language
1) OOPs makes development and maintenance easier, whereas, in a
procedure-oriented programming language, it is not easy to manage if
code grows as project size increases.
2) OOPs provides data hiding, whereas, in a procedure-oriented
programming language, global data can be accessed from anywhere.
Figure: Data Representation in Procedure-Oriented Programming
• Figure: Data Representation in Object-Oriented Programming
• 3) OOPs provides the ability to simulate real-world event much more
effectively. We can provide the solution of real word problem if we
are using the Object-Oriented Programming language.
Benefits of OOP
• Modular, scalable, extensible, reusable, and maintainable.
• It models the complex problem in a simple structure.
• Object can be used across the program.
• Code can be reused.
• We can easily modify, append code without affecting the other code blocs.
• Provides security through encapsulation and data hiding features.
• Beneficial to collaborative development in which a large project is divided
into groups.
• Debugging is easy.
Limitations of OOP
• Requires intensive testing processes.
• Solving problems takes more time as compared to Procedure Oriented
Programming.
• The size of the programs created using this approach may become larger than the
programs written using the procedure-oriented programming approach.
• Software developed using this approach requires a substantial amount of pre-
work and planning.
• OOP code is difficult to understand if you do not have the corresponding class
documentation.
• In certain scenarios, these programs can consume a large amount of memory.
• Not suitable for small problems.
• Takes more time to solve problems.
Applications of OOPs
• Computer graphics applications
• Object-oriented database
• User-interface design such as windows
• Real-time systems
• Simulation and modeling
• Client-Server System
• Artificial Intelligence System
• CAD/CAM Software
• Office automation system
[Link]. On the basis of Procedural Programming Object-oriented
programming
1. Definition It is a programming Object-oriented
language that is derived programming is a computer
from structure programming programming design
and based upon the concept philosophy or methodology
of calling procedures. It that organizes/models
follows a step-by-step software design around data
approach in order to break or objects rather than
down a task into a set of functions and logic.
variables and routines via a
sequence of instructions.
2. Security It is less secure than OOPs. Data hiding is possible in
object-oriented
programming due to
abstraction. So, it is more
secure than procedural
programming.
3. Approach It follows a top-down It follows a bottom-up
approach. approach.
4. Data movement In procedural programming, In OOP, objects can move
data moves freely within and communicate with
the system from one each other via member
function to another. functions.
5. Orientation It is structure/procedure- It is object-oriented.
oriented.
6. Access modifiers There are no access The access modifiers in
modifiers in procedural OOP are named as private,
programming. public, and protected.
7. Inheritance Procedural programming There is a feature of
does not have the concept inheritance in object-
of inheritance. oriented programming.
8. Code reusability There is no code reusability It offers code reusability by
present in procedural using the feature of
programming. inheritance.
9. Overloading Overloading is not possible In OOP, there is a concept
in procedural of function overloading
programming. and operator overloading.
10. Complex problems It is not appropriate for It is appropriate for complex
complex problems. problems.
11. Data hiding There is not any proper There is a possibility of data
way for data hiding. hiding.
12. Program division In Procedural In OOP, a program is divided
programming, a program into small parts that are
is divided into small referred to as objects.
programs that are referred
to as functions.
13. Examples Examples of Procedural The examples of object-
programming include C, oriented programming are -
Fortran, Pascal, and VB. .NET, C#, Python, Java, [Link],
and C++.
state, behavior and identity
• All the objects have a state, behavior and identity.
• State of an object - The state or attributes are the built in
characteristics or properties of an object. For example, a T.V has the
size, colour, model etc.
• Behaviour of the object - The behavior or operations of an object
are its predefined functions. For example, a T.V. can show picture ,
change channels, tune for a channel etc. in object oriented
programming terminology the behavior is implemented through
methods.
• Object identity - Property of an object which distinguishes it from
all others.
• Since object occupy a space in memory so they have unique address
in memory
A few kinds of operation that may perform on
an object
• Modifier
Alters the state of an object
• Selector
Accesses the state of an object, but does not alter it.
• Constructor
Create an object and initializes its state.