当前位置:网站首页>The difference between abstract classes and interfaces
The difference between abstract classes and interfaces
2022-07-25 03:05:00 【Live learning programming ideas】
The difference between abstract classes and interfaces
abstract class
- Abstract method —— Only method declaration , There is no concrete realization .
- Abstract method —— Must use abstract Keyword modification . If a class contains abstract methods , We call this class an abstract class , Abstract classes must be used before classes abstract Keyword modification .
- Because abstract classes contain methods without concrete implementation , So you can't create objects with abstract classes .
- If a class does not contain abstract methods , Just use abstract Adornment is also an abstract class . That is to say, an abstract class does not have to contain abstract methods .
- A class containing abstract methods is called an abstract class , But it doesn't mean that there are only abstract methods in an abstract class , It's the same as a normal class , You can also have member variables and common member methods . Be careful , There are three main differences between abstract classes and ordinary classes :
- The abstract method must be public perhaps protected( Because if it's private, Can't be inherited by subclass , Subclass can't implement this method ), The default is public.
- Abstract classes cannot be used to create objects ;
- If a class inherits from an abstract class , The subclass must implement the abstract method of the parent class . If the subclass does not implement the abstract method of the parent class , The subclass must also be defined as abstract class .
Interface
Interface , It's called interface, In software engineering , Interface generics refer to methods or functions that are called by others
- Interfaces can contain Variables and methods . But be careful , Variables in the interface are implicitly specified as public static final Variable ( And it can only be public static final Variable , use private Modification will report compilation error )
- The method is implicitly specified as public abstract The method can only be public abstract Method ( Use other keywords , such as private、protected、static、final And other modifications will report compilation errors ), And all the methods in the interface can't have specific implementation , in other words , The methods in the interface must all be abstract methods . From here, we can see the difference between interface and abstract class , An interface is an extremely abstract type , It's more than an abstract class “ abstract ”, And in general, variables are not defined in the interface .
- To make a class follow a specific set of interfaces, you need to use implements keyword
- It can be seen that , Allow a class to follow multiple specific interfaces . If a non abstract class follows an interface , You have to implement all the methods in this interface . For an abstract class that follows an interface , The abstract methods in this interface can not be implemented .
The difference between abstract classes and interfaces
1. Grammatical differences
- Abstract classes can provide implementation details of member methods , And there is only... In the interface public abstract Method ;
- Member variables in abstract classes can be of various types , The member variables in an interface can only be public static final Type of ;
- Interfaces cannot contain static code blocks and static methods , Abstract classes can have static code blocks and static methods ;
- A class can only inherit one abstract class , A class can implement multiple interfaces .
边栏推荐
- Riotboard development board series notes (V) -- porting u-boot
- Dc-1-practice
- Experiment 4 CTF practice
- Flume's study notes
- Use unicloud cloud function to decode wechat motion steps in applet
- Preliminary foundation JVM
- Unity refers to a variable in another class (its own instance)
- Selenium framework operation steelth.min.js file hides browser fingerprint features
- JS foundation -- JSON
- Edit mathematical formulas in markdown
猜你喜欢

mysql_ Case insensitive

Technical experts from large factories: Thoughts on cloud nativity and software supply chain security

Riotboard development board series notes (6) -- buildreoot building system image

Jenkins plug-in development -- plug-in expansion

Use of stm32cubemonitor Part II - historical data storage and network access

Pypi counts the number of Downloads
![[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality](/img/12/1763571a99e6ef15fb7f9512c54e9b.png)
[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality

Preliminary foundation JVM

Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template

6.0 cancellation of member registration verification code
随机推荐
Learning Record V
Use pytest + allure to show the chart results (3)
List title of force buckle summary
Go multiplexing
Innobackupex parameter description
Dc-1-practice
Operator explanation - C language
Color space (2) -- YUV
Details of happens before rules
JS foundation -- math
Domestic edge computing organization and product research
2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
C: wechat chat software instance (wpf+websocket+webapi+entityframework)
Tp5.1 login configuration method of whether to login public functions (complete login case)
JS written test question -- realize the flat function of array
Conceptual distinction between Po, Bo, VO, dto and POJO
JS written test question -- deep copy of object
Dynamic programming -- Digital DP
Jenkins plug-in development -- plug-in expansion
Study notes of filebeat