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) {}//接口可以有主函数 }
public class TestDemo implements Demo{ @Override public void test1() { } @Override public void test2() {} @Override public void test4(){}//重写默认函数,也可以不重写 } abstract class TestDemo01 implements Demo{ }
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!