introduction h2>
before JDK1.8, interface did not provide any concrete implementation. It was described in JAVA programming ideas as follows: “interface is a keyword that produces a completely abstract class. It does not provide any concrete implementation at all. It allows the author to determine the method name, argument list, and return type, but does not have any method body. The interface provides the form, not any concrete implementation.
but this limitation is broken in JDK1.8, where interfaces allow you to define default methods and static methods.
code example
defines an IHello interface
public interface IHello {
//抽象方法
void sayHi();
//静态方法
static void sayHello() {
System.out.println("static method : say Hello!");
}
//默认方法
default void sayByebye() {
System.out.println("default method : say bye!");
}
}
defines an IHello interface implementation class
public class HelloImpl implements IHello {
//实现抽象方法
@Override
public void sayHi() {
System.out.println("normal method:say hi");
}
}
The
method call
public class App
{
public static void main(String[] args)
{
HelloImpl helloImpl = new HelloImpl();
//对于abstract抽象方法通过实例对象来调用
helloImpl.sayHi();
//default 方法只能通过实例对象来调用
helloImpl.sayByebye();
//静态方法通过 接口名.方法名()来调用
IHello.sayHello();
//接口是不允许new的,如果使用new后面必须跟上一对花括号用于实现抽象方法,这种方式被称为匿名实现类,匿名实现类是一种没有名称的实现类
//匿名实现类的好处:不用再单独声明一个类 缺点:由于没有名称。不能重复使用,只能使用一次
new IHello() {
@Override
public void sayHi() {
System.out.println("normal method;say hi 新定义");
}
}.sayHi();
}
}
normal method: say hi
default method: say bye!> static method: say Hello!
normal method: say hi new definition
Read More:
- [Solved] error: method does not override or implement a method from a supertype
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- [Solved] Mvel2 Error: java.lang.VerifyError method: getKnownEgressType signature
- [Solved] IDEA java Cannot resolve method ‘getName‘ in ***
- Java Web uses AJAX POST Method 405 terror [Solved]
- [Solved] Invocation of init method failed; nested exception is java.lang.NoSuchMethodError:
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- Problems and causes of Java’s main function format (public static void main (string args()))
- Initializingbean Interface & Applicationcontextaware Interface in Springboot
- [Solved] “status“:405,“error“ Request method ‘POST‘ not supported“
- Comparison method violates its general contract [How to Solve]
- Non empty judgment method: the difference between isnotempty and isnotblank. isNullOrEmpty
- Build a mybatis and it will appear session.selectOne Method error
- [Solved] Error creating bean with name ‘mvcContentNegotiationManager‘: Lookup method resolution failed
- [Solved] The main method caused an error: Could not deploy Yarn job cluster.
- The controller error: the XX method in the service cannot be found