What is an inner class?Inner class are defined inside the body of another class (known as outer class). These classes can have access modifier or even can be marked as abstract and final. Inner classes have special relationship with outer class instances. This relationship allows them to have access to outer class members including private members too. Show
Inner classes can be defined in four different following ways as mentioned below:1) Inner class 1) Inner classAn inner class is declared inside the curly braces of another enclosing class. Inner class is coded inside a Top level class as shown below:- //Top level class definition class MyOuterClassDemo { private int myVar= 1; // inner class definition class MyInnerClassDemo { public void seeOuter () { System.out.println("Value of myVar is :" + myVar); } } // close inner class definition } // close Top level class definition Inner class acts as a member of the enclosing class and can have any access modifiers: Instantiating an inner class class MyOuterClassDemo { private int x= 1; public void innerInstance() { MyInnerClassDemo inner = new MyInnerClassDemo(); inner. seeOuter(); } public static void main(String args[]){ MyOuterClassDemo obj = new MyOuterClassDemo(); obj.innerInstance(); } // inner class definition class MyInnerClassDemo { public void seeOuter () { System.out.println("Outer Value of x is :" + x); } } // close inner class definition } // close Top level class definition Output: Outer Value of x is :1 Instantiating an inner class from outside the outer class Instance Code: public static void main(String args[]){ MyOuterClassDemo.MyInnerClassDemo inner = new MyOuterClassDemo().new MyInnerClassDemo(); inner. seeOuter(); } 2) Method–Local inner classesA method local inner class is defined within a method of the enclosing class. If you want to use inner class , you must instantiate the inner class in the same method, but after the class definition code. Only two modifiers are allowed for method-local inner class which are //Top level class definition class MyOuterClassDemo { private int x= 1; public void doThings(){ String name ="local variable"; // inner class defined inside a method of outer class class MyInnerClassDemo { public void seeOuter () { System.out.println("Outer Value of x is :" + x); System.out.println("Value of name is :" + name);//compilation error!! } //close inner class method } // close inner class definition } //close Top level class method } // close Top level class The above code will throw a compilation error as Inner class cannot use the non-final variables of the method, in which it is defined. final String name ="local variable";// inner object can use it 3) Anonymous Inner ClassesIt is a type of inner class which
Example: package beginnersbook.com; class Pizza{ public void eat() { System.out.println("pizza"); } } class Food { /* There is no semicolon(;) * semicolon is present at the curly braces of the method end. */ Pizza p = new Pizza(){ public void eat() { System.out.println("anonymous pizza"); } }; } 4) Static Nested ClassesA static nested classes are the inner classes marked with static modifier.Because this is static in nature so this type of inner class doesn’t share any special kind of relationship with an instance of outer class.A static nested class cannot access non static members of outer class. Example: class Outer{ static class Nested{} } A static nested class can be instantiated like this: class Outer{// outer class static class Nested{}// static nested class } class Demo{ public static void main(string[] args){ // use both class names Outer.Nested n= new Outer.Nested(); } } Reference(s) Which modifiers is applicable for both Inner and outer classes?1. Public access modifier can apply to instance variables, constructors, inner classes, outer class, methods but not with local variables.
What modifiers may be used with an inner class?Modifiers. You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers private , public , and protected to restrict access to inner classes, just as you use them to restrict access do to other class members.
How can an inner class access the members of outer class?Inner classes can access the variables of the outer class, including the private instance variables. Unlike the non-static nested classes, the static nested class cannot directly access the instance variables or methods of the outer class. They can access them by referring to an object of a class.
Which are the valid modifiers which can be applied on outer class?Also, nested classes can be declared with few more modifiers namely -- private, protected, and static. We are going to keep this question limited to public, final and abstract modifiers applied to only outer classes. One or more of these class modifiers can be applied to a class declaration.
|