Nested Inner Class
//outer class or top level class class A{ //inner class class B{ } }
class A{ class Logic{ //business logic here.. } class View{ //design here.. } }
//outer class class A{ //inner class class B{ //nested inner class class C{ } } }
class A{ class B{ class C{ } } class D{ class C{ } } }
class A{ private class B{ } protected class C{ } public class D{ } final class E{ } abstract class F{ } }
class A{ private int i=10; class B{ void show(){ System.out.println(i); } } public static void main(String [] h){ //create outer class object A a=new A(); //create inner class object through outer reference B b=a.new B(); b.show(); } } /*Note : Inner class B is non static so firstly we need to create an instance ot object of Outer class then through the outer reference create an inner class object to access inner class property other wise there is compile time error.*/ Output is : 10
class A{ class B{ private int i=10; } class C{ void show(){ System.out.println(new B().i); } } public static void main(String [] h){ A a=new A(); C c=a.new C(); c.show(); //OR we can also call like below : new A().new C().show(); } } Output is : 10
class A{ interface B{ } class G{ } } OR interface C{ class F{ } interface D{ } }
class A{ interface T{ // that is possible } class B{ interface C{ // there is compile time error } } void show(){ interface G{ // there is compile time error } } }
class A{ class B{ } static class C{ } void show(){ class F{ } } } //When you compile then compiler create four .class like //A.class, A$B.class, A$C.class and A$1F.class.
There are 4 types of inner class :
- Non-Static Inner class
- Static Inner class
- Local Inner class
- Annonymous Inner class
1. Non-Static Inner class :
class A{ //non-static inner class class B{ } }
class A{ void say(){ System.out.println("Hey i am outer class method"); } class B{ void show(){ System.out.println("Hey i am non-static inner class method"); } } public static void main(String [] h){ A a=new A(); a.say(); B b=a.new B(); b.show(); } } Output is : Hey i am outer class method Output is : Hey i am non-static inner class method
class A{ int i=1; class B{ int i=2; class C{ int i=3; class D{ int i=4; void show(){ int i=5; System.out.println(i); // 5 System.out.println(this.i); // 4 System.out.println(C.this.i); // 3 System.out.println(B.this.i); // 2 System.out.println(A.this.i); // 1 } } } } }
class A{ class B{ final static int i=10; // only this can applicable static void show(){} // not applicable static int k=100; // not applicable } }
2. Static Inner class
class A{ //static inner class static class B{ } }
class A{ static class B{ static void show(){ System.out.println("Hey i am static inner class"); } } public static void main(String [] h){ A.B.show(); } }
class A{ static class B{ int i=100; void show(){ System.out.println(i); } } public static void main(){ A.B a=new A.B(); a.show(); } } Output is : 100
class A{ static class B{ public static void main(String [] h){ System.out.println("Hello static inner class"); } } } //compile by javac A.java //run by java A$B Output is : Hello static inner class
3. Local Inner class
class A{ void show(){ //local inner class class B{ } } }
class A{ void show(){ //local inner class private class B{ // there is compile time error. } protected class B{ // there is compile time error. } public class B{ // there is compile time error. } } }
class A{ void show(){ //local inner class class B{ void goo(){ System.out.println("Hey i am local inner class"); } } B b=new B(); b.goo(); } public static void main(String [] h){ A a=new A(); a.show(); } } Output is : Hey i am local inner class
4. Annonymous Inner class
A simple example using local inner class :
interface B{ void add(int i,int j); } class A{ void show(int i,int j){ class D implements B{ public void add(int i,int j){ System.out.println(i+j); } } D d=new D(); d.add(i,j); } public static void main(String [] h){ new A().show(2,2); } } Output is : 4
A simple example using annonymous inner class :
interface B{ void add(int i,int j); } class A{ public static void main(String [] h){ //this is the syntax of annonymous inner class B b=new B(){ public void add(int i,int j){ System.out.println(i+j); } }; b.add(2,2); } } //Annonymous class is generated at runtime when object is created. //Annonymous .class name is A$1.class Output is : 4
A simple example using lambda expression :
interface B{ void add(int i,int j); } class A{ public static void main(String [] h){ B b=(i,j)->System.out.println(i+j); b.add(2,2); } } Output is : 4
No comments:
Post a Comment