Some summaries of the inner classes.

Source: Internet
Author: User

http://andy136566.iteye.com/blog/1061951/the original text here, but because of typesetting problem, I modify and improve here.

First of all, what is the use of internal classes, this is the most important, after all know the application scenario, after the encounter can be used.
1. Internal class can be very good to implement hidden (general non-internal class, is not allowed to have private and protected permissions, but static internal classes, members of the internal class can)
2. Internal classes have access to all elements of an external class
3. Multiple inheritance can be achieved
4. You can avoid modifying interfaces to implement calls to two methods of the same name in the same class.

Here is an example:
1. Realize hidden

public class Outter {

    private class inner{

    }
    private static class inner2{

    }
}

2, you can unconditionally access all elements of the perimeter class

public class Outter {

    private int num;

    Private class inner{public
        void Test () {
            System.out.println (num);
        }
    }

}

3. Multiple inheritance can be achieved
This feature is very important and is considered by individuals to be one of the biggest reasons for the existence of internal classes. It is because of his existence that makes the inheritance mechanism of Java more perfect. As you all know, Java can only inherit a class, and its multiple inheritance is implemented using interfaces before we learn the inner class. But sometimes there are many inconvenient places to use interfaces. For example, if we implement an interface, we must implement all the methods within it. The inner class is different, and it enables our class to inherit from the class in order to achieve pseudo multiple inheritance through the inner class.

Class One public
class Example1 {public

   String name ()
   {return
       ' Ghy ';
   }
}

Class Two public
class Example2 {public

    int age ()
    {return
        ;
    }
}

Class three public
class outter{

   private class Inner1 extends example1{public

        String name () {
           return Super.name ();
        }

   Private class Inner2 extends example2{public
        int Age () {return
           super.age ();
       }
   }

    Public String name () {return
        new Inner1 (). Name ();

   public int Age () {return
       new Inner2 (). age ();

Avoid modifying interfaces to implement calls to two methods of the same name in the same class
Let's imagine if your class inherits a class and implements an interface, but you find that the class and interface you are inheriting have two methods with the same name. How do you differentiate them? This requires our inner class.

Public interface MyInterface {public
    void Test ();
}

Public abstract class Myabstract {public
    abstract void Test ();
}

public class MyTest extends Myabstract implements myinterface{public
    void Test () {
    }
}

Want to ask everybody test () This method belongs to overlay myabstract here method. It's the way myinterface here. Clearly this is not a good distinction. And we can solve this problem if we use the inner class.

public class MyTest extends Myabstract {
    @Override public
    Void Test () {
    } public

    class Inner implements my interface{
        @Override public
        Void Test () {
            //do something
        }
}}

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.