Encapsulation
ICSE Class 10 · Computer Applications · Chapter 6
What this chapter covers
- Encapsulation and data hiding
- Access specifiers – private, protected and public
- Visibility rules for access specifiers
- Encapsulation in practice: accessor and mutator methods
- Scope of variables
- Class variables
- Instance variables
- Argument variables
- Local variables
- Identifying the kinds of variables in a program
Key points
- Encapsulation binds data members and member methods into one class and restricts direct access to the data from outside it.
- Data hiding is achieved by making data members private and offering public methods to use them.
- Java's access specifier keywords are `private`, `protected` and `public`; writing none gives default (package) access.
- `private`: same class only. Default: same package. `protected`: same package plus subclasses in other packages. `public`: everywhere.
- Access grows wider in the order private, default, protected, public.
- Every member is visible inside its own class; access is checked by class, not by object.
- Access specifiers apply only to members of a class, never to local variables.
- Accessor (getter) methods return a private value; mutator (setter) methods change it after checking it.
- Scope is where a variable can be used by name; lifetime is how long it exists while the program runs.
- A block is a pair of braces, and a variable declared in it is usable only to the end of that block.
- Class variables are `static`, have one shared copy, and last from class loading to the end of the program.
- Instance variables have one copy per object and last as long as the object.
- Argument variables are the formal parameters of a method or constructor and last for one call.
- Local variables are declared inside a method or block, get no default value, and cannot carry an access specifier or `static`.
- When a parameter shadows a field of the same name, `this.field` refers to the field.
Questions answered
Define encapsulation.
What is an access specifier? Name the three access specifier keywords in Java.
Arrange the four access levels from the most restrictive to the least restrictive.
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?
Which access specifier allows a subclass in a different package to use a member, but not an unrelated class in that package?
Why are data members usually made private and methods public in a well-designed class?
Distinguish between an accessor method and a mutator method, with one example of each.
Distinguish between scope and lifetime of a variable.
Past board exam questions
ICSE 2026 board paper · 1 mark
Which of the following is NOT an access specifier?
- private
- protected
- package
- public
Show answer
ICSE 2025 compartment paper · 1 mark
Which of the following access specifiers will make a member accessible only within its own class?
- public
- private
- protected
- default
Show answer
ICSE 2025 board paper · 1 mark
Post office is an example for __________ access specifier.
- public
- local
- protected
- private
Show answer
ICSE 2024 board paper · 1 mark
The access specifier that gives least accessibility is:
- package
- public
- protected
- private
Show answer
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.
- Assertion and Reason both are correct.
- Assertion is true and Reason is false.
- Assertion is false and Reason is true.
- Assertion and Reason both are false.
Show answer
ICSE 2023 sample paper · 1 mark
Wrapping up of data and methods together as one unit is termed as:
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Show answer
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
ICSE 2022 sample paper · 1 mark
Variable that is declared with in the body of a method is termed as:
- Instance variable
- class variable
- Local variable
- Argument variable