JAVA internal class explanation and example _java

Source: Internet
Author: User
Tags anonymous inheritance

JAVA Inner class

One, what is the internal class?

The definition of a class is placed inside another class, and this class is called an inner class

What are the characteristics of the inner class?

1. The inner class is still a separate class, and after compilation The inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class.
2. Internal classes cannot be accessed in a normal way. An inner class is a member of an external class, so an inner class can freely access the member variables of the external class, whether private or not.
3, the internal class declared static, you can not casually access the external class member variables, at this time the internal class can only access the external class static member variables.

What kinds of internal classes are there?

1. member Internal class

Such as:

Package com.test01; public class A {//internal Class B inherits Testpojo and implements TestInterface interface Class B extends Testpojo implements testinterface{//internal Class B own method public void run () {System.out.println ("I'm running!")
    "); }//overridden interface method public void Testf () {System.out.println ("Implement Interface!")
    ");
    The internal class public void Test () {b = new B () is called inside the//method.    B.TESTF ();     Drop the Rewrite interface method B.run ();   Call your own method B.testpojo ();
    Invoke method of inheriting parent class}//Main method test public static void main (string[] args) {A A = new A ();
  A.test ();
  }//Defines an interface that is TESTF () interface testinterface{public void Testf ();}//defines a common class method Testpojo () class testpojo{
  public void Testpojo () {System.out.println ("I am a simple Pojo class");  }//Implementation calls method class inside class textone{public static void Main (string[] args) {a.b b = new A (). New B ();
     The internal Class B/** in the invocation class A is equivalent to the following code * A A = new A ();
     * A.B B = a.new B ();   * * */B.TESTF ();    Drop the Rewrite interface method B.run (); //Call your own method B.testpojo (); To invoke a method that inherits the parent class}

2. Method Inner Class

Package com.test01;

public class Pertest {public
  void Test () {     //define a method class
    ne{        //Define a method internal class public
      void Fle () {  / /define method Inner class method 
        System.out.println ("I am flying!") ");
      }
    } ;
    New Ne (). Fle ();    Method of calling inner class
  } public
  static void Main (string[] args) {
    new Pertest (). Test ();  Test
  }
}

Note: (1), the method inner class can only be instantiated within the method that defines the inner class, and cannot be instantiated outside of this method.

(2), the method inner class object cannot use the non final local variable of the method in which the inner class resides.

Because the method's local variable is on the stack, it exists only in the lifetime of the method. When a method ends, its stack structure is deleted,
Local variables become history. However, after the method is finished, the inner class object created within the method may still exist in the heap!
For example, if a reference to it is passed to some other code, it is stored in a member variable. Because there is no guarantee of local change
The lifetime of a quantity is as long as the method's internal class object, so the inner class object cannot use them. (The understanding from Baidu Encyclopedia)

3. Anonymous inner class

1), abstract anonymous inner class

Package com.anonymous;

public class AbstractClass {
  the public void Test () {        //method is Test
    testa a = new Testa () {    //Implementation abstract class
      @Override Public
      void Run () {    ///Implement abstract class method
        System.out.println ("I am using abstract anonymous inner class");
      }
    ;
    A.run ();          Method of calling inner class
  } public
  static void Main (string[] args) {
    new AbstractClass (). Test ();  Test
  }
//define an abstract class Testa abstract method for run ()
abstract class testa{public
  abstract void Run ();
}

2), Interface anonymous inner class

Package com.anonymous;

public class Testanonymous {
  myinterface m = new MyInterface () {  //implementation interface public
    void Eat () {        / Overriding the MyInterface interface method
      System.out.println ("I'm eating!") ");
    }
  } ;
  public void SS () {  //Method SS
    M.eat ();    Call overridden Method
  } public
  static void Main (string[] args) {
    new testanonymous (). SS ();  Test
  }
//Define an interface method for eat
interface myinterface{public
  void Eat ();
}

Note: Anonymous inner classes can be defined within a method or in a member of a class, whether that anonymous inner class cannot be called directly by an external class

Iv. the role of the internal class?

Each inner class can independently inherit from one (interface) implementation, so no matter whether the outer class has inherited an (interface) implementation, it has no effect on the inner class. Some design and programming problems are difficult to solve without the ability of an inner class to inherit multiple concrete or abstract classes. From this perspective, the inner class makes the multiple-inheritance solution complete.

The interface solves some of the problems, while the inner class effectively implements multiple inheritance.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.