官术网_书友最值得收藏!

Declaring classes

The following lines declare a new minimal Rectangle class in Java. The code file for the sample is included in the java_9_oop_chapter_03_01 folder, in the example03_01.java file.

class Rectangle {
}

The class keyword, followed by the class name (Rectangle), composes the header of the class definition. In this case, we don't specify a parent class or superclass for the Rectangle class. A pair of curly braces ({}) encloses the class body after the class header. In the forthcoming chapters, we will declare classes that inherit from another class, and therefore, they will have a superclass. In this case, the class body is empty. The Rectangle class is the simplest possible class we can declare in Java 9.

Note

Any new class you create that doesn't specify a superclass will be a subclass of the java.lang.Object class. Thus, the Rectangle class is a subclass of java.lang.Object.

The following lines represent an equivalent way of creating the Rectangle class. However, we don't need to specify that the class inherits from java.lang.Object because it adds unnecessary boilerplate code. The code file for the sample is included in the java_9_oop_chapter_03_01 folder, in the example03_02.java file.

class Rectangle extends java.lang.Object {
}
主站蜘蛛池模板: 志丹县| 濮阳县| 苍南县| 泰和县| 东阿县| 固安县| 仁布县| 诸城市| 洛南县| 乌兰察布市| 鄂托克前旗| 阳朔县| 陇西县| 登封市| 庆阳市| 宝兴县| 盐城市| 彝良县| 邢台市| 莲花县| 菏泽市| 渝北区| 天柱县| 雅江县| 开封市| 阜南县| 长岛县| 讷河市| 岳西县| 上饶县| 新巴尔虎左旗| 宣威市| 福清市| 乌拉特后旗| 太保市| 九寨沟县| 岐山县| 阳江市| 开封市| 当涂县| 苗栗县|