Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Applications, Class 10, ICSE
What is encapsulation ?
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.
What does a class encapsulate ?
A class encapsulates the characteristics, state and behaviour (data and functions) of an entity.
How does a class enforce information hiding ?
A class enforces data hiding through the following access specifiers:
Name various visibility modifiers in a class.
The various visibility modifiers in a class are as follows:
Define scope and visibility.
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.
What will be the scope of a public class ?
The scope of a public class is public. It can be accessed from anywhere.
What will be the scope of a protected class ?
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.
What will be the scope of a default class ?
The scope of a default class is friendly or package. It can be accessed from all the classes within the same package.
What will be the scope of a private class?
A top-level class can't be declared as private. Only inner or nested classes can be private.
What will be the visibility of a public class ?
A public class is visible to all the classes, whether they are in the same package or in a different package.
What will be the visibility of a protected class ?
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.
What will be the visibility of a default class ?
The default class is visible to all the classes in the same package.
What will be the visibility of a private class ?
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.
How does Java resolve variables having same name? Give code example.
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.
Consider the example given below:
public class VarHiding
{
// global variable "globalVar" is set to 0
public static int globalVar = 0;
public void checking( )
{
test(5);
System.out.println("First test :" + globalVar);
setGlobal(7);
System.out.println("Second test :" + globalVar);
}
public static void test(int globalVar)
// globalVar is local to method test
// test uses the same name as our globalVar variable
{
// All of these refer to local globalVar
globalVar = globalVar;
// Here local variable namely globaLVar will be
// considered
// Global variable having same name i.e.,
// globaLVar remains hidden, here
System.out.println("Inside of test :" + globalVar);
}
public static void setGlobal(int value)
// test two uses a different name. To resolve
// variable globalVar, we must go higher in the
// "stack"
{
// Refers to global globalVar
globalVar = value;
// Here global variable - globalVar will be considered.
System.out.println("Inside of test2 :" + globalVar) ;
}
}
The output produced by checking( ) function is as follows:
Inside of test :5
First test :0
Inside of test2 :7
Second test :7
What are the getter and setter methods?
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.
How many types of methods are generally there in a class ?
The member methods of a class can be categorized into following three categories :
What are the advantages of encapsulation?
The advantages of encapsulation are as follows:
A class encapsulates ............... .
all the above
Reason — A class encapsulates characteristics, state (data) and behaviour (methods and functionality) of an entity in a single unit
Through which access specifier, a class makes its element visible to all ?
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.
If a local variable is having the same name as that of a global class element, then it
hides the global variable
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.
Java resolves duplicate variable name to ................ .
most local scope variable
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.
A member method that returns the value of a private data member is called ............... .
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.
A member method that can change the value of a private data member is called ............... .
setter
Reason — Setter/mutator methods allow us to change the value of a private data member as private data members cannot be accessed directly.