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

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.

主站蜘蛛池模板: 双峰县| 红桥区| 准格尔旗| 六枝特区| 庐江县| 鲜城| 丰顺县| 固原市| 建瓯市| 新乡市| 手游| 巴塘县| 桑植县| 江川县| 陇西县| 博白县| 浮山县| 玉溪市| 盐城市| 综艺| 汝南县| 磴口县| 简阳市| 滦南县| 汉川市| 墨竹工卡县| 海丰县| 吴江市| 工布江达县| 庆元县| 金沙县| 额尔古纳市| 成武县| 正安县| 疏勒县| 汪清县| 商都县| 巴青县| 巴楚县| 康马县| 神木县|