当前位置:网站首页>What is the difference between an interface and an abstract class?

What is the difference between an interface and an abstract class?

2022-06-11 09:45:00 Six years of Surabaya

The difference between interface and abstract class ?

Let me first talk about the two of them Grammatical differences Well , Then let's talk about my personal understanding .

  1. Abstract classes can have specific methods and properties ,   Interfaces can only have abstract methods and constants .
  2. Abstract class use abstract modification , Interface to use interface modification
  3. A class can only inherit one class , But you can implement multiple interfaces .
  4. Abstract classes can have either ordinary methods or abstract methods , Interfaces are full of abstract methods ,JDK1.8 There can also be specific methods in the interface of , But you have to use static Or is it default To modify .
  5. The default modifier of all methods in the interface is public abstract, The default modifier for member variables in the interface is pulbic static final.

What I just said is the grammatical difference between interfaces and abstract classes , Now let me talk about some of my Personal understanding .

Abstract is actually relative to concrete objects , Matianle , Man , human beings , biological , The higher the level of abstraction , The wider the coverage , The less it means .

Abstract classes are mainly used to abstract classes , Interface is mainly used to abstract functions . To put it bluntly, the abstract class represents what the object is . The interface represents what the object can do . For example, there are men and women , Their abstract class is human . explain , They're all human . People can eat , Cats can also eat , At this time you can put “ Eat something ” Defined as an interface , Then let these classes implement it . as for When to use abstract classes , When to use interfaces , My understanding is to see whether there is common business logic among multiple subclasses , If there are any, use abstract classes and put these logic into the abstract classes , Otherwise, use the interface .

原网站

版权声明
本文为[Six years of Surabaya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012233283542.html