I was learning through interfaces when I noticed that you can now define static and default methods in an interface.

public interface interfacesample2 {
public static void method() {
System.out.println("hello world");
} public default void menthod3() {
System.out.println("default print");
}
}

Kindly explain the difference of the two and also if there's an example of when we would use this would be nice. A little confused on Interfaces.


Differences between static and default methods in Java 8:

1) Default methods can be overriden in implementing class, while static cannot.

2) Static method belongs only to Interface class, so you can only invoke static method on Interface class, not on class implementing this Interface, see:

public interface MyInterface {
default void defaultMethod(){
System.out.println("Default");
} static void staticMethod(){
System.out.println("Static");
}
} public class MyClass implements MyInterface { public static void main(String[] args) { MyClass.staticMethod(); //not valid - static method may be invoked on containing interface class only
MyInterface.staticMethod(); //valid
}
}

3) Both class and interface can have static methods with same names, and neither overrides other!

public class MyClass implements MyInterface {

    public static void main(String[] args) {

        //both are valid and have different behaviour
MyClass.staticMethod();
MyInterface.staticMethod();
} static void staticMethod(){
System.out.println("another static..");
}
}

Difference Between static and default methods in interface的更多相关文章

  1. Java 8 默认方法(Default Methods)

    Java 8 默认方法(Default Methods) Posted by Ebn Zhang on December 20, 2015 Java 8 引入了新的语言特性——默认方法(Default ...

  2. JAVA 8 默认方法-Default Methods

    什么是默认方法-Default Methods 简单的说,就是可以在接口中定义一个已实现方法,且该接口的实现类不需要实现该方法: 如下示例: interface GreetingService { v ...

  3. Core Java Volume I — 4.4. Static Fields and Methods

    4.4. Static Fields and MethodsIn all sample programs that you have seen, the main method is tagged w ...

  4. 【转载】#349 - The Difference Between Virtual and Non-Virtual Methods

    In C#, virtual methods support polymorphism, by using a combination of the virtual and override keyw ...

  5. java的this static public protected private abstract interface 在python的对应,java python一些区别

    1.因为工作的原因,最近使用了三个多月的java作为主力语言.很早之前在菜鸟教程也看过java文档两遍,但实践少,处于能看懂写出来不流畅的状态(对于java必须要略懂,不能能看到就头疼跳过,因为现在百 ...

  6. swift的static和class修饰符---What is the difference between static func and class func in Swift?

    Special Kinds of Methods Methods associated with a type rather than an instance of a type must be ma ...

  7. Static and Instance Methods in JavaScript

    class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...

  8. Static Fields and Methods

    If you define a field as static, then there is only one such field per class. In contrast, each obje ...

  9. eclipse default handler IHandler interface “the chosen operation is not enabled”

    NOTE: These two methods: Tip: Subclass AbstractHandler rather than implementing IHandler. but you ca ...

随机推荐

  1. idea的EasyCode使用

    EasyCode可以自动根据表格生成:entity,dao,service,serviceImpl,controller 使用方法: 一.安装EasyCode插件: File-setting-Plug ...

  2. css流程图

     图片链接:https://mp.processon.com/view/link/5da65435e4b0ea86c2b1fb05 之前是图片链接是有点问题,不知道什么原因被删除了,现在已经更新了,如 ...

  3. 【小知识点】去除inline-block元素间间距的办法

    之前一直用float浮动方法布局,因为display:inline-block有间隙,现在找到办法了.在父元素上面加font-sise:0,就可以了. 效果如图: 代码如下: <!DOCTYPE ...

  4. 服务框架 Pigeon 的设计与实现

    1.服务框架Pigeon架构 监控系统 - CAT,负责调用链路分析.异常监控告警 配置中心 - Lion,负责一些开关配置读取 服务治理 - Governor 一个interface定义为一个服务, ...

  5. 第二章、前端之css

    目录 第二章.前端之css 一.css介绍 二.css语法 三.css几种引入方式 四.css选择器 五.css属性相关 六.css盒子模型 第二章.前端之css 一.css介绍 css(Cascad ...

  6. OverflowError:django signed integer is greater than maximum 数据库日期字段相关错

    使用django中的默认数据库sqlite3, 在pycharm中录入日期字段相关信息结果出现问题 在保存的时候如图 直接在界面选择的日期变成了时间戳, 并且在获取数据的时候报错 经过查询之后(舔大佬 ...

  7. Linux学习笔记(四)Linux常用命令:帮助命令

    一.帮助命令man [man] [命令] 例如: man ls man的级别 man -f [命令]  相当于  whereis [命令] 可查看该命令有几个等级,进而可以通过 man [等级数] [ ...

  8. 记一次引用maven插件报错解决方法

    1.报错信息如图: plugin org.springframework.boot:spring-boot-maven-plugin not found 2.解决方案: maven的配置文件[sett ...

  9. SQLite3学习笔记(1)

    命令: DDL-数据定义: CREATE -- 创建一个新的表,一个表的视图,或者数据库中的其他对象 ALTER -- 修改数据库中的某个已有的数据对象,比如一个表 DROP -- 删除整个表,或者表 ...

  10. 一些网站后台模板源码分析 Particleground.js 验证码

    转: https://blog.csdn.net/bcbobo21cn/article/details/51271750 1 灰色简洁企业后台管理模板 效果: 看下项目结构: 它使用了moderniz ...