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

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.
主站蜘蛛池模板: 滦南县| 广西| 锦州市| 唐海县| 河曲县| 锦屏县| 昭觉县| 广宁县| 博白县| 内黄县| 阜平县| 盐城市| 灵山县| 庆阳市| 五大连池市| 赞皇县| 郸城县| 迭部县| 洱源县| 全南县| 高唐县| 六盘水市| 库车县| 丰台区| 高安市| 天柱县| 石阡县| 探索| 隆尧县| 大石桥市| 龙井市| 衡东县| 云林县| 榆中县| 瓮安县| 马山县| 菏泽市| 临沂市| 普兰店市| 彰化市| 金塔县|