Google Search

Wednesday, July 15, 2009

JAVA INTERVIEW QUESTION PART4


37.How are this() and super() used with constructors?
· Constructors use this to refer to another constructor in the same class with a different parameter list.
· Constructors use super to invoke the superclass's constructor. If a constructor uses super, it must use it in the first line; otherwise, the compiler will complain.

38.What are Access Specifiers?
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifiers..

39.What are Access Specifiers available in Java?
Java offers four access specifiers, listed below in decreasing accessibility:
· Public- public classes, methods, and fields can be accessed from everywhere.
· Protected- protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
· Default(no specifier)- If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
· Private- private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.
40.What is final modifier? The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method. · final Classes- A final class cannot have subclasses. · final Variables- A final variable cannot be changed once it is initialized. · final Methods- A final method cannot be overridden by subclasses.
41.What are the uses of final method? There are two reasons for marking a method as final: · Disallowing subclasses to change the meaning of the method. · Increasing efficiency by allowing the compiler to turn calls to the method into inline Java code. 42.What is static block? Static block which exactly executed exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute. 43.What are static variables? Variables that have only one copy per class are known as static variables. They are not attached to a particular instance of a class but rather belong to a class as a whole. They are declared by using the static keyword as a modifier. static type varIdentifier; where, the name of the variable is varIdentifier and its data type is specified by type. Note: Static variables that are not explicitly initialized in the code are automatically initialized with a default value. The default value depends on the data type of the variables.