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

  • 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.
主站蜘蛛池模板: 高雄县| 呼和浩特市| 大竹县| 大新县| 凤阳县| 和平县| 罗源县| 吐鲁番市| 高要市| 南城县| 都江堰市| 卓资县| 梧州市| 凤山县| 襄樊市| 北碚区| 玉树县| 陆川县| 寻甸| 基隆市| 恩平市| 民乐县| 南溪县| 东阳市| 从江县| 建昌县| 孝感市| 岚皋县| 三明市| 晋州市| 丹阳市| 灵川县| 泸西县| 新津县| 栾城县| 容城县| 鸡东县| 河北区| 康定县| 韶山市| 松阳县|