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

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.

主站蜘蛛池模板: 繁峙县| 天津市| 黔江区| 沐川县| 孟州市| 天镇县| 湘潭市| 万全县| 贵阳市| 新河县| 尼勒克县| 天门市| 榆树市| 丽水市| 富阳市| 襄樊市| 乌拉特后旗| 昭平县| 定日县| 海晏县| 太保市| 清徐县| 运城市| 维西| 云林县| 略阳县| 嵊泗县| 河津市| 香格里拉县| 沂水县| 南城县| 昭通市| 甘泉县| 革吉县| 夹江县| 苍梧县| 牡丹江市| 苏州市| 柞水县| 铜鼓县| 阿瓦提县|