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

Interfaces

Interfaces are reference types in Java. As such, they define the skeleton of classes and objects but without including the actual functionality of methods. Classes implement interfaces but do not extend them. Let's look at an example of a simple interface, further developing the idea of building classes to represent different types of computers.

interface Computer {

    public String getDeviceType();

    public String getSpeed();  

}

class Tablet implements Computer {

    public String getDeviceType() {

        return "it is a tablet";

    }

    public String getSpeed() {

        return "1GHz";

    }

}

class Example15 {

    public static void main(String[] args) {

        Tablet myTab = new Tablet();

        System.out.println( myTab.getDeviceType() );

        System.out.println( myTab.getSpeed() );

    }

}

As you might have guessed, the output for this example is:

it is a tablet

1GHz

Process finished with exit code 0

Some relevant notes on interfaces follow:

  • Interfaces can extend other interfaces.
  • Unlike classes, which can only extend one class at a time, interfaces can extend multiple interfaces at once. You do so by adding the different interfaces separated by commas.
  • Interfaces have no constructors.
主站蜘蛛池模板: 芜湖县| 彰武县| 曲阳县| 新晃| 贞丰县| 明星| 民丰县| 运城市| 保靖县| 启东市| 那曲县| 翁源县| 万全县| 柘城县| 太和县| 织金县| 盈江县| 磐安县| 常山县| 太和县| 黄龙县| 封丘县| 肇州县| 郑州市| 东兴市| 九江市| 黔江区| 四川省| 措勤县| 潞西市| 绥德县| 大埔县| 西乌珠穆沁旗| 锡林郭勒盟| 平山县| 玉田县| 高州市| 永胜县| 梁河县| 金川县| 南川市|