Java Interview Question on Inner classes

In the previous article, I wrote about Java Abstract Class and Interface Interview Questions. In this article, i am going to share Java Interview Question on Inner classes frequently asked on core Java Interview. You can expect many questions from this topic in the interview. This topic is one of the favorites of the interviewer.

Q1. What do you understand by inner class in java?
Ans:
The class in java which is defined inside another class is called an inner class.

Q2. How many types of nested classes are there in java?
Ans:
There are 2 nested classes supported by Java
a) Static Nested Classes

b) Non-static Nested Classes OR Inner Classes

And Non-static nested classes can be of 3 types in java,

a) Member Inner Classes

b) Local Inner Classes

c) Anonymous Inner Classes

Q3. What is Static Nested Classe?
Ans:
A class which is defined inside another class with static keyword is called static inner class. Inside the static inner class, we can declare almost all the member of the class. i.e- instant and static member but inside the static inner class, we can access the only static member of the outer class directly.
In order to create the static inner class object, we need not create an object of outer class we can directly create the object of the static inner class.
For Example:

 

Class Outer
{
int aa=101;
static int bb=202;

static class Inner
{
int a1=110;
static int b1= 222;
static 
{
System.out.println(“sb in inner class”);
}
{
System.out.println(“ib in inner class”);
}
void m1()
{
System.out.println(“m1 in inner class”);
// System.out.println(aa);  // instance variable of outer class cannot be acess inside a static inner 						// class
System.out.println(bb);
System.out.println(a1);
System.out.println(b1);
}
static void m2()
{
System.out.println(“m2 in inner”);
// System.out.println(aa);
System.out.println(bb);
System.out.println(a1);  // in static method we can only acess static variable not instance variable
System.out.println(b1);
}
} // inner

void showAll()
{
System.out.println(“showall in outer);
Inner inr = new inner();
System.out.println(inr.a1);
inr.m1();
inner.m2();
}
}
public class Xadmin
{
public static void main(String args[])
{
Outer.Inner oi1= new Outer().new Inner();
Outer.Inner oi2= new Outer().Inner();
oi2.m1();
Outer.Inner.m2();
}
}

Q4. What do you understand by Nonstatic inner class?
Ans:
A class which is defined inside another class without any extend keyword is called an inner class. In this class, an object can be created with the help of the outer class. Inside the non-static inner class, we cannot have any static declaration. An inner class member cannot be accessed directly inside outer class also. In order to access, member of inner class inside the outer class you need to create the object of inner class inside the outer class then only we can access a member of the inner class.
By using inner class object reference, we cannot access the member of the outer class. Till the time you are not creating the object of the inner class any member of the inner class won,t be processed.
For Example:

 

class Outer
{
int a=10;
int b=20;
static int c=30;

void m1()
{
System.out.println(“m1 in outer class”);
}

static void m2()
{
System.out.println(“m2 in outer class”);
}
class Inner
{
int aa=11;
// static int bb=22;  //static member not allowed inside the inner class.
{
System.out.println(“instance block in inner class”);
}
//  static {  System.out.println(“instance block in inner class”); }//static block not allowed

void mm1()
{
System.out.println(“mm1 in inner class”);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(aaa);

}
}   // inner class closed

void showAll()
{
System.out.println(“showall in outer class”);
// System.out.println(aa);   // part of the inner class cannot be acessed directly
Inner inr = new Inner();
inr.mm1();
System.out.println(inr.aa);
// System.out.println(inr.a); // cannot acess outclass  member with the object of inner class.
}
}
public class Xadmin
{
public static void main(String args[])
{
Outer otr = new outer();
otr.showAll();
// otr.mm1();  not able to acess innner class member through outer class
Outer.Inner o1= new Outer().new Inner();
Outer.Inner o2= otr.new Inner();
o1.mm1();
// o1.showAll();
}
}

Q5. What is Method local inner class?
Ans:
A class which is defined inside any method block or anywhere within a local context is called a local method inner class or method local inner class. In this, we can access almost all the member of a class. The only final member of a method can be accessed inside method local inner class.
Method local inner class members cannot be accessed outside the method in any way. This class will not be processed until the time we have not created an object of method local inner class inside the method.
Example for Method local inner class:

class Outet
{
int a=10;
int b=20;
static int c=30;
void mouter()
{
System.out.println(“Mouter in outer”);
int ab=99;
final int bc=88;

class Inner
{
int x=11;
// static int y=22; // as static is not allowed
static final int xy=33; // it consider as constant.

Void mInner()
{
System.out.println(“minner  in inner”);
System.out.println(a);
System.out.println(b);
System.out.println(c);
// System.out.println(ab); // only final declare can be accessed
System.out.println(bc);
}

} // inner class closed
Inner inr = new inner();
inr.mInner();
} // Mouter

void mn()
{
System.out.println(“m in outer”);
// Inner inr = new Inner();
mouter();
}
}
public class Xadmin
{
public static void main(String args[])
{
Outer otr=new outer();
otr.mn();
}
}

Q6. What do you know about Anominous inner class?
Ans:
This class does not have any name this will always be a subclass of Abstract class or interface or concrete class. This class can be instantiated only once it cannot be used more than once.

Example of understanding the Anonimous inner class:

abstract class Animal
{
abstract void eating();
}
class Dog
{
Animal ani= new Animal(); // here, object created for the anominous class
{
public void eating();
{
System.out.println(“eating in Dog”);
}
public void sleeping()
{
System.out.println(“sleeping in Dog”);
}
};

void  showAll()
{
System.out.println(“showAll  in Dog”);
ani.eating();
// ani.sleeping();
}
Animal mOuter()
{
System.out.println(“mOuter in Dog”);
return new Animal()
}
public void eating()
{
System.out.println(“”eating in dog in Mouter);

}
};
}
}
public class Xadmin
{
public static void main(String args[])
{
Dog dog= new Dog();
dog.showAll();
dog.ani.eating();
Animal ani= dog.mouter();
ani1.eating();
}
}

Q7. Can we write more than one inner class inside a class?
Ans:
Yes, we can write.

Q8. Can we write inner class inside any interface?
Ans:
Yes we can write.
For Example:

Interface I1
{
int a=10;
void m1()
{
class Inner
{
int a1=10;
void m1()
{
System.out.println(“M1 in inner class”);
}
}
}

Interface I2
{
static int b=20;
void m11()
{

class Inner2 implements I2,I1
{
public void m11()
{
System.out.println(“M11 in inner2 class”);
}
}
}
class Hello extends I1.Inner
{
public void m1()
{
System.out.println(“M1 in Hello class”);
}
}
Public class Xadmin
{
Public static void main()
{
I1 i1= null;
i1= new I2.Inner2(); // inner class object creation
i1.m11();   
I1.Inner ii1=null;
ii1=new Hello();
ii1.m1();
}
}

Q9. Can we write any class as private?
Ans:
No we can’t write any class as private.

Q10. Can we write any inner class as private?
Ans:
Yes we can write.

Q11. Can we access non-static members of outer class inside a static nested class?
Ans: No, we can’t access non-static members of outer class inside a static nested class. We can access only static members of the outer class inside a static nested class.

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

Q13. Is it possible to access all the members of the outer class inside a member inner class?
Ans:
Yes, it is possible to access all the members, both static and non-static, of outer class inside a member inner class.

Q14. Is it possible to declare local inner classes as static?
Ans:
No. It is not possible for local inner classes to be static.

Q15. Is it possible to use local inner classes outside the method or block in which they are defined?
Ans:
No. Local inner classes are local to method or block in which they are defined. We can’t use them outside the method or block in which they are defined.

Q16. Is it possible Can we declare local inner classes as private or protected or public?
Ans:
No it is not possible, Local inner classes can’t be declared with access modifiers.

Q17.How to use local variables inside a local inner class?
Ans:
Local variables must be final. We can’t use non-final local variables inside a local inner class in Java.

Q18. Why use or what are the benefits of Java Inner Class
Ans:
1) To make the class useful for one class. It helps in the packaging of the classes.
2) Java inner classes implement encapsulation.
3) inner classes can access outer class private members and at the same time, it can hide the inner class from outer classes in java.
4) It makes the code more readable and maintainable.

Conclusion:
This article will help you with Important Java Interview Questions on inner classes to the freshers and as well as to the experienced candidates. Read the article carefully, I believe this article is going to help you a lot to tackle the question fired by the interviewer. Stay tuned!  for the more interview question. Happy Learning! All the best!