Java Inner Class Interview Questions
In this post we will look at Java Inner Class Interview questions. Examples are provided with explanation.
Q: What is Inner Class in Java?
A: In Java similar methods and variables of a class, we can have a class as member of another class . Declaring a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
class Java_Outer_Demo { class Java_Inner_Demo { } }
Q: What are the advantages of using Inner Class in Java?
A: The Advantages of using Polymer are as follows-
- Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
- Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
- Code Optimization: It requires less code to write.