- Selenium WebDriver Quick Start Guide
- Pinakin Chaubal
- 143字
- 2021-06-24 18:26:25
Default and static methods in an interface
Up until Java 1.7, it was not possible to define a method inside an interface. Now, 1.8 introduces the default methods through which we can provide implementation for a method inside the interface. Let's see an example of this here:
interface Phone{
void dial();
default void text() {
System.out.println("Texting a message");
}
}
Static methods in Java are those methods that can be invoked without creating an object of a particular class, provided that the static method is in that particular class. In Java 8, static methods can be defined inside an interface, as shown here:
interface Phone {
inx x;
void changeRingtone();
static void text() {
System.out.println("Texting");
}
}
public class PhoneDemo {
public static void main(String[] args) {
Phone.text();
}
}
You can invoke the text() method directly using the name of the interface.
推薦閱讀
- Linux運維之道(第3版)
- Containerization with LXC
- 白話區塊鏈
- Java EE 8 Design Patterns and Best Practices
- 深入淺出Node.js
- Mastering Windows 8 C++ App Development
- 統信UOS應用開發進階教程
- Learn Quantum Computing with Python and IBM Quantum Experience
- Linux內核修煉之道
- CSS揭秘
- Windows網絡編程(第2版)
- Learning IBM Watson Analytics
- 電腦辦公(Windows 7+Office 2016)入門與提高
- Windows 8玩全不求人
- Mastering Eclipse Plug-in Development