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. .Net C# EF database first connectionstring

    <connectionStrings> <add name="CupCreditCheckDB" connectionString="metadata= ...

  2. Django中 auto_now_add 和 auto_now 的区别

    auto_now_add = True #创建时添加的时间 修改数据时,不会发生改变 auto_now = True #修改数据的时间,每次修改都会有变动 ........

  3. 在Windows中 , 如何用leakdiag “自动”检测内存泄露 (自动记录日志)

    一.基本用法 在LeakDiag中选择aaa.exe 然后选择Windows Heap Allocator来跟踪heap的使用,按start开始,等一会按log,然后再stop 会在c:\leakdi ...

  4. http GET 和 POST 请求的优缺点和误区

    (1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中) (2)post发送的数据更大(get有url长度限制) (3)post能发送更多的数据类型(get只 ...

  5. 每日一句 Linux, 持续精进

    每日一句 Linux, 持续更新 2019.12.10 1.远程登录 linux 服务器.首先要按照ssh(win10默认是安装了的).命令行窗口,使用 ssh 登录名@serverIp,之后输入密码 ...

  6. Oracle数据库(实例)删除用户和表空间

    删除用户drop user IMPLOCAL cascade; 删除表空间drop tablespace IMPLOCAL including contents and datafiles casca ...

  7. Java基础加强-注解

    /*注解(Annotation)*/(注解相当于一个特殊的类,注解类@interface A) 了解注解及java提供的几个基本注解 1. @SuppressWarnings 指示应该在注释元素(以及 ...

  8. 将excel表格导入到DataGridView

    using System.Data.OleDb; 添加一个button控件,一个textBox控件,用于显示选择路径  private void loadxls() { String fileName ...

  9. Kinect 深度测量原理

    和其他摄像机一样,近红外摄像机也有视场.Kinect摄像机的视野是有限的,如下图所示: 如图,红外摄像机的视场是金字塔形状的.离摄像机远的物体比近的物体拥有更大的视场横截面积.这意味着影像的高度和宽度 ...

  10. Django 权限控制配置步骤

    1.models下面添加权限控制信息: class UserProfile(models.Model): user = models.OneToOneField(User) name = models ...