- 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.
- Getting Started with Gulp(Second Edition)
- arc42 by Example
- Python從菜鳥(niǎo)到高手(第2版)
- Mastering Python Scripting for System Administrators
- 區(qū)塊鏈:以太坊DApp開(kāi)發(fā)實(shí)戰(zhàn)
- Java虛擬機(jī)字節(jié)碼:從入門(mén)到實(shí)戰(zhàn)
- 自制編程語(yǔ)言
- 組態(tài)軟件技術(shù)與應(yīng)用
- 基于SpringBoot實(shí)現(xiàn):Java分布式中間件開(kāi)發(fā)入門(mén)與實(shí)戰(zhàn)
- 編寫(xiě)高質(zhì)量代碼:改善Objective-C程序的61個(gè)建議
- Mastering Python Design Patterns
- GitHub入門(mén)與實(shí)踐
- C編程技巧:117個(gè)問(wèn)題解決方案示例
- Python GUI Programming Cookbook(Second Edition)
- R語(yǔ)言實(shí)戰(zhàn)(第2版)