Most frequently asked Java Nested Classes Interview Questions
- What is nested class in java?Give syntax?
- What are the types of nested class?
- Why do we use nested classes?
- What is static nested class?
- What is java inner class?
- What is member inner classes?
- What is local inner classes?
- What is anonymous inner class in java?
- Explain the difference between static and non static nested classes?
- Why are private members of inner Java classes accessible to outer Java classes?
- How Many.class Files Are Created When You Compile A File With Inner Classes And Are They All Accessible In The Same Way?
What is nested class in java? Give syntax?
When class is defined within another class,such class is called a nested class.Syntax:
class Outer{ ... class Nested{ ... } }
What are the types of nested class?
There are two types of nested class in java.- Static Nested Classes
- Non-static Nested Classes OR Inner Classes.
- Member Inner Classes
- Local Inner Classes
- Anonymous Inner Classes
Why do we use nested classes?
- It's a sensible approach of combining classes that are only used once: if a class is only useful to one other class, it's logical to embed it in that class and keep the two together. Their package is more simplified by nesting such "helper classes."
- Consider two top-level classes, X and Y, in which Y requires access to members of X that would otherwise be declared private. X A's members can be declared private and Y can access them by hiding class Y within class X. Furthermore, Y can be hidden from the outer world.
- The code is closer to where it is utilised when small classes are nested within top-level classes.
What is static nested class?
If the nested class is static, then it's called a static nested class. Only static members of the outer class are accessible to static nested classes, including private. Non-static (instance) data members cannot be accessed by the static nested class. A static nested class is the same as any other top-level class, with the exception that it is nested for packaging purposes. The following statement can be used to create a static class object.OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();Example:
class outer{ static int x=10; static class Inner{ void show(){ System.out.println("Value is: "+x); } } public static void main(String args[]){ outer.Inner s=new outer.Inner(); s.show(); } }
What is java inner class?
In Java, an inner class is any non-static nested class. Java inner classes are linked to the class's object and have access to all of the outer class variables and functions. We can't have any static variables in inner classes because inner classes are associated with the instance. Although we know that a class cannot be associated with the private access modifier, if the class is a member of another class, the inner class can be declared private.The following statement can be used to create a non-static nested class object.
OuterClass outerObject = new OuterClass(); OuterClass.InnerClass innerObject = outerObject.new InnerClass();
What is member inner classes?
Non-static nested classes that are declared as non-static members of an outer class are known as Member Inner Classes.It should contain only non-static members,no static members allowed.class outer{ private int x=10; class inner{ void show(){ System.out.println("Value is "+x); } } public static void main(String args[]){ outer s=new outer(); outer.inner i=s.new inner(); i.show(); } }
What is local inner classes?
Local inner classes must be defined inside a method or a block,and this block is generally a method body.They can be final or abstract.You must instantiate the local inner class inside the method if you want to call the methods of the local inner class.class test{ private int x=10; void show(){ class local{ void msg(){ System.out.println("Value is:"+x); } } local obj=new local(); obj.msg(); } public static void main(String args[]){ test t=new test(); t.show(); } }
What is anonymous inner class in java?
Anonymous inner class, is a class which is defined without a name. In Java, an anonymous inner class is a non-static nested class that has no name.In this class only a single object is created.If you need to override a method of class or interface me, anonymous classes can be used .There are two techniques to make an anonymous inner class in Java:- Class (may be abstract or concrete).
- Interface
abstract class animal{ abstract void dog(); } class test{ public static void main(String args[]){ animal a=new animal(){ void dog(){ System.out.println("Dog is an animal"); } }; a.dog(); } }
interface vehicle{ void car(); } class test{ public static void main(String args[]){ vehicle v=new vehicle(){ public void car(){ System.out.println("Car is a vehicle"); } }; v.car(); } }
Explain the difference between static and nonstatic nested classes?
- The static inner class has full access to the outer class's static members. However, you must instantiate the outer class to access its instance members.
- Static nested class does not require an Outer class reference, while nonstatic nested class or Inner class requires an outer class reference.
- A non-static nested class has complete access to the members of the class it is nested within, whereas static nested class does not have a reference to the nesting instance, it is unable to call non-static methods or access non-static fields of an instance of the class inside which it is nested.
Why are private members of inner Java classes accessible to outer Java classes?
The inner class is simply a means to neatly separate some code that should be in the outer class. They're meant to be used when you've got two requirements: Some functionality in your outer class would be easier to understand if it were implemented in a separate class. Despite being in a different class, the functionality is closely tied to how the outer class operates. Inner classes have complete access to their outside classes due to these requirements. Because they're members of the outer class, it's only natural that they have access to the outer class's methods and properties, including privates.How Many.class Files Are Created When You Compile A File With Inner Classes, And Are They All Accessible In The Same Way?
When an inner class enclosed by with an outer class is compiled,then two.class files are created: one for each inner class and one for the outer class.Example:
class outer { class inner{ } }If you compile with the command: % javac EnclosingOuter.java.There will be two files created. Even though a separate inner class file is generated, it is not accessible in the usual way.
outer.class
outer$inner.class