OOPS using Java – Test 1 Preparation (From
Scratch)
Don’t worry 🙂 Many first-year students face this situation. These notes are exam‑oriented, easy to
understand, and written assuming zero prior knowledge. If you study this properly, you can score
very well in Test‑1.
What is OOPS?
OOPS (Object Oriented Programming System) is a way of writing programs by thinking in terms of
real‑world objects instead of only functions and logic.
Real‑world example
• Object: Student
• Properties (data): name, rollNo, branch
• Actions (behavior): study(), writeExam()
In Java, we represent objects using classes.
UNIT – I
1. Encapsulation
Meaning
Encapsulation means binding data and methods together into a single unit and protecting the
data from direct access.
In simple words: Data hiding + wrapping data and methods together
Real‑world example
Think of a capsule (medicine): - Medicine is inside - You cannot directly touch it - You use it only in the
allowed way
Same in Java: - Variables are kept private - Access is given through methods
Java Example
class Student {
private int marks; // data hidden
public void setMarks(int m) {
1
marks = m;
}
public int getMarks() {
return marks;
}
}
Why Encapsulation is needed?
• Protects data
• Improves security
• Makes code easy to maintain
Exam keywords
data hiding, private variables, getter and setter methods
2. Abstraction
Meaning
Abstraction means showing only important details and hiding internal implementation.
Focus on what an object does, not how it does it
Real‑world example
ATM Machine - You know: withdraw money, check balance - You don’t know: internal processing
How abstraction is achieved in Java?
1. Abstract classes
2. Interfaces
Example using abstract class
abstract class Vehicle {
abstract void start();
}
class Bike extends Vehicle {
void start() {
[Link]("Bike starts with kick");
}
}
2
Key points
• Abstract class can have abstract and non‑abstract methods
• Object of abstract class cannot be created
Difference: Encapsulation vs Abstraction
Encapsulation Abstraction
Data hiding Implementation hiding
Achieved using access modifiers Achieved using abstract classes/interfaces
3. Constructors
Meaning
A constructor is a special method used to initialize objects.
Rules of constructor
• Name must be same as class name
• No return type (not even void)
• Automatically called when object is created
Example
class Student {
int roll;
Student() {
roll = 10;
}
}
Types of constructors
1. Default constructor – no parameters
2. Parameterized constructor – parameters passed
Student(int r) {
roll = r;
}
Why constructors are used?
• To initialize values
• To allocate memory
3
UNIT – II
4. Scope
Meaning
Scope defines where a variable can be accessed in a program.
Types of scope in Java
1. Local Scope
• Declared inside a method
• Accessible only inside that method
void show() {
int x = 10; // local variable
}
2. Instance Scope
• Declared inside class but outside methods
• Each object gets its own copy
class Demo {
int a; // instance variable
}
3. Static Scope
• Declared using static
• Shared among all objects
static int count;
5. Visibility (Access Modifiers)
Visibility decides who can access what.
Access Modifiers in Java
Modifier Same Class Same Package Subclass Outside
private ✔ ✖ ✖ ✖
4
Modifier Same Class Same Package Subclass Outside
default ✔ ✔ ✖ ✖
protected ✔ ✔ ✔ ✖
public ✔ ✔ ✔ ✔
Examples
private int x; // accessible only inside class
public int y; // accessible everywhere
Exam tip
Most secure → private Most accessible → public
IMPORTANT EXAM QUESTIONS (Practice)
1. Define Encapsulation with example
2. Explain Abstraction and its advantages
3. What is a constructor? Explain its types
4. Difference between abstraction and encapsulation
5. Explain scope of variables in Java
6. Write a note on access modifiers
How to Study for Good Score
1. Read one concept at a time
2. Memorize definitions + examples
3. Practice writing code by hand
4. Learn tables (access modifiers)
READY‑TO‑PREPARE EXAM NOTES (2 & 3 MARKS)
Use these answers directly in the exam. Language is simple and scoring‑oriented.
ENCAPSULATION
2‑Mark Answer
Encapsulation is the process of wrapping data and methods into a single unit (class) and hiding
data using access modifiers. It provides data security.
5
3‑Mark Answer
Encapsulation is an OOPS concept in which data members and member functions are combined into a
single unit called a class. Data is protected using access modifiers like private , and access is given
through public methods. This ensures data security and better control.
Example
class Student {
private int marks;
public void setMarks(int m) { marks = m; }
public int getMarks() { return marks; }
}
ABSTRACTION
2‑Mark Answer
Abstraction is the process of hiding implementation details and showing only essential features to
the user.
3‑Mark Answer
Abstraction focuses on what an object does rather than how it does it. In Java, abstraction is achieved
using abstract classes and interfaces. It improves security and reduces complexity.
Example
abstract class Vehicle {
abstract void start();
}
DIFFERENCE: ENCAPSULATION vs ABSTRACTION (VERY
IMPORTANT)
3‑Mark Answer (Table)
Encapsulation Abstraction
Data hiding Implementation hiding
Uses access modifiers Uses abstract class/interface
Protects data Reduces complexity
6
CONSTRUCTORS
2‑Mark Answer
A constructor is a special method used to initialize objects. Its name is same as the class name and it
has no return type.
3‑Mark Answer
A constructor is automatically called when an object is created. It initializes data members of a class.
Constructors can be default or parameterized.
Example
class Student {
Student() {
[Link]("Constructor called");
}
}
TYPES OF CONSTRUCTORS
2‑Mark Answer
1. Default constructor – no parameters
2. Parameterized constructor – parameters are passed
SCOPE OF VARIABLES
2‑Mark Answer
Scope defines the area of a program where a variable can be accessed.
3‑Mark Answer
In Java, scope determines the visibility of variables. Java supports local, instance, and static scope.
TYPES OF SCOPE
3‑Mark Answer
1. Local Scope – inside method
2. Instance Scope – inside class, outside methods
3. Static Scope – shared among all objects
7
VISIBILITY / ACCESS MODIFIERS
2‑Mark Answer
Access modifiers define the accessibility of classes, variables, and methods.
3‑Mark Answer
Java provides four access modifiers: private, default, protected, and public. They control visibility of
members.
Table (VERY IMPORTANT)
Modifier Same Class Same Package Subclass Outside
private ✔ ✖ ✖ ✖
default ✔ ✔ ✖ ✖
protected ✔ ✔ ✔ ✖
public ✔ ✔ ✔ ✔
MOST LIKELY QUESTIONS FOR TEST‑1
✔ Define Encapsulation ✔ Explain Abstraction ✔ What is a constructor? Types ✔ Difference between
Encapsulation and Abstraction ✔ Scope of variables ✔ Explain access modifiers
LAST‑DAY REVISION TIPS
• Learn definitions by heart • Practice tables • Write code neatly • Use keywords: data hiding, security,
initialization
You are fully prepared for 10/10 marks if you study this 💯