Internal Classes in JAVA

Inner class

  • Classes defined within other classes are called internal classes, and classes containing internal classes are called external classes.

  • The characteristics of internal classes:

    • Better encapsulation allows internal classes to be encapsulated in external classes, and no other classes in the same package are allowed to access the internal classes.
    • Internal classes have direct access to members of external classes, including private members. However, external classes can not access internal classes directly, so they need to create objects.
    • Three permission modifiers that can be used by internal analogies and external classes: private, protected,static
  • Define internal classes:

    • Membership inner class: the class defined outside the method (member location) in the class;

      class Outer{
          class Inner{
              
          }
      }
      
    • Local internal classes: internal classes defined in methods;

      class Outer{
         public void show(){
              class Inner{
              
          }
         }
      }
      
  • Membership inner class

    • Create member inner classes
    public class MyTest {
        public static void main(String[] args) {
            Outer outer = new Outer();
            System.out.println(outer.num);
            outer.outerShow();
            outer.method();
            System.out.println("--------------------------------");
        
            //Create member inner classes
            Outer.Inner inner=new Outer().new Inner();
            System.out.println(inner.b);
            inner.innerShow();
            inner.innerTest();
        }
    }
    
    class Outer {
        int num = 10;
        private int a = 100;
        //Define member inner classes
        class Inner {
            int b = 1000;
            public void innerShow() {
                System.out.println("Inner class show Method");
            }
            //Internal classes directly access members of external classes, including private ones
            public void innerTest(){
                System.out.println(num);
                System.out.println(a);
                outerShow();
                outerTest();
            }
    
        }
    
        public void outerShow() {
            System.out.println("External class show Method");
        }
    
        private void outerTest() {
            System.out.println("Privatization of external classes show Method");
        }
        //External classes access internal classes
        public void method(){
            //Create objects for internal classes
            Inner inner = new Inner();
            System.out.println(inner.b);
            inner.innerShow();
    
        }
    }
    
    //Result:
    /*
        10
        show method of external class
        1000
        show method of inner class
        --------------------------------
        1000
        show method of inner class
        10
        100
        show method of external class
        Private show Method for External Classes
    */
    
    • When the inner class is private ly modified, it is impossible to create inner class objects outside the outer class. If you want to access the inner class in the main() method, you can provide a method to access the inner class in the outer class.

      public class Test{
          public static void main(String[] args) {
             Outer outer = new Outer();
             outer.method();
          }
      }
      
      class Outer{
      
          private class Inner{
              public void show(){
                  System.out.println("Inner class show Method");
              }
          }
          public void method(){
              Inner inner = new Inner();
              inner.show();
          }
      }
      
      //Result:
      //   show method of inner class
      
    • Internal ibu classes modified by static, i.e. static internal classes (or called class internal classes), belong to the external class itself, not to an object of the external class. According to the rule that static members cannot access non-static members, static internal classes cannot access non-static members of external classes.

    • The use of this keyword in internal classes:

      class Test {
          public static void main(String[] args) {
              Outer.Inner oi = new Outer().new Inner();
              oi.show();
          }
      }
      class Outer {
          public int num = 1;
          class Inner {
              public int num = 2;
              public void show() {
                  int num = 3;
                  System.out.println(num); //3
                  System.out.println(Inner.this.num); //2
                  System.out.println(new Outer().num); //1
                  System.out.println(Outer.this.num);//1
              }
          }
      }
      
  • Local inner class

    • Objects of local internal classes cannot be created directly outside of external classes.
    • When a local inner class accesses a local variable of an external class, the local variable must be finalized (Jdk1.8 is added by default), because the local variable will disappear with the call of the method, and the object of the local inner class does not disappear from the heap memory. It still needs to use the local variable. In order to make the data available, a final key is added to the local variable. Word, so that stored in memory is a constant value.
  • Anonymous Inner Class

    • Format:

      new class name or interface name (){
      		Rewriting method;
      } ;
      
      • Anonymous inner class is a simplified way to write local inner class. It is actually an anonymous object of a subclass that inherits the class or implements the interface and can only be used once.
      • As you can see above, anonymous inner classes must inherit a parent class or implement an excuse.
    • Use:

      interface Inter {
          public abstract void show();
      }
      
      class Outer{
          public static void method(){
              //Anonymous inner classes are actually subclasses of anonymous objects that inherit the Inter excuse
              new Inter(){
                  public void show(){
                      System.out.println("Rewrite show()Method");
                  }
              }.show();
      
          }
      }
      class OuterTest{
          public static void main(String[] args) {
              Outer outer = new Outer();
              outer.method();
      
          }
      }
      
      public class Demo {
          public static void main(String[] args) {
              Outer k = new Outer();
              //Anonymous objects are passed in as parameters
              k.method(new AbstractClass() {
                  @Override
                  public void show() {
                      System.out.println("Rewrite show()Method");
                  }
              });
          }
      }
      
      abstract class AbstractClass {
          public abstract void show();
      }
      
      class Outer {
          public void method(AbstractClass p) {
              p.show();
          }
      }
      

Keywords: Programming

Added by ericorx on Mon, 20 May 2019 05:58:06 +0300