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

  • The Java Workshop
  • David Cuartielles Andreas G?ransson Eric Foster Johnson
  • 250字
  • 2021-06-11 13:05:23

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.
主站蜘蛛池模板: 桃江县| 大名县| 株洲市| 金昌市| 柳州市| 建德市| 改则县| 铁岭县| 微山县| 紫阳县| 靖江市| 巴里| 云安县| 杨浦区| 施秉县| 新闻| 黄石市| 汾阳市| 高安市| 怀集县| 揭阳市| 靖远县| 两当县| 威信县| 江川县| 丹凤县| 峨眉山市| 香港 | 仁化县| 玉屏| 彩票| 商丘市| 盐城市| 望都县| 舟曲县| 富裕县| 武宣县| 安化县| 达尔| 黄大仙区| 景洪市|