Saturday 16 July 2011

Inner Class Interview Questions

 Q1) What is an inner class?

Ans) Inner class is a class defined inside other class and act like a member of the enclosing class.

Q2) What are the different types of inner classes?

Ans) There are two main types of inner classes –

    Static member class
    Inner class
        Member class
        Anonymous class
        Local class

Q3) What is static member class?

Ans) A static member class behaves much like an ordinary top-level class, except that it can access the static members of the class that contains it. The static nested class can be accessed as the other static members of the enclosing class without having an instance of the outer class. The static class can contain non-static and static members and methods.

public class InnerClass {

      static class StaticInner {

            static int i = 9;
            int no = 6;

            private void method() {}

            public void method1() {}

            static void method2() {}

            final void method3() {}

      }
}

      The static inner class can be accessed from Outer Class in the following manner:
InnerClass.StaticInner staticObj= new InnerClass. StaticInner ();

No outer class instance is required to instantiate the nested static class because the static class is a static member of the enclosing class.

Q4) What are non static inner classes?

Ans) The different type of static inner classes are: Non - static inner classes – classes associated with the object of the enclosing class. Member class - Classes declared outside a function (hence a "member") and not declared "static".
The member class can be declared as public, private, protected, final and abstract. E.g.

public class InnerClass {

    class MemberClass {

    public void method1() { }
    }

}

Method local class – The inner class declared inside the method is called method local inner class. Method local inner class can only be declared as final or abstract. Method local class can only access global variables or method local variables if declared as final

public class InnerClass {

int i = 9;

public void method1() {

final int k = 6;
class MethodLocal {
MethodLocal() {
System.out.println(k + i);
}
}
}
}

Anonymous inner class - These are local classes which are automatically declared and instantiated in the middle of an expression.  Also, like local classes, anonymous classes cannot be public, private, protected, or static. They can specify arguments to the constructor of the superclass, but cannot otherwise have a constructor. They can implement only one interface or extend a class.
Anonymous class cannot define any static fields, methods, or classes, except for static final constants.
Also, like local classes, anonymous classes cannot be public, private, protected, or static

Some examples:

public class MyFrame extends JFrame {

JButton btn = new JButton();
MyFrame() {
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}

Anonymous class used with comparator

List<Parent> l = new ArrayList<Parent>();
l.add(new Parent(2));
l.add(new Parent(3));
Collections.sort(l, new Comparator() {
public int compare(Object o1, Object o2) {
Parent prt1 = (Parent) o1;
Parent prt2 = (Parent) o2;

    if (prt1.getAge() > prt2.getAge()) {

        return -1;
        }else if(prt1.getAge()<prt2.getAge()) {

            return 1;
            } else {

                return 0;
                }
                }
                });

Q5) Does a static nested class have access to the enclosing class' non-static methods or instance variables?

Ans) No .

Q6)What are the advantages of Inner classes?

Ans) The embedding of inner class into the outer class in the case when the inner class is to be used only by one class i.e. the outer class makes the package more streamlined. Nesting the inner class code where it is used (inside the outer class) makes the code more readable and maintainable.

The inner class shares a special relationship with the outer class i.e. the inner class has access to all members of the outer class and still have its own type is the main advantages of Inner class. Advantage of inner class is that they can be hidden from the other classes in the same package and still have the access to all the members (private also) of the enclosing class. So the outer class members which are going to be used by the inner class can be made private and the inner class members can be hidden from the classes in the same package. This increases the level of encapsulation.

If a class A is written requires another class B for its own use, there are two ways to do this. One way is to write a separate class B or to write an inner class B inside class A. Advantage of writing the inner class B in the class A is you can avoid having a separate class. Inner classes are best used in the event handling mechanism and to implement the helper classes. The advantage of using inner class for event handling mechanism is that the use of if/else to select the component to be handled can be avoided. If inner classes are used each component gets its own event handler and each event handler implicitly knows the component it is working for. e.g.

Button btn1 = new Button("Submit");
Btn.addActionListener(new ActionListener(){/br>

    Public void actionPerformed(ActionEvent ae){ submitClicked(); }

} );

The advantage of using static nested class is that to instantiate a static nested class you need not create an instance of the enclosing class which reduces the number of objects the application creates at runtime.

Q7)What are disadvantages of using inner classes?

Ans) 1. Using inner class increases the total number of classes being used by the application. For all the classes created by JVM and loaded in the memory, jvm has to perform some tasks like creating the object of type class. Jvm may have to perform some routine tasks for these extra classes created which may result slower performance if the application is using more number of inner classes. 2. Inner classes get limited support of ide/tools as compared to the top level classes, so working with the inner classes is sometimes annoying for the developer.

Q8) What are different types of anonymous classes?

Ans 1) Plain old anonymous class type one–
e.g.
class superClass{
         void doSomething() {
                  System.out.println(“Doing something in the Super class”);
         }
 }

class hasAnonymous{
               superClass anon = new superClass(){
                       void doSomething() {
                               System.out.println(“Doing something in the Anonymous class”);
                     }
            };
Here anon is the reference which is of type superClass which is the class extended by the anonymous class i.e. superclass of the anonymous class. The method doSomething() is the super class method overridden by the anonymous class.
2) Plain old anonymous class type two –

interface Eatable{
       public void prepareSweets();
 }
class serveMeal{
 Eatable food = new Eatable(){
              public void prepareSweets(){ //come implementation code goes here }
     };
}
 food is reference variable of type Eatable interface which refers to the anonymous class which is the implementer of the interface Eatable. The anonymous implementer class of the interface Eatable implements its method prepareSweets() inside it.
3) Argument defined anonymous class – e.g.

interface Vehicle {
   void getNoOfWheels();
 }
class Car {
       void getType(Vehical v) { }
}
class BeautifulCars {
        void getTheBeautifilCar() {
             Car c = new Car ();
             c.getType (new Vehicle () {
                          public void getNoOfWheels () {
                                 System.out.println("It has four wheels");
                          }
             });
        }
 }
 Anonymous class is defined as the argument of the method getTheBeautifilCar(), this anonymous class is the implementer of the interface Vehicle. The method of class Car getTheBeautifilCar() expects the argument as an object of type Vehicle. So first we create an object of Car referenced by the variable ‘c’. On this object of Car we call the method getTheBeautifilCar() and in the argument we create an anonymous class in place which is the implementer of interface Vehicle hence of type Vehicle.

Q9) If you compile a file containing inner class how many .class files are created and what are all of them accessible in usual way?

Ans) If a inner class enclosed with an outer class is compiled then one .class file for each inner class an a .class file for the outer class is created. e.g.
class EnclosingOuter {
    class Inner{ }
 }
 If you compile the above code with command
% javac EnclosingOuter.java
Two files
EnclosingOuter.class
EnclosingOuter$Inner.class
will be created. Though a separate inner class file is generated, the inner class file is not accessible in the usual way like,
% java EnclosingOuter$Inner

Q10) How to access the inner class from code within the outer class?

Ans) The inner class is instantiated only through the outer class instance.
class EnclosingOuter {
private int noInnerClass = 1;
public void getNoOfInnerClasses(){
           Inner in = new Inner();
System.out.println(“No Of Inner classes is : “+ in.getNoOfClassesFromOuter());
}
class Inner{

public int getNoOfClassesFromOuter(){ return noInnerClass; }

}
Here the method getNoOfInnerClasses() is called on the outer class’s instance through this outer class instance the inner class instance in is created.

Q11) How to create an inner class instance from outside the outer class instance code?

Ans) To create an instance of the inner class you must have the instance of its enclosing class.
e.g. class EnclosingOuter {
class Inner{ }
}
 To create the instance of inner class from class other than the enclosing class.
1) class OtherThanOuter{
EnclosingOuter out = new EnclosingOuter();
EnclosingOuter.Inner in = out.new Inner();
}

2) class OtherThanOuter{
EnclosingOuter.Inner out = new EnclosingOuter.Inner (); }

Q12) How to refer to the outer this i.e. outer class’s current instance from inside the inner class?

Ans) The outer this reference i.e. the outer class’ current instance’ reference can be refered using ‘OuterClassName.this’. E.g
 class EnclosingOuter {
           class Inner{
          System.out.println(“Inner class reference is “ + this); // inner class instance

    System.out.println(“Outer class reference is “ + EnclosingOuter.this); //outer class instance
     }
    }
    To refer the inner class reference from within the inner class use this.

Q13) Which modifiers can be applied to the inner class?

Ans) Following are modifiers that can be applied to the inner:
     public
     private
     abstract
     final
     protected
     strictfp
     static – turns the inner class into static nested class.

Q14) Can the method local inner class object access method’s local variables?

Ans) No, a method local inner class object can not access the method local variable.
Reason: The local variables are not guaranteed to live as long as the local inner class object. The method local variable live on stack and exist only till the method lives, their scope is limited only code inside the method they are declared in. But the local inner class object created within the method lives on heap and it may exist even after the method ends if in case the reference of this local inner class is passed into some other code and is stored in an instance variable. So we can not be sure that the local variables will live till the method local inner class object lives, therefore the method local inner class object can not access the method local variable. To access the method local variables, the variable has to be declared as final.

Q15) Can a method local inner class access the local final variables?Why?

Ans) Yes. Because the final variables are stored on heap and they live as long as the method local inner class object may live.

Q16) Which modifiers can be applied to the method local inner class?

Ans) Only abstract or final keyword isallowed.

Q17) Can a local class declared inside a static method have access to the instance members of the outer class?

Ans) No. There is no this reference available in the static method .The static method class can not have access to any members of the outer class other than static members.

Q18) Can a method which is not in the definition of the superclass of an anonymous class be invoked on that anonymous class reference?

Ans) No. Compilation will fail.As the reference variable type of the anonymous class will be of superclass which will not know of any method defined inside the anonymous class the compilation will fail.
e.g. class SuperClass{
             void doSomething() {
                       System.out.println("In the Super class");
             }
 }
class hasAnonymous{
SuperClass anon = new SuperClass(){
                void doSomething() {
                   System.out.println("In the Anonymous class");
                }
             void doStuff() {
                    System.out.println("An Anonymous class method not present in superClass");
            }
 };

public void doIt(){
      anon.doSomething(); // legal superClass has this method
      anon.doStuff(); // Not legal }
}
 The above code does not compile as the superClass does not know about the anonymous class method doStuff().

Q19) Can an anonymous class define method of its own?

Ans) Yes. But there will be no way by which the methods defined in the anonymous class which are not present in its superclass be invoked. As only those methods which are defined in the suprclass which the anonymous class extends be invoked defining the methods in the anonymous class will be of no use.

Q20) Can an anonymous class implement multiple interfaces directly?

Ans) No. An anonymous class can implement only one interface. If the anonymous class is extending a class then it becomes the implementer of all the interfaces implemented by its superclass automatically.

Q21) Can an anonymous class implement an interface and also extend a class at the same time?

Ans) No. An anonymous class can either extend a class or implement a single interface. If the anonymous class is extending a class then it becomes the implementer of all the interfaces implemented by its superclass automatically.

No comments:

Post a Comment

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com