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. 3037 插板法+lucas

    先说下lucas定理 1)Lucas定理:p为素数,则有: (2)证明: n=(ak...a2,a1,a0)p = (ak...a2,a1)p*p + a0 =  [n/p]*p+a0 (注意 这里( ...

  2. js实现div转图片并保存

    最近工作中遇到的需求,将div转成图片并保存. 1.准备需要用到的js插件jquery-1.8.2.js,html2canvas.min.js(将div转换为canvas),bluebird.js(用 ...

  3. StoneTab标签页CAD插件 3.2.5

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  4. asp.net core 中hangfire面板的配置及使用

    1.定义校验授权类DyDashboardAuthorizationFilter /// <summary> /// Hangfire仪表盘配置授权 /// </summary> ...

  5. asp.net frameworke处理程序的作用

    1 向客户端发送响应的工作都由处理程序完成 2 任何实现System.web.ihttpHandler接口的类都可以作为传入的http请求的目标 3 如果需要重复使用自定义处理程序对象,需要创建自定义 ...

  6. 【转】SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递

    实现思路: 1:准备一个ThreadLocal变量,供线程之间共享. 2:每个微服务对所有过来的Feign调用进行过滤,然后从请求头中获取User用户信息,并存在ThreadLocal变量中. 3:每 ...

  7. 使用jMeter构造大量并发HTTP请求进行微服务性能测试

    比如我开发好了一个微服务,想测试其在大并发请求下的性能表现如何. 比较方便的一个做法是使用工具jMeter来构造这些请求. 创建一个新的工程: 创建一个新的Thread Group,下图意思是这个工程 ...

  8. DDOS 攻击的防范教程--转载自阮一峰的博客

    一个多月前,我的个人网站遭受 DDOS 攻击,下线了50多个小时.这篇文章就来谈谈,如何应对这种攻击. 需要说明的是,我对 DDOS 并不精通,从没想过自己会成为攻击目标.攻击发生以后,很多素昧平生的 ...

  9. OS 常见函数使用

    os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...

  10. Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1428942566812653608

    这个问题也是某天做一个上传文件功能发生的.然后在网上查找的资料,整理了这几个解决方案. 1.在application.yml文件中设置multipart location ,并重启项目 spring: ...