本文共 7115 字,大约阅读时间需要 23 分钟。
Difference between Abstract Class and Interface is one of the popular interview questions. Abstract Class and Interface are a core part of the Java programming language. Whether to choose an interface or abstract class is a design decision that every architect faces.
抽象类和接口之间的区别是流行的采访问题之一。 抽象类和接口是Java编程语言的核心部分。 选择接口还是抽象类是每个架构师都面临的设计决策。
In my last articles, I have provided as much as possible details about and .
在上一篇文章中,我提供了有关和尽可能多的细节。
In this post, we will learn about the difference between abstract class and Interface and when should we use interface over the abstract class and vice versa.
在本文中,我们将了解抽象类和接口之间的区别,以及何时应在抽象类上使用接口,反之亦然。
abstract
keyword is used to create an abstract class and it can be used with methods also whereas interface
keyword is used to create interface and it can’t be used with methods. abstract
关键字用于创建抽象类,并且还可以与方法一起使用,而interface
关键字用于创建接口,并且不能与方法一起使用。 extends
keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses use implements
keyword to implement interfaces and should provide implementation for all the methods declared in the interface. 子类使用extends
关键字来扩展抽象类,除非子类也是抽象类,否则它们需要提供抽象类中所有已声明方法的实现,而子类则使用implements
关键字来实现接口,并且应为在中声明的所有方法提供实现。接口。 abstract
keyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations. 抽象类具有普通Java类的所有功能,但我们无法实例化它。 我们可以使用abstract
关键字将类抽象化,但是接口是完全不同的类型,并且只能具有公共静态最终常量和方法声明。 main()
method but we can’t run an interface because they can’t have main method implementation. 如果抽象类具有main()
方法,则可以运行该类,但由于它们不能具有主方法的实现,因此无法运行接口。 That’s all for the difference between an interface and abstract classes, now we can move on to know when should we use Interface over Abstract class and vice versa.
这就是接口和抽象类之间的区别 ,现在我们可以继续了解何时应该在抽象类上使用接口,反之亦然。
Whether to choose between Interface or abstract class for providing a contract for subclasses is a design decision and depends on many factors. Let’s see when Interfaces are the best choice and when can we use abstract classes.
在接口还是抽象类之间进行选择以为子类提供合同是一个设计决定,取决于许多因素。 让我们看看什么时候接口是最佳选择,什么时候可以使用抽象类。
Using interfaces and abstract classes together is the best approach to design a system. For example, in JDK java.util.List
is an interface that contains a lot of methods, so there is an abstract class java.util.AbstractList
that provides a skeletal implementation for all the methods of List interface so that any subclass can extend this class and implement only required methods.
一起使用接口和抽象类是设计系统的最佳方法。 例如,在JDK java.util.List
, java.util.List
是一个包含许多方法的接口,因此存在一个抽象类java.util.AbstractList
,它为List接口的所有方法提供了基本实现,以便任何子类都可以扩展此方法。类并仅实现必需的方法。
We should always start with an interface as the base and define methods that every subclass should implement and then if there are some methods that only certain subclass should implement, we can extend the base interface and create a new interface with those methods.
我们应该始终以接口作为基础,并定义每个子类都应实现的方法,然后,如果某些方法仅某些子类应实现,则可以扩展基本接口并使用这些方法创建新接口。
The subclasses will have the option to chose between the base interface or the child interface to implement according to its requirements.
子类可以选择在基本接口或子接口之间进行选择,以根据其要求进行实现。
If the number of methods grows a lot, it’s not a bad idea to provide a skeletal abstract class implementing the child interface and providing flexibility to the subclasses to chose between interface and an abstract class.
如果方法的数量增加很多,那么提供一个框架抽象类来实现子接口并为子类提供灵活性以在接口和抽象类之间进行选择就不是一个坏主意了。
From Java 8 onwards, we can have method implementations in the interfaces. We can create default as well as static methods in the interfaces and provide an implementation for them.
从Java 8开始,我们可以在接口中使用方法实现。 我们可以在接口中创建默认方法和静态方法,并为其提供实现。
This has bridged the gap between abstract classes and interfaces and now interfaces are the way to go because we can extend it further by providing default implementations for new methods.
这弥合了抽象类和接口之间的鸿沟,现在接口才是可行的方法,因为我们可以通过为新方法提供默认实现来进一步扩展接口。
For more details, check out .
有关更多详细信息,请查看 。
翻译自:
转载地址:http://vfqzd.baihongyu.com/