Home > Java > JavaBase > How to implement rewriting in interface in java

How to implement rewriting in interface in java

王林
Release: 2019-12-07 12:01:30
forward
4282 people have browsed it

How to implement rewriting in interface in java

Ordinary class implements interface

1. Both ordinary methods and abstract methods must be rewritten;

2. The default method can be rewritten or not Rewrite.

Abstract class implements interface

Online video tutorial sharing:java teaching video

by abstract If you use a class to implement an interface, you don't have to override the interface's methods. You can not rewrite all methods or only rewrite some methods.

public interface Demo {
	 public void test1();//普通方法,需要重写
	 public abstract void test2();//抽象方法
	 public static void test3() {}//静态方法,接口中的静态方法不能被实现类继承和子接口继承
	 public default void test4(){}//默认方法可以直接使用实现类的对象进行调用,也可以在实现类中对其进行覆盖重写。
	 public static void main(String[] args) {}//接口可以有主函数
}
Copy after login
public class TestDemo implements Demo{

	@Override
	public void test1() {
	}

	@Override
	public void test2() {}
	
	@Override
	public void test4(){}//重写默认函数,也可以不重写

	
	

}

abstract class TestDemo01 implements Demo{
	
}
Copy after login

Note:

1. Default methods and static methods need to have method bodies;

2. Static methods cannot be overridden.

Recommended related articles and tutorials: Introduction to java language

The above is the detailed content of How to implement rewriting in interface in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template