INTRODUCTION TO OBJECT ORIENTED PROGRAMMING CONCEPTS
PROGRAM
A program is a set of instructions written in a programming language that
tells a computer what to do and how to do it.
Programming is the process of writing instructions that tell a computer
how to perform a task.
A programming paradigm is a style or way of writing programs
Procedural programming
Programs are written as step-by-step instructions using functions.
Object-oriented programming (OOP)
Programs are organized using objects and classes.
Example: Java
Generations of Programming Languages
Programming languages are classified into generations based on their
development and ease of use.
First Generation Language (1GL)
✓ Machine language (binary: 0s and 1s)
✓ Directly understood by the computer
✓ Very difficult for humans
Example: 101101
Second Generation Language (2GL)
✓ Assembly language
✓ Uses symbols and short codes (mnemonics codes)
✓ Requires a translator called an assembler
Example: ADD, SUB
Third Generation Language (3GL)
✓ High-level languages
✓ Easy to read and write
✓ Machine independent
Examples: C, Java, Python
Fourth Generation Language (4GL)
✓ Very high-level languages
✓ Focus on problem solving and database handling
✓ Requires fewer lines of code
Example: SQL
Fifth Generation Language (5GL)
✓ Based on artificial intelligence
✓ Used for problem solving and expert systems
Example: Prolog
Difference between POP and OOP
POP OOP
It divides program into small parts It divides program into small parts
known as methods(procedure) known as objects
It deals with algorithm It deals with data
It is less secure It is more secure
It follows top to down approach It deals with bottom to up approach
Eg C , COBOL Eg C++, Java
Principles of Object Oriented Programming (OOP)
1. Data Abstraction
Definition:
Data abstraction means showing only essential features while hiding
unnecessary details.
Real-life example:
When you use an ATM, you see options like withdraw or balance check. You
don’t see the internal banking process — it is hidden.
Focus: What to do, not how it works.
2. Encapsulation
Definition:
Encapsulation means wrapping data and code (methods) together into a
single unit (class) and protecting the data.
Real-life example:
A capsule medicine contains ingredients packed together. You cannot
access them directly — they are protected inside.
Focus: Data protection and organization.
3. Inheritance
Definition:
Inheritance allows one class to acquire properties and behavior of
another class.
Real-life example:
A child inherits features like eye color or height from parents.
Focus: Reusability of code.
4. Polymorphism
Definition:
Polymorphism means one action behaving in different ways.
Real-life example:
A person behaves differently as a student in school, a friend with peers, and
a child at home.
Focus: One interface, multiple behaviours.
JAVA
Java is a high-level, object-oriented programming language used to
develop platform-independent applications.
Types of Java Programs
Java programs are mainly divided into two types:
1. Stand alone application ( Normal Java Program)
✓ A stand-alone program that runs on a computer
✓ Starts execution from the main() method
✓ Used for general tasks
2. Applet (Web Applets or Java Applets)
✓ A small Java program that runs inside a web page
✓ Requires a browser or applet viewer
✓ Used for interactive web features
Example: Animation or small web tools
Features of Java
Simple
Java is easy to learn and write because it removes complex features.
Object-Oriented
Programs are built using classes and objects, making code organized.
Platform Independent
Java programs run on any system using the Java Virtual Machine (JVM).
Write once, run anywhere.
Secure
Java provides strong security features to protect data.
Robust
Java handles errors well and reduces program crashes.
Portable
Programs can be moved easily from one system to another.
Multithreaded
Java supports running multiple tasks at the same time.
High Performance
Java gives good performance with efficient execution.
Real-Life Applications that Use Java
Java is widely used to build many real-world applications:
Web applications
Java is used to develop dynamic websites and online services.
Mobile applications
Many Android apps are built using Java.
Banking systems
Java is used in secure online banking and transaction processing.
Desktop applications
Software tools and utilities are created using Java.
Enterprise applications
Large business systems use Java for reliability.
Scientific applications
Java helps in simulations and research software.
Games
Some games are developed using Java.
------------------------------------------------------------------------------
Features of Java
Simple — easy to learn and write
Robust — handles errors efficiently
Secure — protects data and memory
Object Oriented — uses classes and objects
Platform Independent — runs on any system via JVM
Portable — easy to move between systems
Multithreaded — supports multiple tasks
High Performance — efficient execution
Real-Time Applications Using Java (Examples Only)
• Mobile applications — Android apps
• Desktop GUI applications — Acrobat Reader
• Web-based applications — Banking systems, e-commerce
• Gaming applications — interactive games
• Robotic/healthcare systems — automation tools
• Education applications — online quiz, grading systems
• Chatbots — customer service messengers
• Virtual assistants — smart assistants
----------------------------------------------------------------------------------
SOURCE CODE
Java source code is the program written by a programmer in the Java
language, saved with the .java extension
BYTECODE
Bytecode is the intermediate code produced when a Java source program is
compiled, which is executed by the Java Virtual Machine (JVM). It is stored in
a file with the .class extension.
OBJECT CODE
Object code is the compiled form of a program that is produced after
translation and is ready for execution by the computer.
Object code is the converted version of a program that the computer can
run.
COMPILATION PROCESS
Traditional Compilation Process
In traditional languages (like C/C++):
Steps:
Source Program → Compiler → Machine Code → Output
✓ The compiler converts the entire program directly into machine
language.
✓ The machine code runs only on that specific system.
Result: Platform dependent.
Java Compilation Process
Java uses a two-step compilation process:
Steps:
Source Program (.java) → Java Compiler → Bytecode(.class) → JVM →
Machine Code → Output
✓ The Java compiler converts the program into bytecode.
✓ The Java Virtual Machine (JVM) converts bytecode into machine
code.
✓ The same bytecode runs on any system with JVM.
Result: Platform independent.
JDK(Java Development Toolkit)
OBJECT ORIENTED PROGRAMMING
Modelling entities and their behaviour by objects is the process of
representing real-world things as objects in a program, where attributes
store data and methods define actions.
OBJECTS
Object is a real-world entity having 3 values attribute behaviour and state.
An object is an instance of a class that encapsulates state (attributes/data)
and behaviour (methods/actions), representing a real-world entity inside a
program.
Three core parts of an object
Attributes (Data)
Attributes describe what the object has.
They store the current condition or information of the object.
Examples:
✓ name
✓ colour
✓ speed
✓ marks
These values can change — that change is called the state of the object.
Behaviour (Methods / Actions)
Behaviour describes what the object can do.
These are functions that operate on the attributes.
Examples:
✓ move()
✓ display()
✓ calculate()
✓ study()
Behaviour often changes the object’s state.
State
State is the current value of all attributes of an object at a given time.
Example:
If a Car object has:
✓ colour = red
✓ speed = 60
This combination represents its current state.
Simple diagram
Object : Student
---------------------
Attributes (State)
- name
- rollNo
- marks
Behaviour (Methods)
- study()
- displayResult()
Real-world analogy
Think of a Student:
✓ Attributes → name, marks
✓ Behaviour → study, write exam
✓ State → marks = 85 (current condition)
Key characteristics of objects
✓ Encapsulation — data + methods together
✓ Represents real-world entities
✓ Maintains state
✓ Interacts with other objects
CLASS
A class is a blueprint or template that defines the attributes (data) and
behaviours (methods) of objects.
Why is a class called an object factory?
A factory produces products — similarly:
A class produces objects.
✓ You can create many objects from one class
✓ Each object has the same structure but different values
Why is a class user-defined?
A class is created by the programmer to model real-world entities.
Why is a class a composite data type?
A composite data type means it is made of multiple data elements.
A class combines:
✓ Variables (attributes/data)
✓ Methods (functions)
So, it stores different kinds of related data and behaviour together — making
it composite.
Example:
class Student
{
String name;
int rollNo;
double marks;
How Objects encapsulate state (attributes) and have behaviour
(methods)?
In object-oriented programming, an object bundles its data and actions into
a single unit.
Encapsulation = wrapping data + methods together
Object = state (what it has) + behaviour (what it does)
An object stores its state using variables called attributes. Methods define
actions the object can perform.
Objects encapsulate state by storing attributes inside them and provide
behaviour through methods that access or modify those attributes, ensuring
controlled interaction and data protection.
How do objects interact with each other?
In object-oriented programming, objects communicate through message
passing. A message is a method call sent from one object to another to
trigger behaviour.
✓ A message to an object means requesting that object to perform an
action by calling one of its methods.
✓ A message between objects happens when one object sends such a
request to another object to cooperate in completing a task.
VALUES AND DATATYPES
CHARACTER SET
The character set is the set of letters, digits, and special symbols that are
allowed in writing Java programs.
Types of characters in Java
Letters
Uppercase and lowercase alphabets (A–Z, a–z)
Digits
Numeric characters (0–9)
Special symbols
Characters used for operations and structure
Examples: + - * / = < > ( ) { } ; , . _
In programming environments like Java, characters are stored inside the
computer using standard character encoding systems. The two most
important systems are ASCII and Unicode.
ASCII (American Standard Code for Information Interchange)
ASCII is one of the earliest standard character sets used in computers.
✓ Uses 7 bits to represent characters
✓ Can store 128 characters
Extended ASCII uses 8 bits to represent characters, can store 256
characters.
Purpose
ASCII was designed for basic English text communication between
computers.
Limitation
It cannot represent characters from other world languages.
UNICODE (Universal character encoding standard)
Unicode is a modern universal character encoding system.
Key features
✓ Supports thousands of characters
✓ Covers almost all world languages
✓ Includes symbols, emojis, and special signs
✓ Java internally uses Unicode
Purpose
Unicode allows computers to handle global text consistently.
ESCAPE SEQUENCES
Escape sequences are special backslash codes that allow formatting and are
used to represent non-printable or special characters inside strings in Java.
Common escape sequences
Escape sequence Meaning
\n New line
\t Tab space
\b Backspace
\r Carriage return
\" Double quote
\' Single quote
\\ Backslash
TOKENS
A token is the smallest individual unit of a Java program recognized by the
compiler.
TYPES OF TOKENS IN JAVA
✓ Keywords → reserved words (e.g., class, int)
✓ Identifiers → names given by programmers
✓ Literals → fixed values (numbers, characters, strings)
✓ Punctuators (Separators) → punctuation symbols (;, {}, ())
✓ Operators → symbols that perform operations (+, -, *)
Keywords
Keywords are reserved words with predefined meanings. They cannot be
used as names for variables or classes. They define the structure of the
program.
Examples:
class, int, if, else, return, while
Identifiers
Identifiers are user-defined names given to variables, methods, classes, etc.
Examples:
sum, studentName, TotalMark
Naming conventions of Identifiers
1. An identifier may contain letters, digits, underscore (_) and dollar sign
($).
2. An identifier cannot start with a digit.
3. Keywords cannot be used as identifiers.
4. An identifier can be of any length.
5. Whitespace (spaces) is not allowed in an identifier.
6. Java is case-sensitive, so uppercase and lowercase letters are treated
as different.
Literals
Literals are fixed values written directly in a program.
Types include:
✓ Integer literal → 10
✓ Floating literal → 3.14
✓ Character literal → 'A'
✓ String literal → "Hello"
✓ Boolean literal → true /false
Separators (Punctuators)
Separators organize program structure. They separate statements and
blocks.
Examples:
;,(){}[]
Operators
Operators are symbols that perform operations on data.
Examples:
✓ Arithmetic → + - * /
✓ Relational → > < ==
✓ Logical → && ||
FORMS OF OPERATORS
Forms of operators refer to how many operands an operator acts upon. An
operand is a value or variable on which an operator acts.
Three forms of operators
Unary operator
Works on one operand. Changes or operates on a single value.
Examples: ++a, --b, -x
Binary operator
Works on two operands.
Examples: a + b, x * y, p > q
Ternary operator
Works on three operands. Used for decision making.
Syntax : condition ? value1 : value2
Example
int max = (a > b) ? a : b;
Here ?: is a ternary operator.
UNARY OPERATORS
A unary operator works on one operand.
Common unary operators
✓ + → unary plus
✓ - → unary minus
✓ ++ → increment
✓ - - → decrement
Example ++a;
Binary operators
A binary operator works on two operands. These are further divided into
types.
(a) Arithmetic operators
Used for mathematical calculations.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
Example: a + b
(b) Relational operators
Used to compare values. Result is true or false.
Operator Meaning
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater or equal
<= Less or equal
Example: a > b
(c) Logical operators
Used to combine conditions.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Truth tables
Logical AND (&&)
A B A && B
T T T
T F F
F T F
F F F
Logical OR (||)
A B A || B
T T T
T F T
F T T
F F F
Logical NOT (!)
A !A
T F
F T
Ternary operator
Works on three operands and is used for decision making.
Syntax
condition ? value1 : value2
Example
max = (a > b) ? a : b;
new operator
The new operator is used to create objects in memory.
Dot (.) operator
The dot operator is used to access members (variables/methods) of an object
or class.
Hierarchy of operators (precedence)
Operator hierarchy determines the order of evaluation in an expression.
Common order (high → low)
1. Unary (++, --, !)
2. Arithmetic (*, /, %)
3. Arithmetic (+, -)
4. Relational (>, <, ==)
5. Logical (&&, ||)
6. Assignment (=)
Brackets () have highest priority.
Example
5 + 3 * 2 = 11 - Multiplication happens first.
VARIABLES
A variable is a named memory location that holds a value. Variables store
changeable data in a program using a valid name and data type.
Syntax : datatype variable name;
Counter
A counter is a variable used to count how many times something happens.
Example
count = count + 1; Increases step by step (usually by 1).
Use: counting loops, number of items, etc.
Accumulator
An accumulator is a variable used to store a running total.
Example
sum = sum + value; Adds values continuously.
Use: totals, averages, sums.
DATA TYPES
A data type is a classification that specifies the type of data a variable can
hold and the operations that can be performed on it. In Java, data types are
broadly classified into primitive and non-primitive types.
Primitive data types
Primitive data types are predefined data types that store single, simple
values.
Data type Size (bits) Size (bytes) Example
Byte 8 bits 1 byte byte x = 10;
Short 16 bits 2 bytes short n = 200;
Int 32 bits 4 bytes int age = 15;
Long 64 bits 8 bytes long pop = 100000;
float 32 bits 4 bytes float pi = 3.14f;
Non-primitive
double 64 bits 8 bytes double d = 9.81; data types
char 16 bits 2 bytes char ch = 'A';
boolean 8 bits 1 bytes boolean flag = true;
Non-primitive data types store references to objects and can hold multiple
values or behaviors.
✓ String
✓ Array
✓ Classe
✓ Interface
Precedence of operators
Precedence means the priority of operators — which operator is evaluated
first. Higher precedence → evaluated earlier.
Common precedence order (high → low)
Level Operators
Highest ( ) brackets
Unary ++ -- !
Level Operators
Arithmetic * / %
Arithmetic + -
Relational > < >= <= == !=
Logical `&&
Lowest = assignment
Example
5 + 3 * 2 = 11
Multiplication happens before addition.
Associativity of operators
Associativity decides the direction of evaluation when operators have the
same precedence.
✓ Left → Right
✓ Right → Left
Examples
Left to right associativity
10 - 5 - 2
= (10 - 5) - 2
=3
Right to left associativity
a = b = 5;
Assignment happens from right to left.
In Java, output is displayed on the screen using [Link]() and
[Link]().
[Link]()
This statement prints output and moves the cursor to the next line.
Example
[Link]("Hello");
[Link]("World");
Output
Hello
World
Each output appears on a new line.
[Link]()
This statement prints output without moving to a new line.
Example
[Link]("Hello ");
[Link]("World");
Output
Hello World
Output appears on the same line.
Key difference
println() → prints + new line
print() → prints on same line
INPUTS IN JAVA
Three ways we can input values.
1. Direct initialization (Literals)
2. Using methods (Parameters)
3. Scanner class
Initialization means assigning an initial value to a variable before the
program starts executing. The variable starts with a known value.
Eg: int sum = 0;
Parameters are values supplied to a program or method when it runs. Values
are passed during execution.
User input during execution is taken using the Scanner class.
Common Scanner methods
Method Purpose
nextShort() Reads short integer
nextInt() Reads integer
Method Purpose
nextLong() Reads long integer
nextFloat() Reads decimal number
nextDouble() Reads large decimal
next() Reads one word
nextLine() Reads full line
next().charAt(0) Reads a character
These methods allow user data entry during execution.
Types of errors
Errors are mistakes that affect program execution.
Syntax errors
Errors in grammar or rules of Java. Detected at compile time.
Example:
int x = ;
Runtime errors
Errors that occur while the program runs. Program crashes during execution.
Example:
int x = 5 / 0;
Logical errors
Program runs but gives wrong output. Harder to detect.
Example:
area = length + breadth; // wrong formula
Comments in Java
Comments explain code and are ignored by the compiler.
Single-line comment
// This is a comment
Multi-line comment
/* This is
a multi-line
comment */
Improves readability.
PACKAGES
Packages are named collections of related classes grouped according to their
functionality in Java.
Why packages are used
Organize large programs
Avoid name conflicts
Reuse existing classes
Improve readability
Types of packages
Built-in (predefined) packages
These are provided by Java.
Examples:
✓ [Link] → utilities like Scanner
✓ [Link] → basic classes (automatic import)
Java API packages
Java API (Application Programming Interface) packages contain ready-
made classes for programming tasks.
Common API packages:
Package Purpose
[Link] Core language support
[Link] Utility classes & Scanner
[Link] File & input/output
[Link] Graphics & GUI tools
These packages save development time.
[Link]
Contains utility classes. Used for input, collections, and helper tools.
Example: Scanner class for user input.
[Link]
Handles input and output operations. Used for reading/writing data and
files.
[Link]
Core language support. Contains basic classes like String and Math.
Automatically available — no import needed.
[Link]
Used for graphics and GUI (windows, buttons, layouts).
[Link]
Supports network communication. Used for internet and data transfer
programs.
[Link]
Used to create applets (small Java programs run in browsers).
User-defined packages
Created by programmers to group their own classes. Helps organize project
files.
Example:
package school;
MATHEMATICAL LIBRARY METHODS
In Java, mathematical operations are supported by built-in classes provided
in the default package:
[Link]
This package is automatically available in every Java program — no import
statement is required.
It contains useful classes like Math, String, System, etc.
Math Class
The Math class provides methods to perform common mathematical
calculations like powers, roots, rounding, and comparisons. All methods are
static, so they are accessed using:
[Link]()
Important Math Methods
1) pow(x, y)
Returns x raised to the power y. Return type is double
[Link](2, 3) → 8.0
2) sqrt(x)
Returns square root of x. Return type is double
[Link](25) → 5.0
3) cbrt(x)
Returns cube root of x. Return type is double
[Link](27) → 3.0
4) ceil(x)
Rounds up to nearest integer. Return type is double
[Link](3.2) → 4.0
5) floor(x)
Rounds down to nearest integer. Return type is double
[Link](3.9) → 3.0
6) round(x)
Rounds to nearest integer. Return type is integer
[Link](3.5) → 4
7) abs(a)
Returns absolute value. Return type is according to inputs.
[Link](-10) → 10
8) max(a, b)
Returns the larger value. Return type is according to inputs
[Link](5, 9) → 9
9) min(a, b)
Returns the smaller value. Return type is according to inputs
[Link](5, 9) → 5
10) random()
Generates a random number between 0.0 and 1.0. Return type is double
[Link]() → 0.0 to 1.0
Java Expressions Using Math Methods
Java expressions combine:
✔ arithmetic operators → + - * / %
✔ relational operators → > < ==
✔ Method calls
✔ variables and constants
Example Expressions
double result = [Link](16) + [Link](2,3);
int maxVal = [Link](10, 20);
double rounded = [Link](5.3) * 2;
These expressions perform calculations and store results efficiently.
CONDITIONAL STATEMENTS IN JAVA
Conditional statements in Java are decision-making statements that control
the flow of execution of a program. They allow a program to execute specific
blocks of code depending on whether a condition is true or false. These
statements are part of Java’s core library:
[Link]
Conditional constructs make programs logical and intelligent.
1. if Statement
The if statement executes a block of code only when a specified condition is
true. For checking a single condition.
if (condition)
{
statement
}
Example:
int age = 18;
if (age >= 18)
{
[Link]("You are eligible to vote.");
}
2. if-else Statement
The if–else statement executes one block if the condition is true and another
block if it is false. When there are two possible outcomes.
if (condition)
{
Statement1
}
else
Statement2
}
Example:
int age = 16;
if (age >= 18)
{
[Link]("You are eligible to vote.");
else
{
[Link]("You are not eligible to vote.");
}
3. if-else if-else Ladder
This statement checks multiple conditions one after another. The block
corresponding to the first true condition is executed. For multiple decision
making.
if (condition1)
{
Statement1
} else if (condition2)
{
Statement2
else
{
Statement3
}
Example:
int marks = 75;
if (marks >= 90)
{
[Link]("Grade: A");
}
else if (marks >= 75)
{
[Link]("Grade: B");
else
{
[Link]("Grade: C");
4. switch Statement
The switch statement selects and executes one block of code from several
alternatives based on the value of an expression. When comparing one
variable with many fixed values.
switch (expression) {
case value1:
Statement1
break;
case value2:
statement2
break;
default:
Statement
Example:
int day = 3;
switch (day)
{
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid day");
}
Ternary Operator
The ternary operator is a shorthand form of the if–else statement. It
evaluates a condition and returns one of two values. For simple and compact
conditional expressions.
result = (condition) ? value_if_true : value_if_false;
Example:
int age = 20;
String eligibility = (age >= 18) ? "Eligible" : "Not Eligible";
[Link](eligibility);
Important Points:
✓ Use if and if-else for simple conditions.
✓ Use if-else if for multiple conditions.
✓ Use switch when comparing a single variable against multiple possible
values.
✓ Use the ternary operator for concise conditional assignments.
Java provides a feature to terminate the currently running program using the
exit method of the system class.
Syntax : [Link](n) ;
The argument n serves as a status code. A non zero status indicates
abnormal termination and a zero status code indicates a normal termination.
LOOPS
A loop is a control structure that allows a block of code to be executed
repeatedly as long as a given condition is satisfied.
Loops help reduce repetition and make programs efficient and readable.
Looping constructs are part of Java’s core library: [Link]
Parts of a Loop
Every loop generally has three important parts:
1. Initialization
Sets the starting value of the control variable. int i = 1;
2. Condition (Test Expression)
Checks whether the loop should continue. i <= 5
3. Updation (Reinitialization)
Changes the control variable after each iteration. i++;
4. Loop body
Types of Loops in Java
1. for Loop
Used when the number of iterations is known.
Syntax
for(initialization; condition; updation)
{
statements;
Example
for(int i = 1; i <= 5; i++)
{
[Link](i);
2. while Loop
Executes statements while the condition is true.
Syntax
while(condition)
{
statements;
}
Example
int i = 1;
while(i <= 5)
[Link](i);
i++;
}
3. do–while Loop
Executes statements at least once, then checks condition.
Syntax
do
{
statements;
}
while(condition);
Example
int i = 1;
do
{
[Link](i);
i++;
while(i <= 5);
Classification of Loops
Entry Controlled Loop
Condition is checked before execution.
Examples:
✓ for loop
✓ while loop
Exit Controlled Loop
Condition is checked after execution.
Example:
✓ do–while loop
Finite Loop
Runs a fixed number of times and stops.
for(int i = 1; i <= 5; i++)
Infinite Loop
Runs continuously due to missing or incorrect condition.
for(;;)
{
statements;
}
Empty Loop
Loop with no body — ends with a semicolon.
for(int i=1; i<=5; i++);
Step Loop
Loop where the control variable changes by steps other than 1.
for(int i=0; i<=10; i+=2)
Continuous Loop
A loop that runs repeatedly until externally stopped.
while(true)
{
statements;
}
Jumping Statements in Loops
Jumping statements alter normal loop execution.
break
Terminates the loop immediately.
break;
continue
Skips current iteration and continues next cycle.
continue;
return
Exits from the method entirely.
return;
Important Points
✓ Loops repeat execution of statements.
✓ A loop has initialization, condition, and updation.
✓ for and while are entry controlled.
✓ do–while is exit controlled.
✓ Infinite loops never stop unless interrupted.
✓ Jumping statements modify loop flow.