MODULE 1 BCA5B08-JAVA
Introduction to OOPS
Limitation of procedure oriented programming:
Data is not secure
Many functions access the same data
Do not represent real world problems.
Arrangement of data cannot be changed without modifying all the functions.
To overcome these limitations, the concept of Object-Oriented Programming (OOP) was
introduced.
OOP(Object Oriented Programming):
OOP is a programming approach that allows decomposition of a problem into number of entities
called objects and then builds a data and functions around these objects.
Characteristics of OOPS:
Programs are divided into number of entities called objects.
Data is hidden and cannot be accessed by external functions.
Objects may communicate with each other through functions.
New functions and data can be easily added.
It follows bottom up approach.
It can solve real world problem itself.
Support inheritance: Eliminate redundant code and extend the use of existing classes.
Better quality of software.
Lesser maintenance cost.
1
MODULE 1 BCA5B08-JAVA
Object oriented languages
C++
Python
Java
Comparison between procedural and object oriented programming:
Procedural OOP
Top down approach Bottom up approach
Large programs are divided into smaller Programs are divided into number of entities
programs known as functions called objects
Most of the functions share global data. Data is hidden and cannot be accessed by
external functions.
Data move openly around the system from Objects may communicate with each other
function to function through functions
Do not solve real world problems. It can solve real world problem itself.
2
MODULE 1 BCA5B08-JAVA
Basic principles of Object Orientation
1. Class:
A class is a way to bind the data and its associated functions together. A class can have both
variables and functions as members of class. The class variables called objects.
Syntax Example
class <classname>
{
//variable declaration
<visibility_mode> <datatype> <variable>;
//function
<visibility_mode> <return_datatype> <function_name>()
{
//body
}
}
2. Object:
Objects are the basic runtime entities in an [Link] object contain data and functions
to manipulate data. Objects can interact without having to know details of each other’s data
or code.
Create Object:
Syntax Example
<classname><objectname>=new <classname>(); Person p=new Person();
3
MODULE 1 BCA5B08-JAVA
Access public datamember using object:
Syntax Example
<objectname>.<variable>;
<objectname>.<functionname>();
3. Abstraction:
Abstraction refers to the act of representing essential features without including the
background details or explanations.
Example:
4
MODULE 1 BCA5B08-JAVA
BCA5B08
4. Encapsulation:
Encapsulation is the binding of data and functions into a single unit. It is achieved
through information hiding, which protects data from direct access. This is implemented
using visibility modes such as private, public, and protected.
Example:
5
MODULE 1 BCA5B08-JAVA
BCA5B08
5. Inheritance:
Inheritance is the capability of a class to acquire the properties and behaviors of another
class. When a class inherits from another, the new class is called the derived (or subclass), and
the existing class is called the base class (or super
superclass).
class). The derived class inherits all accessible
features of the base class and can also define additional members of its own. Inheritance
promotes reusability.
Example:
6
MODULE 1 BCA5B08-JAVA
6. Polymorphism:
Polymorphism means the ability to take more than one form. It is implemented using method
overloading and method overriding.
Example:
The + operator has two uses: string concatenation and numeric addition. Polymorphism
allows a single operator or method to operate in different ways based on the context.
7. Modularity :
Modularity means dividing a program into separate parts called classes, where each class
handles a specific task. This makes the program easier to understand, develop, and maintain.
8. Message passing.
Object may communicate with each other through function. It involves specifying the name
of the object, the name of the function and the information to be sent.
Example:
7
MODULE 1 BCA5B08-JAVA
Features of object orientation
Example:
1. Attributes:class data
The variables defined inside a class. They describe what the class has.
Example:
Here name is an attribute – holds the name of the person.
2. State: object data
The current values of an object’s attributes. It shows what the object is like at a specific
moment.
Example:
[Link] = "Anu" and [Link] = "Cinu" are different states.
8
MODULE 1 BCA5B08-JAVA
BCA5B08
3. Identity:
A unique identity that distinguishes one object from another.
Example:
p1 and p2 are two different objects
4. Operation: class functions
The functions or methods defined in a class that describe what the class can do.
do
Example:
readName() and displayName() are operations
object function
5. Behavior:object
How an object acts or responds when its methods are called. It's the result of performing
operations.
Example:
9
MODULE 1 BCA5B08-JAVA
10