Encapsulation

Encapsulation

[ICSE Syllabus on this Topic]
private, public, scope and visibility rules. packages and package level access.

Q. Explain the term ‘Encapsulation’ with an example?
Ans: The wrapping up to data and methods into a single units (called class) is known as encapsulation. For example an engine of car or any vehicle contains many small parts, which enables the entire machinery system to work. Encapsulation property hides the inner working of objects from the real world.

Q. What does a class encapsulate?
Ans: A class encapsulate Data Members that contains the information necessary to represent the class and Member Functions that perform operations on the data member.

Q. How does a class enforce information hiding?
Ans: Classes enforce information hiding by means of access specifier.

Q. What is initial class?
Ans: A java program contains many classes. But one class in a Java program contains the main() method. This class is called initial class.

Q. What is Class variable (Static Variable)?
Ans: A data member that is declared once for a class. All objects of that class type, share these data members, as there is single copy of them available in memory. Keyword ‘Static’ in the variable declaration makes a class variable.

Q. What is Instance variable?
Ans: A data member that is created for every objects of the class.

Q. What does the class consists of ?
Ans: A class consists of:
(i) Data members: It contain information necessary to represent that class .
(ii) Methods: It perform operations on the data members of the class.

Q. What is visibility modifiers?
Ans: It is also called access specifier. It defines which function or method is able to use this method.

Q. Define variable’s scope (scope rule)?
Ans: The program parts in which a particular data value (e.g., variable) can be accessed is known as variable’s scope.

Q. Define the term visibility?
Ans: Visibility is a related term which refers to whether one can use a variable from a given place in the program.

Q. Define the term Local variable and Global variable.
Ans: Local Variable: Variable declared inside a method or block.
Global Variable: Class variable which is available to the entire class.

Q. Mention the levels of scope and visibility offered by java?
Ans: (i) Data declared at the class level can be used by all methods in the class.
(ii) Data declared within a method can be used only in the method.
(iii) Data declared within a method is called local data.
(iv) Variable that are declared in block i.e., local variable are available to every method inside of the block.
(v) Variable declared in interior blocks are not available outside of that block.
(vi) Variable declared in exterior blocks are visible to the interior blocks.

Q. Explain the different types of access specifier?
Ans: Access specifier can be of following types:
(a) PUBLIC: It means that any one can call this method.
(b) PRIVATE: It means that only the methods in the same class are permitted to use this method.
(c) PROTECTED: It means that methods in this class and methods in any subclass may access this method.

Q. What are member variables? State their types?
Ans: Member variables are also known as Instance variables. These member variables are used to store value in the class. It may be public, private and protected, where private and protected members remains hidden from outside world and there by support data.

Q. What is meant by private visibility of a method?
Ans: PRIVATE visibility of a Method means that only the methods in the same class are permitted to use this method.

Leave a Comment