Java Innerclass Interview Questions :-


  1. What is an inner class?

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


  2. What are the advantages of Inner classes?

There are main advantages of Inner classes :-

1. With inner classes you can access private members of the enclosing class.

2. They are useful for interface implementations that are only used by the enclosing class (event handlers in a application)

3. They are useful for providing fine grained access and creation control over an interface implementation that is retrieved externally (maybe something like an Iterator implementation)


  3. What are disadvantages of using inner classes?

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.

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.

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.


  4. What are the different types of inner classes?

There are two main types of inner classes :-

1. Static member class.

2. Inner class

   (a). Member class

   (b). Anonymous class

   (c). Local class


  5. What is static member class?

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.


  6. What are non static inner classes?

The different type of static inner classes are :- Non - static inner classes Ė classes associated with the object of the enclosing class.

1. Member class :- Classes declared outside a function (hence a "member") and not declared "static".

Note :- The member class can be declared as public, private, protected, final and abstract.

2. 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

3. 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.

Note :- 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


  7. What are the difference between Inner class and nested static class in Java.

Difference between Inner class and nested static class in Java :-

1.Inner class require instance of outer class for initialization and they are always associated with instance of enclosing class. On the other hand nested static class is not associated with any instance of enclosing class.

2. Another difference between Inner class and nested static class is that later uses static keyword in there class declaration, which means they are static member of class and can be accessed like any other static member of class.

3. Nested static class can be imported using static import in Java.

4. One last difference between Inner class and nested static class is that later is more convenient and should be preferred over Inner class while declaring member classes.


  8. Can we declare local inner classes as static?

No. Local inner classes canít be static.


  9. Can we declare local inner classes as private or protected or public?

No. Local inner classes canít be declared with access modifiers.They canít be private or protected or public.


  10. What is the condition to use local variables inside a local inner class?

The condition is that local variables must be final. We canít use non-final local variables inside a local inner class.


  11. Can member inner classes have static members in them?

No, member inner classes canít have static members in them. They can have only non-static members. But, exception being the static and final field. i.e member inner class can have static and final field, but it must be initialized at the time of declaration only.


  12. How will you reference the inner class?

To reference it you will have to use OuterClass$InnerClass


  13. Can an inner class be defined inside a method?

Yes it can be defined inside a method and it can access data of the enclosing methods or a formal parameter if it is final .


PHP

PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language..

Read more