Java Inner Class extends Outer Class

后端 未结 3 1586
情歌与酒
情歌与酒 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.

提交回复
热议问题