Skip to content
Computer ApplicationsICSE Class 10 · Chapter 14

Encapsulation ICSE Class 10 Computer Applications

Updated 14 October 2026 · Fact-checked

Encapsulation is wrapping data and methods into one class and controlling access to the data. Java uses access specifiers: private (visible only inside the class), default with no keyword (inside the package), protected (package plus subclasses) and public (everywhere). ICSE Class 10 also tests the scope of class (static), instance, argument and local variables.

What Encapsulation is about

Encapsulation keeps an object's data safe: other code can change it only through the methods the class allows. In 2024 the board asked which access specifier gives the least accessibility (private), and in 2025 which access specifier a post office (open to all) represents (public). Examiners noted that many candidates chose public for the least accessible specifier, apparently because they had used only public in their programs, and suggested practical examples of each specifier.

The syllabus asks for access specifiers (private, protected, public) with their scope and visibility rules, and the scope of variables: class variables, instance variables, argument variables and local variables.

Weightage: CISCE publishes no chapter weightage. Section A (40 marks) is compulsory: 1-mark MCQs and 2-mark short answers including output and error questions; in Section B you answer any four of six 15-mark programming questions written in Java.Source: CISCE ICSE Computer Applications (661) specimen paper structure for 2027

Key concepts

Meaning and benefits of encapsulation

Encapsulation is the binding of data (variables) and the methods that act on it into a single unit, the class, together with restricting direct access to the data from outside. Usually the data is made private and public methods are provided to read it (getters/accessors) or change it (setters/mutators), which can check the values first (data hiding).

Benefits: (1) data security: outside code cannot set invalid values; (2) control: changes happen only through checked methods; (3) easier maintenance: internal details can change without affecting other code; (4) modularity and reuse.

Real-life example: a medicine capsule; a bank where you cannot touch the vault but can deposit and withdraw through the counter.

Worked example

(2 marks) Define encapsulation and give one benefit.

Try it first, then show the solutionSolution
  1. Binding data and methods into one class and restricting direct access to the data - 1 mark.
  2. Benefit: data security, since data can change only through the class's own checked methods - 1 mark.

Answer: Encapsulation is wrapping data and the methods that operate on it into a single class while restricting direct access to the data. Benefit: the data is protected, because it can be changed only through the class's own methods, which can reject invalid values.

Access specifiers and visibility rules

Access specifiers decide where a class member can be used.

private: accessible only inside the same class. Most restrictive (least accessible).
default (no specifier, 'package' access): accessible to any class in the same package.
protected: accessible in the same package and in subclasses (even in other packages).
public: accessible from everywhere. Least restrictive.

Visibility table (accessible = yes):

SpecifierSame classSame packageSubclass in another packageAny other class
privateyesnonono
defaultyesyesnono
protectedyesyesyesno
publicyesyesyesyes

Order from least to most accessible: private < default < protected < public.

Worked example

(1 mark) The access specifier that gives the least accessibility is:
(a) package
(b) public
(c) protected
(d) private

Try it first, then show the solutionSolution
  1. private allows access only within the class itself.

Answer: (d) private

Getters and setters

A common encapsulation pattern:
class Student
{
private int marks;
public void setMarks(int m)
{
if (m >= 0 && m <= 100)
marks = m;
else
System.out.println("Invalid marks");
}
public int getMarks()
{
return marks;
}
}

Another class cannot write s.marks = 150; (marks is private: compile-time error). It must call s.setMarks(150), which rejects the value. getMarks() lets others read the value safely. setMarks is an impure (mutator) method; getMarks is a pure (accessor) method.

Worked example

(2 marks) Another class has Student s = new Student(); and writes s.marks = 95;. Why is this an error, and how should it be done?

Try it first, then show the solutionSolution
  1. marks is private, so it is not visible outside Student: compile-time error - 1 mark.
  2. Use the public setter: s.setMarks(95); - 1 mark.

Answer: Because marks is private, it cannot be accessed outside the Student class, so the compiler reports an error. Use the public method: s.setMarks(95);.

Scope of variables

Scope is the part of a program in which a variable can be used.

Class (static) variables: declared with static in the class; one copy shared by all objects; exist as long as the class is loaded; usable throughout the class (and outside, depending on access).
Instance variables: declared in the class without static; one copy per object; exist as long as the object exists; usable in all non-static methods of the class.
Argument (parameter) variables: declared in a method's header; exist only while that method runs; usable only inside that method.
Local variables: declared inside a method or a block; usable only from the point of declaration to the end of that block; they have no default value and must be assigned before use. A variable declared in a for header (for (int i = ...)) is local to the loop.

If a local or argument variable has the same name as a member variable, inside that method the local one hides (shadows) the member.

Worked example

(2 marks) Identify the class, instance, argument and local variables:
class Shop
{
static int count;
double sales;
void sell(double amt)
{
double tax = amt * 0.05;
sales += amt + tax;
count++;
}
}

Try it first, then show the solutionSolution
  1. Class variable: count (static). Instance variable: sales - 1 mark.
  2. Argument variable: amt. Local variable: tax - 1 mark.

Answer: Class variable: count. Instance variable: sales. Argument variable: amt. Local variable: tax.

Scope in action

class Scope
{
int x = 10; // instance variable
void show(int x) // argument x hides the member x
{
int y = x * 2; // local variable
System.out.println(x + " " + y);
}
void showMember()
{
System.out.println(x);
}
public static void main(String args[])
{
Scope s = new Scope();
s.show(3);
s.showMember();
}
}
Output:
3 6
10

In show(), x means the argument (3); in showMember(), x means the instance variable (10). y cannot be used outside show().

Worked example

(2 marks) Why is this an error? for (int i = 1; i <= 5; i++) { } System.out.println(i);

Try it first, then show the solutionSolution
  1. i is declared in the for header, so its scope is the loop only - 1 mark.
  2. After the loop i does not exist: compile-time error; declare int i; before the loop to use it later - 1 mark.

Answer: i is local to the for loop, so it cannot be used after the loop (cannot find symbol). Declare it before the loop: int i; for (i = 1; i <= 5; i++) { } System.out.println(i); which prints 6.

Encapsulation in real life

Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

public access

Suppose a post office counter can be used by anyone in the town to send letters, without any membership.

Which access specifier does it represent and why?

Try it first, then show the solutionSolution
  1. Anyone, from anywhere, can use it.
  2. That matches public: accessible from all classes.

Answer: public, because it is open to everyone without restriction.

Memory hook: Public is for the public.

private access

Imagine your diary kept in your locked drawer: only you can read or write it.

Which specifier is this and what is the Java equivalent?

Try it first, then show the solutionSolution
  1. Only the owner (the class itself) has access.
  2. private int pages; inside class Diary.

Answer: private: accessible only within its own class.

Memory hook: Private stays in its own room.

protected access

Suppose a family recipe is shared with everyone in the household (same package) and with the children even after they move to their own homes (subclasses elsewhere), but not with neighbours.

Which access specifier fits?

Try it first, then show the solutionSolution
  1. Shared within the household: package access.
  2. Also shared with descendants elsewhere: subclasses.
  3. Not with unrelated outsiders.

Answer: protected: same package plus subclasses in other packages.

Memory hook: Protected = family, wherever they live.

Topics in this chapter

Encapsulation: syntax and key facts

Visibility order
private < default (package) < protected < public
private = least accessible.
Encapsulation pattern
private data + public getX() / setX(value with checks)
Data hiding.
Scope
class (static) > instance (per object) > argument (method call) > local (block)
Local variables have no default value.

How the board asks Encapsulation

The question patterns to expect, and the method that works for each.

Question typeHow it is askedMethod
MCQ (Section A)Least accessible specifier (2024); real-life example of public, the post office (2025).Rank private < default < protected < public; match openness.
Short answer (2 marks)Classify variables by scope; explain an access error; define encapsulation.Look where each variable is declared; check the specifier of the member being used.
Program (15 marks)Class with private data members and public methods (accept, calculate, display).Members private, methods public, object in main() calls only the methods.

Tricks to remember Encapsulation

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Pri-De-Pro-Pub

    Private, Default, Protected, Public: from the tightest lock to the open door.

    Use it when: Order of accessibility.

  • CIAL scope

    Class, Instance, Argument, Local: from the longest life to the shortest.

    Use it when: Types of variables by scope.

Encapsulation: board-level solved questions

Ordered from board level to HOTS. Cover the solution, write your own, then compare step by step.

Example 1: Post office · 1 mark · MCQBoard level

A post office is an example of which access specifier?
(a) public
(b) local
(c) protected
(d) private

Try it first, then show the solutionSolution
  1. Open to everyone: public.

Answer: (a) public

Example 2: Assertion and reason: protected · 1 mark · Assertion-reasonHarder board level

Assertion (A): A protected member can be accessed by a subclass in another package.
Reason (R): A protected member is accessible only within its own class.
(a) Both A and R are true and R is the correct explanation of A
(b) Both A and R are true but R is not the correct explanation of A
(c) A is true but R is false
(d) A is false but R is true

Try it first, then show the solutionSolution
  1. A is true. R describes private, not protected: false.

Answer: (c) A is true but R is false

Example 3: Scope output · 2 marks · Very short answerHarder board level

Give the output of the Scope class (instance x = 10; show(int x) prints x and x * 2; showMember() prints x) when main calls s.show(3); s.showMember();

Try it first, then show the solutionSolution
  1. In show(3), the argument x = 3 hides the member: prints 3 6.
  2. showMember() uses the instance x: prints 10.

Answer: 3 6
10

Example 4: Local variable without value · 2 marks · Very short answerHarder board level

Why does this not compile? void f() { int total; total = total + 5; System.out.println(total); } Correct it.

Try it first, then show the solutionSolution
  1. Local variables get no default value; total is used before it is assigned.
  2. Initialise it: int total = 0;

Answer: Local variable total might not have been initialised (compile-time error). Correct: int total = 0; then the method prints 5.

Example 5: Bank account (Section B) · 15 marks · Long answerHarder board level

Define a class Account with private members accNo (int) and balance (double). Provide public methods: void open(int n, double amt) to set them; void deposit(double amt) for positive amounts; void withdraw(double amt) only if the balance is enough, else print 'Insufficient balance'; double getBalance(). In main(), open account 101 with ₹5,000, deposit ₹2,000, withdraw ₹4,000, try to withdraw ₹5,000, and print the balance.

Try it first, then show the solutionSolution
  1. class Account
    {
    private int accNo;
    private double balance;
    public void open(int n, double amt)
    {
    accNo = n;
    balance = amt;
    }
  2. public void deposit(double amt)
    {
    if (amt > 0)
    balance = balance + amt;
    }
    public void withdraw(double amt)
    {
    if (amt <= balance)
    balance = balance - amt;
    else
    System.out.println("Insufficient balance");
    }
  3. public double getBalance()
    {
    return balance;
    }
    public static void main(String args[])
    {
    Account a = new Account();
    a.open(101, 5000);
    a.deposit(2000);
    a.withdraw(4000);
    a.withdraw(5000);
    System.out.println("Balance = " + a.getBalance());
    }
    }
  4. Trace: 5000.0 → 7000.0 → 3000.0 → 5000 > 3000, so 'Insufficient balance' and no change.
  5. balance is private, so no other class can set it to an invalid value directly.

Answer: Output:
Insufficient balance
Balance = 3000.0

Example 6: Shared and individual counters · 2 marks · Short answerHOTS / competency

Give the output:
class Ticket
{
private static int sold = 0;
private int myNo;
public void buy()
{
sold++;
myNo = sold;
}
public int getNo() { return myNo; }
public static int getSold() { return sold; }
public static void main(String args[])
{
Ticket t1 = new Ticket(), t2 = new Ticket(), t3 = new Ticket();
t2.buy(); t1.buy(); t3.buy();
System.out.println(t1.getNo() + " " + t2.getNo() + " " + t3.getNo() + " " + Ticket.getSold());
}
}

Try it first, then show the solutionSolution
  1. Order of buying: t2 gets 1, t1 gets 2, t3 gets 3.
  2. sold is shared: 3.

Answer: 2 1 3 3

Example 7: Temperature class with validation · 15 marks · Long answerHOTS / competency

Define a class Thermostat with private double temp. Write setTemp(double t) that accepts only values from 16 to 30 (otherwise prints 'Out of range' and keeps the old value), getTemp(), and main() that sets 24, then tries 35, then prints the temperature. Explain how encapsulation helps.

Try it first, then show the solutionSolution
  1. class Thermostat
    {
    private double temp = 20;
    public void setTemp(double t)
    {
    if (t >= 16 && t <= 30)
    temp = t;
    else
    System.out.println("Out of range");
    }
  2. public double getTemp()
    {
    return temp;
    }
    public static void main(String args[])
    {
    Thermostat th = new Thermostat();
    th.setTemp(24);
    th.setTemp(35);
    System.out.println("Temperature = " + th.getTemp());
    }
    }
  3. Trace: 24 is accepted; 35 is rejected with 'Out of range'; temp stays 24.0.
  4. Encapsulation: temp is private, so no code can set an unsafe value directly; the setter checks every change.

Answer: Output:
Out of range
Temperature = 24.0
Encapsulation hides temp and allows changes only through setTemp(), which rejects invalid values.

Case-study practice (Section B style)

Read the situation, then answer every part. In the board paper this is a 10-mark question split into 2 + 3 + 5 marks.

The school fee system

Imagine a class Fee with public double amount; and a method pay(). A student finds that another class can write f.amount = -5000; and the system then shows a refund. The developer decides to change the design.

  1. (i) · 2 marks

    Which principle has the developer ignored, and what is the risk?

    Answer

    Answer: Encapsulation (data hiding): amount is public, so any class can set an invalid value such as a negative fee.

    Principle 1 mark; risk 1 mark.

  2. (ii) · 3 marks

    Rewrite the declaration and add a safe setter.

    Answer

    Answer: private double amount;
    public void setAmount(double a)
    {
    if (a >= 0) amount = a;
    else System.out.println("Invalid fee");
    }

    private 1 mark; setter with check 2 marks.

  3. (iii) · 5 marks

    Explain the four access levels and state which one a subclass in another package can use, giving five points in all.

    Answer

    Answer: (1) private: same class only. (2) default: same package. (3) protected: same package and subclasses anywhere. (4) public: everywhere. (5) A subclass in another package can use protected and public members, but not private or default ones.

    1 mark each.

Where the marks are

How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.

Classify variables as class, instance, argument and local. (2 marks) (2 marks)

StepMarks
Class (static) and instance variables identified.1
Argument and local variables identified.1

Encapsulated class with private data and public methods (Account). (15 marks) (15 marks)

StepMarks
Private data members.2
open()/setter assigning values.2
deposit() with validation.3
withdraw() with balance check and message.3
getBalance() returning the value.2
main(): object and calls in order; output.3

Where students lose marks in Encapsulation

The slips examiners see most often, and what to write instead.

  • Mistake: Choosing public as least accessible

    The 2024 examiners noted candidates answered public, apparently because they used only public in programs.

    Do this: private is the least accessible; public the most.

    Usually costs: 1 mark.

  • Mistake: Choosing protected for open-to-all examples

    In 2025 candidates chose protected for the post office.

    Do this: Open to everyone = public.

    Usually costs: 1 mark.

  • Mistake: Using a local variable outside its block

    Loop variables declared in for headers are often printed after the loop.

    Do this: Declare the variable before the loop if it is needed afterwards.

    Usually costs: 1 mark.

  • Mistake: Accessing private members from another class

    Students write ob.balance when balance is private.

    Do this: Use public getter or setter methods.

    Usually costs: 1 to 2 marks.

Practice questions on Encapsulation

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which specifier makes a member accessible to all classes?
    (a) private
    (b) protected
    (c) public
    (d) default

    Hint

    Open.

    Answer

    (c) public

  2. Q2 · 1 mark · MCQBoard level

    A variable declared inside a method is a:
    (a) class variable
    (b) instance variable
    (c) local variable
    (d) static variable

    Hint

    Inside.

    Answer

    (c) local variable

  3. Q3 · 1 mark · MCQHarder board level

    A member with no access specifier can be accessed by:
    (a) every class
    (b) classes in the same package
    (c) only its own class
    (d) only subclasses

    Hint

    Package access.

    Answer

    (b) classes in the same package

  4. Q4 · 2 marks · Very short answerBoard level

    Distinguish between private and public access.

    Hint

    Where visible.

    Answer

    A private member is accessible only within its own class; a public member is accessible from any class anywhere.

  5. Q5 · 2 marks · Very short answerHarder board level

    Distinguish between an instance variable and a local variable on two points.

    Hint

    Where declared; default value.

    Answer

    (1) An instance variable is declared in the class outside methods and lasts as long as the object; a local variable is declared inside a method or block and lasts only while it runs. (2) Instance variables get default values (0, null); local variables must be assigned before use.

  6. Q6 · 2 marks · Short answerHOTS / competency

    Why are data members usually private and methods public?

    Hint

    Control.

    Answer

    Private data cannot be changed directly from outside, so the class keeps control and can validate every change; public methods provide the controlled ways for other classes to use the object.

  7. Q7 · 15 marks · Long answerBoard level

    Define a class Rectangle with private length and breadth, a public method set(double l, double b) that accepts only positive values, and public methods area() and perimeter(). Write main() to set 6 and 4 and print both.

    Hint

    Validate in set().

    Answer

    class Rectangle
    {
    private double length, breadth;
    public void set(double l, double b)
    {
    if (l > 0 && b > 0)
    {
    length = l;
    breadth = b;
    }
    else
    System.out.println("Invalid sides");
    }
    public double area() { return length * breadth; }
    public double perimeter() { return 2 * (length + breadth); }
    public static void main(String args[])
    {
    Rectangle r = new Rectangle();
    r.set(6, 4);
    System.out.println("Area = " + r.area());
    System.out.println("Perimeter = " + r.perimeter());
    }
    }
    Output:
    Area = 24.0
    Perimeter = 20.0

  8. Q8 · 2 marks · Short answerHOTS / competency

    Give the output:
    class Q
    {
    int n = 5;
    void m()
    {
    int n = 8;
    n++;
    System.out.print(n + " ");
    }
    public static void main(String args[])
    {
    Q q = new Q();
    q.m();
    System.out.println(q.n);
    }
    }

    Hint

    Local hides member.

    Answer

    9 5 (the local n becomes 9; the instance n stays 5)

  9. Q9 · 2 marks · Very short answerBoard level

    State the scope of an argument variable.

    Hint

    Method.

    Answer

    An argument (parameter) variable can be used only inside the method in whose header it is declared, and exists only while that method is running.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Most restrictive specifier?Show answer

    private

  2. 2.Which specifier allows subclasses in other packages?Show answer

    protected (and public)

  3. 3.Do local variables get default values?Show answer

    No.

  4. 4.Scope of a variable declared in a for header?Show answer

    The loop only.

  5. 5.What is data hiding?Show answer

    Keeping data private and allowing access only through methods.

Best order to study it

  1. Meaning and benefits of encapsulation.
  2. private, default, protected, public: visibility table.
  3. Getters and setters with validation.
  4. Scope of class, instance, argument and local variables.
  5. Shadowing and block scope traces.

Quick revision checklist

The night before the exam: can you do each of these without looking?

  • Encapsulation: data + methods in a class; data hidden.
  • private (class) < default (package) < protected (package + subclasses) < public (all).
  • Getters read, setters change with checks.
  • Class (static), instance, argument, local variables.
  • Local variables: no default value; scope ends with the block.

Encapsulation: frequently asked questions

Is 'default' an access specifier keyword?

No keyword is written; leaving out the specifier gives default (package) access.

Can a class be private?

A top-level class can be only public or default; the syllabus focuses on members.

Is encapsulation the same as abstraction?

No. Abstraction shows only essential features; encapsulation bundles data with methods and hides the data. They work together.

Based on: CISCE ICSE 2027 Computer Applications syllabus; CISCE Analysis of Pupil Performance ICSE 2025: Computer Applications (with the 2025 board paper)