Java Inner Class extends Outer Class

后端 未结 3 1587
情歌与酒
情歌与酒 2021-02-08 07:31

There are some cases in Java where an inner class extends an outer class.

For example, java.awt.geom.Arc2D.Float is an inner class of java.awt.geom.Arc2D, and also exten

相关标签:
3条回答
  • 2021-02-08 08:09

    Have a look at Java's Point2D. It has two inner classes that are sub-classes of it.

    The important thing to note is that they are static inner classes. This has an entirely diffenent meaning that a regular inner class. Just like a static method, a static class is defined at the class-level instead of the object level.

    In the Point2D case, it is done to logically couple the classes and their logic. It helps a user of the abstract type Point2D find an implementation that they can use.

    In response to your edit I'd like to point out 1 important fact. A single Java file may only contain one public class, except for public inner classes. While both of your examples may compile, they do not allow access to those classes to the public. If you want to present multiple public classes to someone in a single file, you must use public static inner classes.

    0 讨论(0)
  • 2021-02-08 08:12

    There 1st one compiles fine on my IntelliJ.

    Strictly speaking, static member classes are not inner classes. They are called nested classes.

    0 讨论(0)
  • 2021-02-08 08:20

    There are two cases of inner-classes:

    • static inner-classes. The inner-class does not keep reference to the outer-class.

    • non-static inner-classes. The inner-class does keep a reference to the outer-class.

    The case of a static inner-class that extends the outer-class is not as interesting as the non-static inner-class extending the outer-class.

    What happens with the latter is: to create the inner-class, one needs a reference to the outer-class. However, as the inner-class is an instance of the outer-class, it also accepts a reference to another instance of the inner-class, to be used as outer-class.

    Let's look at some code:

    Outer a = new Outer();
    Outer.Inner b = a.new Inner();
    
    // Only possible when Inner extends Outer:
    Outer.Inner c = a.new Inner().new Inner();
    

    If you know the builder pattern, this can be used to have an OOP-version of it:

    public abstract class Command {
    
        // Not possible to create the command, else than from this file!
        private Command() {
        }
    
        public abstract void perform();
    
        public static class StartComputer extends Command {
            public void perform() {
                System.out.println("Starting Computer");
            }
        }
    
        public class OpenNotepad extends Command {
            public void perform() {
                Command.this.perform();
                System.out.println("Opening Notepad");
            }
        }
    
        public class ShutdownComputer extends Command {
            public void perform() {
                Command.this.perform();
                System.out.println("Shutting Computer");
            }
        }
    
    }
    

    Which is used as: new Command.StartComputer().new OpenNotepad().new ShutdownComputer().perform();.

    0 讨论(0)
提交回复
热议问题