Before discussing the most important 36 facts to master encapsulation in java, let us first briefly understand what is encapsulation really is.
The wrapping up of data and functions into a single unit is known as encapsulation. This is one of the strong features of the object oriented approach. The data is not directly accessible to the outside world and only the functions, which are wrapped in the class, can access it. Functions are accessible to the outside world. These functions provide the interface to access data. If one wants to modify the data of an object, s/he should know exactly what functions are available to interact with it. This insulation of the data from direct access by the program is known as data hiding.
|36 facts to Master Encapsulation in Java|
1. Encapsulation is the process of enclosing data and methods that operate on the data in a single unit or capsule.
2. Data hiding is an outcome of encapsulation.
3. Information protection and security are enforced by encapsulation
4. Encapsulation is the complement of abstraction.
5. Encapsulation enables grouping the significant items in a capsule, leaving out the less important ones outside.
6. The accessibility of features of a class are specifically mentioned by an access specifier.
7. Java uses public, private and protected as the three access specifiers.
8. Visibility points out the way by which the derived class draws properties from the base or mother class.
9. Public, protected and private are the three visibility modes.
10. Encapsulation is accomplished by the appropriate use of visibility modifiers.
11. Visibility specifiers are also called access specifiers.
12. An access specifier defines who can use a method or data.
13. The public access specifier indicates that method and data members can be accessed from anywhere within the program.
14. A private access specifier indicates that a method or data member can be accessed only within its class.
15. A protected access specifier shows that a method or data member may be accessed with its own class or subclass of the class.
16. A default access specifier allows access of data members and methods from any class within a package.
17. Scope shows the region within which a variable is accessible.
18. The scope of visibility rules refers to which parts of a program a variable can be seen (i.e., accessed) and used.
19. Data declared at the class level are available to all methods of the class.
20. Data declared within a method or blocks are called local variable.
21. Variables available to an entire class, i.e., having class scope are known as global variables.
22. Local variables may be used only within a method in which they are defined.
23. When a variable is declared within a block, it is available to all methods inside the block.
24. The variable declared in the interior block of a nested block is not visible in the exterior block.
25. The variable declared in the exterior block is visible to the interior block.
26. If a local variable declared has the same name as a global variable, the local variable hides the global variable.
27. Methods (and variables) in a class are either static or instance methods (or variables).
28. A static variable (or method) is a class variable (or method) with one value for all instance of the class.
29. An instance method can only be invoked bound to an object of the class. For example
30. The object employee on which the instance method displaySalary() operates is called (implicit) argument of the method.
31. An instance method is invoked bound to an object which is its (implicit) argument.
32. The employee variable object employee3, serves as an argument variable of the associated method.
33. A variable which is static is an instance variable.
34. A class cannot be declared as private or protected mode. But its variables or methods can.
35. If a class or variable or method is not given any specific access specifier like public, protected or private then the particular item is supposed to have package access.
36. A package access indicates that the particular item may be accessed from any class in the same package.