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

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.

主站蜘蛛池模板: 民权县| 禄丰县| 平果县| 保定市| 衢州市| 平和县| 石阡县| 佛山市| 都江堰市| 阿克| 凤城市| 静安区| 庆安县| 名山县| 漳平市| 册亨县| 稻城县| 伊宁市| 昌宁县| 长沙县| 嵊泗县| 隆昌县| 康保县| 惠水县| 台前县| 托克托县| 易门县| 金平| 巴林左旗| 古田县| 新乡市| 碌曲县| 福海县| 如东县| 剑川县| 方城县| 绍兴市| 霸州市| 阿荣旗| 河曲县| 尼玛县|