Why internal classes are required
In general, an inner class inherits from a class or implements an interface, and the inner class's code action creates an object of its outer class. So it can be thought that the inner class provides some kind of window into its outer class.
One question that the inner class must answer is: if you just need a reference to an interface, why not implement that interface through the perimeter class? The answer is: "If this satisfies the demand, then it should be done." "So what's the difference between an internal class implementing an interface and a perimeter class implementing this interface?"
The answer is: the latter is not in that can enjoy the convenience of the interface, sometimes need to use the implementation of the interface. So the most appealing reasons for using inner classes are:
each inner class can inherit itself from one (interface) implementation independently, so no matter whether the perimeter class has inherited an implementation of (an interface), there is no effect on the inner class.
Some design and programming problems can be difficult to solve without the ability of an inner class to inherit multiple concrete or abstract classes. From this point of view, the inner class makes the solution of multiple inheritance complete. The interface solves some of the problems, while the inner class effectively implements "multiple inheritance." Other words
Inner classes allow the inheritance of multiple non-interface types (classes or abstract classes).
Scenario 1: Two interfaces must be implemented in a class in some way. Due to the flexibility of the interface, it can be implemented in 2 ways: Using a single class, or using an inner class
1 Packagetools;2 3 Importtools. INNERCLASS.A;4 Importtools. innerclass.b;5 6 Public classInnerclass {7 8 InterfaceA {}9 InterfaceB {}Ten One Static voidTakesa (a A) {} A Static voidTakesb (b b) {} - - Public Static voidMain (string[] args) { the -x x =NewX (); -Y y =NewY (); - //1 + Takesa (x); - Takesa (y); + //2 A takesb (x); at Takesb (Y.makeb ()); - } - - } - - classXImplementsA, B {} in classYImplementsA { - B Makeb () { to return NewB () {}; + } -}
View Code
Scenario 2: If you have an abstract class or a concrete class, rather than an interface, you can only use an inner class to implement multiple inheritance.
1 Packagetools;2 3 classD {}4 Abstract classE {}5 classZextendsD {6E Makee () {return NewE () {};}7 }8 9 Ten Public classInnerclass { One Static voidTakesd (d d) {} A Static voidTakese (E d) {} - Public Static voidMain (string[] args) { -Z z =NewZ (); the takesd (z); - Takese (Z.makee ()); - } - +}
View Code
Inner classes also have some other features:
1. An inner class can have multiple instances, each with its own state information, and the information of its peripheral class objects is independent of each other.
2. In a single perimeter class, you can have multiple inner classes implement the same interface in different ways, or inherit the same class.
3. Create an inner class object at the moment and rely on the creation of the perimeter class object
4. The inner class does not have a confusing "is-a" relationship, it is a separate entity.
Revisit Java (inner class (1)-Why internal classes are required)