1. What are nested classes in java?

Class declared with in another class is defined as nested class.
There are two types of nested classes in java.
1) Static nested class : static nested class has static keyword declared before class definition
2) Non static nested class : Nested classes without any static keyword declaration
.

2. What are inner classes or non static nested classes in java?

Nested classes without any static keyword declaration in class definition are defined as non static nested
classes. Generally non static nested classes are referred as inner classes.
There are three types of inner classes in java :
1) Local inner class
2) Member inner class
3) Anonymous inner class

3. Why to use nested classes in java?

  • Grouping of related classes : Classes which are not reusable can be defined as inner class instead of creating inner class.
  • Increase encapsulation : Inner class can access private members of outer class.so by creating Getter & Setter methods for private variables , outside world can access these variables. But by creating inner class private variables can be accessed only by inner class.
  • Code readable and maintainable : Instead of creating a new class we can create inner class so that it is easy to maintain.
  • Hiding implementation details: Inner class helps us to hide implementation of class.

4. How to instantiate static nested classes in java?

We can access static members and static methods of outer class without creating any instance of outer
class. As shown in below example InnerStaticClass should be accessed with MainClass.InnerStaticClass

public class MainClass {

	static class InnerStaticClass {
		
	   public void display() {
		   System.out.println("Hello!!!");
	   }
	}
	
	public static void main(String[] args) {
		MainClass.InnerStaticClass inner = new MainClass.InnerStaticClass();
		inner.display();
	}
}

5. Explain about static nested classes in java?

When a static class is defined inside a enclosing class we define that as nested class. Static nested classes
are not inner classes. Static nested classes can be instantiated without instance of outer class.
A static nested doesnot have access to instance variables and non static methods of outer class.

6. Explain about method local inner classes or local inner classes in java?

Nested classes defined inside a method are local inner classes. We can create objects of local inner class
only inside method where class is defined. A local inner classes exist only when method is invoked and
goes out of scope when method returns.

7. Explain about features of local inner class?

1) Local inner class does not have any access specifier.
2) We cannot use access modifiers static for local inner class. But we can use abstract and final for local
inner class.
3) We cannot declare static members inside local inner classes.
4) We can create objects of local inner class only inside method where class is defined.
5) Method local inner classes can only access final variables declared inside a method.
6) Method local inner classes can be defined inside loops(for,while) and blocks such as if etc.

8. Explain about anonymous inner classes in java?

Inner class defined without any class name is called anonymous inner class. Inner class is declared and
instantiated using new keyword.The main purpose of anonymous inner classes in java are to provide
interface implementation. We use anonymous classes when we need only one instance for a class. We can
use all members of enclosing class and final local variables.
When we compile anonymous inner classes compiler creates two files
1) EnclosingName.class
2) EnclsoingName$1.class

9. Explain restrictions for using anonymous inner classes?

1) An anonymous inner class cannot have any constructor because there is no name for class.
2) An anonymous inner class cannot define static methods, fields or classes.
3) We cannot define an interface anonymously.
4) Anonymous inner class can be instantiated only once.

10. Is this valid in java ? can we instantiate interface in java?

Runnable r = new Runnable() {
@Override
public void run() {
}
};
Runnable is an interface.If we see the above code it looks like we are instantiating Runnable interface. But
we are not instantiating interface we are instantiating anonymous inner class which is implementation of
Runnable interface.

11. Explain about member inner classes?

Non static class defined with in enclosing class are called member inner class. A member inner class is
defined at member level of class. A member inner class can access the members of outer class including
private members.
Features of member inner classes :
1) A member inner class can be declared abstract or final.
2) A member inner class can extend class or implement interface.
3) An inner class cannot declare static fields or methods.
4) A member inner class can be declared with public, private, protected or default access.

12. How to instantiate member inner class?

OuterClassName.InnerclassName inner=new OuterClassReference.new InnerClassName();
We cannot instantiate inner class without outer class reference

How to do encapsulation in Java?

Make instance variables private.
Define getter and setter methods to access instance variables

13. What are reference variables in java?

Variables which are used to access objects in java are called reference variables.
Ex : Employee emp=new Employee();
In the above example emp is reference variable.
Reference variable can be of only one type.
A reference variable can point to any number of objects. But if a reference variable is declared final it
can’t point to other objects.
A reference variable can be declared either to a class type or interface type. If a reference variable is
declared with interface type it points to the class that implements the interface.