Difference Between static and default methods in interface
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的更多相关文章
- Java 8 默认方法(Default Methods)
Java 8 默认方法(Default Methods) Posted by Ebn Zhang on December 20, 2015 Java 8 引入了新的语言特性——默认方法(Default ...
- JAVA 8 默认方法-Default Methods
什么是默认方法-Default Methods 简单的说,就是可以在接口中定义一个已实现方法,且该接口的实现类不需要实现该方法: 如下示例: interface GreetingService { v ...
- 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 ...
- 【转载】#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 ...
- java的this static public protected private abstract interface 在python的对应,java python一些区别
1.因为工作的原因,最近使用了三个多月的java作为主力语言.很早之前在菜鸟教程也看过java文档两遍,但实践少,处于能看懂写出来不流畅的状态(对于java必须要略懂,不能能看到就头疼跳过,因为现在百 ...
- 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 ...
- Static and Instance Methods in JavaScript
class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...
- Static Fields and Methods
If you define a field as static, then there is only one such field per class. In contrast, each obje ...
- 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 ...
随机推荐
- Python文件操作——读写图片,音频,视频
注意:其实就是将mode="rb"或者mode="wb",因为图片,视频,音频就是二进制进行读取,b 代表binary ,其他的和一般文件操作步骤一样,另外, ...
- js组合继承
//组合继承指的是将原型链和借用构造函数(call.apply)的技术组合到一起,从而发挥二者之长的一种继承模式,//其背后的思路就是使用原型链实现对原型属性和方法的继承://而通过借用构造函数来实现 ...
- js Indexof的用法
JavaScript中indexOf()函数 JavaScript中indexOf()函数方法返回一个整数值,指出 String 对象内子字符串的开始位置.如果没有找到子字符串,则返回 -1.如果 ...
- SIP协议入门:初学者必须明白的几个重要概念
SIP协议初学者必须明白的几个重要概念 http://blog.sina.com.cn/s/blog_60e1d7bb0100f6er.html 一. SIP协议的分层结构 SIP是一个分层结构协议, ...
- Docker Registry搭建
一.前言 Docker官方镜像仓库 访问速度很慢,Docker Registry允许搭建我们自己的镜像仓库,为实现镜像拉取.推送提供便利. 二.安装与启动 1.创建目录 mkdir /usr/loca ...
- django 权限控制精简版
视图代码: 视图代码 def index(request): return render(request,'index.html') def login(request): if request.me ...
- 【ASE高级软件工程】第一次结对作业
问题定义 具体规则见:讲义.大致规则如下: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数 ...
- Miniconda虚拟环境管理工具命令方法
创建制定Python版本的虚拟环境 conda create --name 虚拟环境名称 Python=3.7.3(版本号) 进入指定虚拟环境 conda activate 虚拟环境名称 退出虚拟环境 ...
- css 之引入自定义字体/特殊字体-----使用ttf格式语言包
1.准备好需要的 .ttf 格式的语言包,在css导入: @font-face { font-family: myFont; src: url('../assets/font/Oswald-SemiB ...
- zznu-2183: 口袋魔方
大致题意: 题目描述 口袋魔方又称为迷你魔方,通俗的来讲就是二阶魔方,只有八个角块的魔方,如图所示. 二阶魔方8个角块的位置均可进行任意互换(!种状态),如果以一个角块不动作为参考角块,其他7个 角块 ...