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

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.

主站蜘蛛池模板: 平乐县| 阿拉尔市| 盐池县| 双流县| 鄂伦春自治旗| 赤峰市| 咸丰县| 桂阳县| 台江县| 天等县| 龙川县| 河源市| 三门县| 报价| 南涧| 营山县| 兰考县| 英超| 郓城县| 阿鲁科尔沁旗| 玉山县| 屏东县| 凤凰县| 张北县| 蒙自县| 宁河县| 郴州市| 商都县| 锡林郭勒盟| 东明县| 鄱阳县| 泰和县| 洱源县| 马龙县| 视频| 二手房| 和平区| 舒兰市| 汤阴县| 孝义市| 万全县|