Translate

Thursday, December 27, 2012

Core Java Interview Questions Part-5


Q) Static methods cannot access instance variables why?
 Static methods can be invoked before the object is created; Instance variables are created only when the new object is created. Since there is no possibility to the static method to access the instance variables. Instance variables are called called as non-static variables.

Q) String & StringBuffer
            String is a fixed length of sequence of characters, String is immutable.
           
            StringBuffer represent growable and writeable character sequence, StringBuffer is mutable which means that its value can be changed. It allocates room for 16-addition character space when no specific length is specified. Java.lang.StringBuffer is also a final class hence it cannot be sub classed. StringBuffer cannot be overridden the equals() method.

Q) Conversions
   String to Int Conversion: -
   int I   = integer.valueOf(“24”).intValue();
                           int x  = integer.parseInt(“433”);
                           float f = float.valueOf(23.9).floatValue();
           
    Int to String Conversion :-
                          String arg = String.valueOf(10);

Q) Super()
            Super() always calling the constructor of immediate super class, super() must always be the first statements executed inside a subclass constructor.

Q) What are different types of inner classes?
A) Nested top-level classes- If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other top-level class. Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. e.g., outer.inner. Top-level inner classes implicitly have access only to static variables. There can also be inner interfaces. All of these are of the nested top-level variety.

Member classes - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested top-level class. The primary difference between member classes and nested top-level classes is that member classes have access to the specific instance of the enclosing class.

Local classes - Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a more publicly available interface. Because local classes are not members the modifiers public,  protected, private and static are not usable.

Anonymous classes - Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.

à Inner class inside method cannot have static members or blocks

Q) Which circumstances you use Abstract Class & Interface?
--> If you need to change your design make it an interface.
--> Abstract class provide some default behaviour, A.C are excellent candidates inside of application framework. A.C allow single inheritance model, which should be very faster.

Q) Abstract Class
Any class that contain one are more abstract methods must also be declared as an abstract, there can be no object of an abstract class, we cannot directly instantiate the abstract classes. A.C can contain concrete methods.
àAny sub class of an Abstract class must either implement all the abstract methods in the super class or be declared itself as Abstract.
à Compile time error occur if an attempt to create an instance of an Abstract class.
à You cannot declare “abstract constructor” and “abstract static method”.
à An “abstract method” also declared private, native, final, synchronized, strictfp, protected.
à Abstract class can have static, final method (but there is no use).
à Abstract class have visibility public, private, protected.
à By default the methods & variables will take the access modifiers is , which is accessibility as package.
à An abstract method declared in a non-abstract class.
à An abstract class can have instance methods that implement a default behavior.
à A class can be declared abstract even if it does not actually have any abstract methods. Declaring such a class abstract indicates that the implementation is somehow incomplete and is meant to serve as a super class for one or more subclasses that will complete the implementation.
à A class with an abstract method. Again note that the class itself is declared abstract, otherwise a compile time error would have occurred.

Abstract class A{
            Public abstract callme();
            Void callmetoo(){
            }
}

class B extends A(
            void callme(){
            }
}

class AbstractDemo{
public static void main(string args[]){
            B b = new B();
            b.callme();
            b.callmetoo();
}
}

No comments: