Saturday, 29 December 2018

Java Innerclass


  • Inner class is a class defined inside other class and act like a member of the enclosing class.
  • There are two main types of inner classes
    • Static member class
    • Inner class
      • Member class
      • Anonymous class
      • Local class
  • Static member class
    • A static member class behaves much like an ordinary top-level class, except that it can access the static members of the outer
    • Outer class can access all inner class fields by referring Inner Class.(InnerA.printAnimal()). But inner class can access all static outer class properties and methods directly.
    • Both inner and outer classes can access others private properties and methods.
    • The static nested class can be accessed as the other static members of the enclosing class without having an instance of the outer class.
    •  The static class can contain non-static and static members and methods.
    • InnerClassTest1.java
      • public class InnerClassTest1 { private static int out = 1; public static class innerA { private static int number = 10; static void printAnimal(String animal) { System.out.println(out); System.out.println(animal); test(); } } private static void test() { System.out.println(innerA.number); } }
    • App.java
      • public class App { public static void main(String[] args) { InnerClassTest1.innerA.printAnimal("Tiger"); } }
  • Member class
    • Need to create Instance access properties and class out side the method.
    • Need Outer class object to create inner class object.
    • The member class can be declared as public, private, protected, final and abstract. E.g.
    • InnerClassTest2.java
      • public class InnerClassTest2 { private int out = 1; public class MemberClass { private void showName() { System.out.println("Sonu"); System.out.println(out); } } public void test() { MemberClass m = new MemberClass(); m.showName(); } }
    • App.java
      • public class App { public static void main(String[] args) { InnerClassTest2 o = new InnerClassTest2(); InnerClassTest2.MemberClass m = o.new MemberClass(); } }
  • Local class
    • It can be accessed only from same method.
    • Local classes cannot be public, private, protected, or static. (No access modifiers)
    • Local class can access all outer class properties and methods.
    • local class can access local variables and parameters of the enclosing method that are effectively final.
    • InnerClassTest3.java
      • public class InnerClassTest3 { int outerNumber = 1; public void methodTest() { int methodNumber = 2; class MethodLocalClass { private void showName() { System.out.println("SONU"); System.out.println(outerNumber); System.out.println(methodNumber); test(); } } methodNumber = 2; //error outerNumber =3; //will work MethodLocalClass m = new MethodLocalClass(); m.showName(); } public void test() { System.out.println("TEST"); } }
  • Anonymous class
    • These are local classes which are automatically declared and instantiated in the middle of an expression
    • Also, like local classes, anonymous classes cannot be public, private, protected, or static. 
    • They can define in the  arguments to the constructor of the outerclass, but cannot otherwise have a constructor.
    • They can implement only one interface or extend a class.
    • Anonymous class cannot define any static fields, methods, or classes, except for static final constants.
  • xxxx

No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *