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. Batch normalization简析

    Batch normalization简析 What is batch normalization 资料来源:https://www.bilibili.com/video/av15997678/?p= ...

  2. SSM框架警告/错误集合

    警告: 1.使用Eclipse的Spring Elements组件的时候发现会提示有警告:Expect at least one bean match() 解决办法:项目可以正常运行,未有报错,在其他 ...

  3. 14 Django之Form和Model Form组件

    一.什么是Form 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用 ...

  4. Category VS Extension 原理详解

    (一)Category 1.什么是Category? category是Objective-C 2.0之后添加的语言特性,别人口中的分类.类别其实都是指的category.category的主要作用是 ...

  5. Celery 初步使用心得

    一. 基本介绍 Celery是一个专注于实时处理和任务调度的分布式任务队列.所谓任务就是消息,消息中的有效载荷中包含要执行任务需要的全部数据. 使用Celery常见场景: Web应用.当用户触发的一个 ...

  6. Oracle笔记(十) 约束

    表虽然建立完成了,但是表中的数据是否合法并不能有所检查,而如果要想针对于表中的数据做一些过滤的话,则可以通过约束完成,约束的主要功能是保证表中的数据合法性,按照约束的分类,一共有五种约束:非空约束.唯 ...

  7. Django框架起步

    一.环境安装 二.创建项目 三.项目目录 四.创建项目应用 五.应用目录 六.第一个响应 七.第一个模板页面 八.第一个重定向 九.url应用移植 十.多应用相同模板页面冲突 十一.静态资源的配置 十 ...

  8. IIS教程:因权限问题被拒绝访问的解决方案

    https://blog.csdn.net/a497785609/article/details/49952281 写了一个类IISAdmin,负责建立.设置.删除虚拟目录,发现在web中调用,遇到权 ...

  9. Django 权限控制配置步骤

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

  10. 牛客练习赛53 E 老瞎眼 pk 小鲜肉 (线段树,思维)

    链接:https://ac.nowcoder.com/acm/contest/1114/E来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...