Java Interview Questions & Answers Part 5 - Package & Access SpecifiersWhat is a package?
Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.
Which package is imported by default?
java.lang package is imported by default even without a package declaration.
Is package statement mandatory in a Java source file?
It's not mandatory.
What will happen if there is no package for Java source file?
The classes will packaged into a no name default package. In practice, we always put classes into a meaningful package.
What is the impact using a * during importing(for example import java.io.*;?
When a * is used in a import statement, it indicates that the classes used in the current source can be available in the imported package. Using slightly increases the compilation time but has no impact on the execution time.
Can a class declared as private be accessed outside it's package?
A class can't be declared as private. It will result in compile time error.
Can a class be declared as protected?
A class can't be declared as protected. only methods can be declared as protected.
What is the access scope of a protected method?
A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.
What is the impact of marking a constructor as private?
Nobody can instantiate the class from outside that class.
What is meant by default access?
default access means the class,method,construtor or the variable can be accessed only within the package.
Is default a keyword?
Yes. default is a keyword but is associated with switch statement not with access specifiers.
Then how to give default access to a class?
If you dont specify any access, then it means the class is of default access.
Can i give two access specifiers to a method at the same time?
No its not possible. It will result in compile time error.
Pls send your questions, suggestions and feedback to j2eeguide@yahoo.com
© Copyright JGuide 2006, All Rights Reserved.
|