Q: Which of the following subclasses of the InnerClass implementation ChildInnerClassX can be compiled and run?


     
  1. class OutClass {

  2.    class InnerClass {

  3.    }

  4. }

  5. class ChildInnerClass1 extends OutClass.InnerClass {

  6. }

  7. class ChildInnerClass2 extends OutClass.InnerClass {

  8.    public ChildInnerClass2() {

  9.        super();

  10.    }

  11. }

  12. class ChildInnerClass3 extends OutClass.InnerClass {

  13.    public ChildInnerClass3(OutClass outClass) {

  14.        super();

  15.    }

  16. }

  17. class ChildInnerClass4 extends OutClass.InnerClass {

  18.    public ChildInnerClass3(OutClass outClass) {

  19.        outClass.super();

  20.    }

  21. }

Copy the code

A: Only the ChildInnerClass4 subclasses will compile and run, the others will not compile. The constructor of an inherited class must have a reference to an object in the outer class and call super() from that reference. The constructor of an inherited class must have a reference to an object in the outer class and call super() from that reference. An external class cannot instantiate itself without first instantiating it.

Q: What is the result of the following program? Why is that?


     
  1. List list1 = new ArrayList();

  2. List list2 = new ArrayList(){};

  3. List list3 = new ArrayList(){{}};

  4. List list4 = new ArrayList(){{}{}{}};

  5. / / 1

  6. System.out.println(list1.getClass() == list2.getClass());

  7. / / 2

  8. System.out.println(list1.getClass() == list3.getClass());

  9. / / 3

  10. System.out.println(list1.getClass() == list4.getClass());

  11. / / 4

  12. System.out.println(list2.getClass() == list3.getClass());

  13. / / 5

  14. System.out.println(list2.getClass() == list4.getClass());

  15. / / 6

  16. System.out.println(list3.getClass() == list4.getClass());

Copy the code

Answer: Program run returns 6 false.

First, list1 points to an ArrayList object instance; List2 points to an anonymous class inner class object that inherits from ArrayList; List3 also points to an anonymous inner class (with a pair of parentheses for initialization blocks) that inherits from ArrayList; List4 also points to an anonymous inner class that inherits from ArrayList (with multiple initialization blocks in parentheses); Since these anonymous inner classes are all in the same class, they compile as Demo$1, Demo$2, and Demo$3, so they are all equivalent to each other. You can verify this by using listx.getClass ().getName().

Q: What are the considerations (lessons) for using Java anonymous inner classes in development?

A: Common precautions are as follows.

  • When using anonymous inner classes, you must either inherit a class or implement an interface (you cannot do both and can only inherit a class or implement an interface).

  • Constructors cannot be defined in anonymous inner classes and can be initialized by constructing code blocks.

  • An anonymous inner class cannot have any static member variables or static methods.

  • Anonymous inner classes are local inner classes, so any restrictions that apply to local inner classes also apply to anonymous inner classes.

  • An anonymous inner class cannot be an abstract class and must implement either the inherited class or all the abstract methods that implement the interface.

Q: How are Java anonymous inner classes initialized when used?

A: Anonymous inner classes cannot be initialized by constructors, so we can only initialize them by constructing code blocks.

This is the sequel to Part 2 of the last Part of the internal class, please watch tomorrow’s push sequel

Old iron people, don’t be too short, long you will not finish reading, so this is the purpose of daily code farmers (for other historical articles, please check the public account history record) ~

After watching the share wave, it will be more interesting to discuss with your friends. Share 666 ~ in the upper right corner

Relax with a joke

In the music class, the teacher played a piece of Beethoven. Xiao Ming asked Xiao Hua, “Do you know music?” Xiao Hua: Yes, Xiao Ming: Do you know what the teacher is playing? Xiao Hua: “Piano.”

Only in your smile, I just have to breathe.

Only in your smile, I can breathe.