[Tech.][Struts.][Servlet.][JSP.][EJB.][JNDI-JMS][SQL.][JDBC.]
CORE-JAVA: [OOP][CLASS][ABSTRACT][EXCEPTIONS][THREADS][UTILS][PACKAGES][JVM][CASTING][NETWORKING][RMI-XML]
Controlling Access to Members of a Class.
* State the significance of PUBLIC, PRIVATE, PROTECTED, DEFAULT modifiers both singly and in combination.
State the effect of package relationships on declared items qualified by these modifiers. (packages)
A: PUBLIC class is visible in other packages, field is visible everywhere (class must be public too)
PRIVATE variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
PROTECTED Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature. This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
DEFAULT is what we get by default ie, without any access modifier. It means that it is visible to all within a particular package.
What is the difference between a public and a non-public class? (PACKAGE)
Access Levels Modifier Class Same Package Subclass of any package World /Other Package public
Y Y Y Y protected
Y Y Y N no modifier Y Y N N private
Y N N N
A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.
What is a Java package and how is it used?
A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces. (FAQ)
What modifiers may be used with a top-level class?
A top-level class may be PUBLIC, ABSTRACT, or FINAL.
If a class is declared without any access modifiers, where may the class be accessed?
A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the SAME PACKAGE.
If a variable is declared as private, where may the variable be accessed?
A private variable may only be accessed within the SAME CLASS in which it is declared.
Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?
A: No we have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it's subpackage. (Managing)
Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution. To put it in code: long start = System.currentTimeMillis (); method (); long end = System.currentTimeMillis (); System.out.println ("Time taken for execution is " + (end - start)); Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amount of processing.
If a method is declared as PROTECTED, where may the method be accessed?
A protected method may only be accessed by classes or interfaces of the SAME PACKAGE or by SUB-CLASSES of the class in which it is declared (same or other package).
What modifiers can be used with a LOCAL INNER CLASS?
OUTER classes can only be declared Public or package private (Default). A local-INNER-class may be FINAL or ABSTRACT with same modifiers as of the outer class. Non-Static INNER classes can be final, abstract, public, private, and protected. Static INNER classes do not have access to OUTER members. (Local-Inner)
* Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD compile?
A: Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying, can not resolve symbol: class ABCD location: package io import java.io.ABCD;
* Can a top level class be private or protected?
A: No. A top level class (outer class) can not be private or protected. It can have either "PUBLIC" or no modifier, DEFAULT (package private). Thus outer classes can only be declared public or package private. If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected. (the default, also known as package-private)
Where and how can you use a private constructor?
A: Private constructor can be used if you do not want any other class to instantiate the object , the instantiation is done from a static public method, this method is used when dealing with the factory method pattern when the designer wants only one controller (factory method ) to create the object.
What restrictions are placed on the location of a package statement within a source code file?
A package statement must appear as the first line in a source code file (excluding blank lines and comments).
Which package is always imported by default?
The java.lang package is always imported by default.
What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import java.net.* versus import java.net.Socket)?
It makes NO DIFFERENCE in the generated class files since only the classes that are actually used are referenced by the generated class file. There is another practical benefit to importing single classes, and this arises when two (or more) packages have classes with the SAME NAME. For Example java.util.Timer and javax.swing.Timer, for example. If I import java.util.* and javax.swing.* and then try to use "Timer", I get an error while compiling (the class name is ambiguous between both packages). The class we need was the javax.swing.Timer class, and the only classes we plan on using in java.util are Collection and HashMap. In this case, some people will prefer to import java.util.Collection and import java.util.HashMap instead of importing java.util.*. This will now allow them to use Timer, Collection, HashMap, and other javax.swing classes without using fully qualified class names.
*[Life-is-full-of-Tests.] [Things-become-Inevitable!] [Realizations-In-Real-Life.]
Realizations. Realizations-In-Real-Life. Amazing-Constructive-Vibrations. Astrology. Creating-Leaders. How-ideas-are-concluded-and-decisions-are-materialized. Relationships-&-Partnerships. The-path-of-victory. An-attempt-for-definition. Speak-with-a-heart. These-are-contagious. Happy-kingdom. MIRACLES. Better-to-create-one ! Cast-God-Religion ! Things-become-inevitable ! We-are-all-looking-for! Phase-of-Life. Destiny-Karma-and-God. Struggle-Perfection-Money. Independence-and-Freedom. Relationships-and-Happiness.
No comments:
Post a Comment