LIWA

Encapsulation

ICSE Class 10 · Computer Applications · Chapter 6

What this chapter covers

Key points

Questions answered

Define encapsulation.
Encapsulation is the wrapping up of data members and the member methods that operate on them into a single unit, the class, together with restricting direct access to the data from outside the class.
What is an access specifier? Name the three access specifier keywords in Java.
An access specifier is a keyword written before a member of a class that decides which parts of the program may use that member. The three keywords are `private`, `protected` and `public`.
Arrange the four access levels from the most restrictive to the least restrictive.
private, default (no keyword), protected, public.
A class `A` and a class `B` are in the same package, and `B` does not extend `A`. Which members of `A` can `B` use?
`B` can use the default, protected and public members of `A`, but not its private members.
Which access specifier allows a subclass in a different package to use a member, but not an unrelated class in that package?
`protected`.
Why are data members usually made private and methods public in a well-designed class?
Private data members cannot be changed directly by other classes, so every change has to pass through the public methods, which can check the new value. This protects the object from being put into an invalid state, while the public methods give other classes the operations they need.
Distinguish between an accessor method and a mutator method, with one example of each.
An accessor returns the value of a private data member without changing it, for example `getBalance()`. A mutator changes the value of a private data member, usually after validating it, for example `deposit(double amt)` or `setTemp(double t)`.
Distinguish between scope and lifetime of a variable.
Scope is the region of the program text in which a variable can be referred to by name. Lifetime is the period during execution for which the variable exists in memory. A private class variable, for example, has its class as its scope but lasts for the whole run of the program.

Past board exam questions

ICSE 2026 board paper · 1 mark

Which of the following is NOT an access specifier?

  1. private
  2. protected
  3. package
  4. public
Show answer
package

ICSE 2025 compartment paper · 1 mark

Which of the following access specifiers will make a member accessible only within its own class?

  1. public
  2. private
  3. protected
  4. default
Show answer
private

ICSE 2025 board paper · 1 mark

Post office is an example for __________ access specifier.

  1. public
  2. local
  3. protected
  4. private
Show answer
public

ICSE 2024 board paper · 1 mark

The access specifier that gives least accessibility is:

  1. package
  2. public
  3. protected
  4. private
Show answer
private

ICSE 2024 board paper · 1 mark

Assertion (A): Static method can access static and instance variables. Reason (R): Static variable can be accessed only by static method.

  1. Assertion and Reason both are correct.
  2. Assertion is true and Reason is false.
  3. Assertion is false and Reason is true.
  4. Assertion and Reason both are false.
Show answer
Assertion and Reason both are false.

ICSE 2023 sample paper · 1 mark

Wrapping up of data and methods together as one unit is termed as:

  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction
Show answer
Encapsulation

ICSE 2023 sample paper · 2 marks

Consider the following program and answer the questions given below: ```java class sample { int a, b; sample(int x, int y) { a = x; b = y; } void calculate() { int z; z = a+b; System.out.println(z); } } ``` (a) Name the global variables. (b) What are the method variables?

Show answer
(a) The global (instance) variables are a and b. (b) The method (local) variable is z, declared in calculate(); x and y are the parameters of the constructor sample(int x, int y).

ICSE 2022 sample paper · 1 mark

Variable that is declared with in the body of a method is termed as:

  1. Instance variable
  2. class variable
  3. Local variable
  4. Argument variable
Show answer
Local variable