Encapsulation
Encapsulation
Encapsulation
Question 1
1. data
2. methods
3. functionality
Answer
Reason — A class encapsulates characteristics, state (data) and behaviour (methods and
functionality) of an entity in a single unit
Question 2
Through which access specifier, a class makes its element visible to all ?
1. public
2. private
3. protected
4. friendly
Answer
public
Reason — A class makes its element visible to all by using public specifier as a public data member
and method can be accessed by any other class.
Question 3
If a local variable is having the same name as that of a global class element, then it
3. produces an error
Reason — If a local variable is having the same name as that of a global class element, the system
resolves the name to the most local scope available i.e., most local variable with the same name will
be considered. The global variable will be hidden by the local variable.
Question 4
1. global variable
2. local variable
Answer
Reason — Java resolves duplicate variable name to most local scope variable. For example, if a local
variable is having the same name as that of a global class element, then the most local variable with
the same name will be considered. The global variable will be hidden by the local variable.
Question 5
A member method that returns the value of a private data member is called ............... .
1. setter
2. getter
3. manager
4. accessor
Answer
getter, accessor
Reason — Accessor methods are used to read values of private data members of a class which are
not directly accessible in non-member methods.
Question 6
A member method that can change the value of a private data member is called ............... .
1. setter
2. getter
3. manager
4. accessor
Answer
setter
Reason — Setter/mutator methods allow us to change the value of a private data member as private
data members cannot be accessed directly.
Assignment Questions
Question 1
What is encapsulation ?
Answer
The process of wrapping or grouping of data and functions in such a way that they are used as a
single unit is known as encapsulation.
Question 2
Answer
A class encapsulates the characteristics, state and behaviour (data and functions) of an entity.
Question 3
Answer
1. public
2. private
3. protected
4. default
Question 4
Answer
1. public — The public members are accessible in all the classes whether a subclass or any
other class in same package or another package.
2. private — The private members are accessible only inside their own class and nowhere else.
3. protected — The protected members are accessible inside all the classes in their own
package as well as in all subclasses of their class.
4. default — The default members are accessible inside all the classes of the same package.
Question 5
Define scope and visibility.
Answer
Scope refers to the parts of the program where a particular piece of code or data item would be
known and can be accessed.
Visibility is a related term and refers to whether we can use a variable from a given place in the
program.
Question 6(a)
Answer
Question 6(b)
Answer
The scope of a protected class is protected. It can be accessed from all the classes within the same
package as well as from the sub classes in the other packages.
Question 6(c)
Answer
The scope of a default class is friendly or package. It can be accessed from all the classes within the
same package.
Question 6(d)
Answer
A top-level class can't be declared as private. Only inner or nested classes can be private.
Question 7(a)
Answer
A public class is visible to all the classes, whether they are in the same package or in a different
package.
Question 7(b)
Answer
A protected class is visible to all the classes in the same package as well as to the classes outside the
package that inherit the class.
Question 7(c)
Answer
The default class is visible to all the classes in the same package.
Question 7(d)
Answer
A private class is visible only within the same class. It is not visible to any sub-class or the classes in
the same package.
Question 8
How does Java resolve variables having same name? Give code example.
Answer
Java resolve variables having same name to the most local scope available. Therefore, if a local
variable is having the same name as that of a global class element, the most local variable with the
same name will be considered. The global variable will be hidden by the local variable.
test(5);
setGlobal(7);
globalVar = globalVar;
// considered
// "stack"
globalVar = value;
Inside of test :5
First test :0
Inside of test2 :7
Second test :7
Question 9
Answer
Getter methods are used to read values of private data members of a class which are directly not
accessible in non-member methods. They do not modify the data members. They should have
"public" access modifier and return type same as the data type of that instance variable. A getter
method simply returns the instance variable's value.
Setter methods allow us to change the values of an instance variable of a class. They should have
"public" access modifier and "void" return type.
Question 10
Answer
The member methods of a class can be categorized into following three categories :
1. Accessor Methods — These are public member methods of the class that allow us to access
the data members (instance variables) of object. They cannot change the value of data
members. They are used to read values of private data members of a class which are directly
not accessible in non-member methods.
2. Mutator Methods — These member methods allow us to change the data members of an
object. Any member method that changes the values of an instance variable of a class is a
mutator method.
3. Manager Methods — These are member methods with specific methods (e.g., constructors)
that deal with initializing class instances.
Question 11
Answer
1. Data Hiding — Encapsulation enables the programmer to hide desired data by making it
private and give access only to the desired classes / methods. The programmer can hide how
variables and data are stored.
2. Implementation Logic are Hidden — User only knows that to update a data member's value,
call its setter method and to read a data member's value, call its getter method but what
these setter and getter methods are doing, is purely hidden from them.
3. Flexibility — Since implementation details are hidden, it is easier to change the inner logic.
4. More control in programmer's hand — With Java encapsulation, a programmer has full
control over which values are allowed via setter and getter methods. This allows making the
data read-only or write-only as per requirements. It lead to more flexibility and more control
in programmer's hand.