Home
| | Sitemap
||Page number :23
Access Protection
Using three access modifiers “public, private & protected” we can control the visibility of our members in and out of the package and class.
Public
Inside Class Visible Outside Class Visible Inside Package Visible Outside Package Visible
Private
Inside Class Visible Outside Class Not-Visible Inside Package Not-Visible Outside Package Not Visible
Protected
Inside Class Visible Inside Package Visible Outside Package(subclass) Visible Outside Package(non-subclass) Non-Visible
Wrapper Class
Wrapper class is present in the “java.lang” package. Wrapper class was introduced to treat primitives like int, double, float, byte, short etc., as Objects.
For example. Consider this following code:
//This is correct
Vector vec = new Vector();
vec.addElement("Nitish");
//This is wrong, produces Compiler error!!
Vector vec = new Vector();
int num = 5;
vec.addElement(num);
Why the Compiler is complaining? This is because ‘int' is not
an object like String, it's a primitive.
So to make sure int, short, byte, long, float, double, char are
treated as Objects , Wrapper classes where introduced.
If there is no Wrapper class , you can't use primitives when using
Collections FrameWork. Collections FrameWork (java.util)form the main part of Java.They have
Classes like Vector, List, Date, Map, HashSet Iterator, etc., which are used for storing, sorting, and various functions. What in C++ they call STL(Standard Template Library), they call it as 'Collections Framework' in Java.
So for each primitive there is a corresponding Wrapper class. For int ->Integer, float - >Float,double->Double etc., A Wrapper class wraps around a primitive and makes it an Object.
For eg.,
int primInt = 23; Integer wrapInt = new Integer(primInt); Vector vec = new Vector(); vec.addElement(wrapInt);
Since Integer is an Object , it can be added to Vector. As you can see the Integer class wraps around the primitive 'int' variable, so it's called a Wrapper class. Similarly you can change float, double, byte, etc., as Objects using Wrapper class.
Q1. What is a package in Java?
Q2. Name some built in java packages with their uses.
Q3. How does a package help us in organization of our classes?
Q4. How does the Java Runtime System searches for a required package?
Q5. Explain the use of ‘import’ statement with the help of an example.
Q6. Write a program that takes the input from the user. Describe the classes you have used to get the input.
Q7 How access modifiers control the visibility in a package?
Q8. Explain wrapper class.
Q9. How is bytes stream different from Character stream?
Q10. How can we import all the classes of a package in our program?
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29