当前位置:网站首页>The difference between abstract and interface interface

The difference between abstract and interface interface

2022-06-26 05:53:00 Fog distant view

Abstract abstract : Let's start with abstract methods , Abstract methods are not like ordinary methods , It has no method body {}、 And use abstart Keyword modification 、 And abstract methods can only be written in abstract classes ;

e.g. public void student(){...} // Common method

public abstart void student(); // Abstract method

Let's talk about abstract classes , Abstract classes are generally used as parent classes , When inheriting an abstract parent class, you must override the abstract methods of the parent class ( When the subclass is also an abstract class, there is no need to override )

Abstract methods can have constructors 、 Member variables

Interface Interface :Interface The interface is more like a “ pure ” Abstract method of , Abstract methods in an interface can be omitted abstract

, But there are no common methods in the interface , But in the JDK1.8 Start , Interface allows you to define a default method ( Method with method body )e.g. public default void student(){...}, This is equivalent to “ Common methods in interfaces ”;

No constructor is allowed in the interface ;

Members in the interface “ Variable ” Somewhat different , Not variables but constants , Must be responsible for and use in the interface public As modifier

原网站

版权声明
本文为[Fog distant view]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180503422419.html