Java中抽象类和接口之间的区别

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 java interface and abstract class.

在上一篇文章中,我提供了有关java接口抽象类的尽可能多的细节。

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.

在本文中,我们将了解抽象类和接口之间的区别,以及何时应在抽象类上使用接口,反之亦然。

抽象类和接口之间的区别 (Difference between Abstract Class and Interface)

  1. 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关键字用于创建接口,并且不能与方法一起使用。
  2. Subclasses use 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关键字来实现接口,并且应为在中声明的所有方法提供实现。接口。
  3. Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations. Note that from Java 8 onwards, we can create default and static methods in interface that contains the method implementations.

    抽象类可以具有实现的方法,而接口提供绝对抽象,并且不能具有任何方法的实现。 请注意,从Java 8开始,我们可以在包含方法实现的接口中创建默认方法和静态方法。
  4. Abstract classes can have constructors but interfaces can’t have constructors.

    抽象类可以具有构造函数,但接口不能具有构造函数。
  5. Abstract class have all the features of a normal java class except that we can’t instantiate it. We can use 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关键字将类抽象化,但是接口是完全不同的类型,并且只能具有公共静态最终常量和方法声明。
  6. Abstract classes methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.

    抽象类方法可以具有公共,私有,受保护,静态的访问修饰符,但是接口方法是隐式公共和抽象的,我们不能将任何其他访问修饰符与接口方法一起使用。
  7. A subclass can extend only one abstract class but it can implement multiple interfaces.

    子类只能扩展一个抽象类,但可以实现多个接口。
  8. Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.

    抽象类可以扩展其他类并实现接口,但是接口只能扩展其他接口。
  9. We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.

    如果抽象类具有main()方法,则可以运行该类,但由于它们不能具有主方法的实现,因此无法运行接口。
  10. Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.

    接口用于定义子类的协定,而抽象类也定义协定,但是它可以提供其他方法实现以供子类使用。

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.

这就是接口和抽象类之间区别 ,现在我们可以继续了解何时应该在抽象类上使用接口,反之亦然。

接口或抽象类 (Interface or Abstract Class)

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.

在接口还是抽象类之间进行选择以为子类提供合同是一个设计决定,取决于许多因素。 让我们看看什么时候接口是最佳选择,什么时候可以使用抽象类。

  1. Java doesn’t support multiple class level inheritance, so every class can extend only one superclass. But a class can implement multiple interfaces. So most of the times Interfaces are a good choice for providing the base for class hierarchy and contract. Also coding in terms of interfaces is one of the best practices for coding in java.

    Java不支持多个类级别的继承,因此每个类只能扩展一个超类。 但是一个类可以实现多个接口。 因此,大多数时候,接口是为类层次结构和协定提供基础的好选择。 此外,在接口方面进行编码也是用Java进行编码的最佳实践之一。
  2. If there are a lot of methods in the contract, then abstract class is more useful because we can provide a default implementation for some of the methods that are common for all the subclasses. Also if subclasses don’t need to implement a particular method, they can avoid providing the implementation but in case of interface, the subclass will have to provide the implementation for all the methods even though it’s of no use and implementation is just empty block.

    如果协定中有很多方法,那么抽象类会更有用,因为我们可以为所有子类通用的某些方法提供默认实现。 同样,如果子类不需要实现特定的方法,则可以避免提供实现,但是在使用接口的情况下,子类将必须为所有方法提供实现,即使它没有用,实现也只是空块。
  3. If our base contract keeps on changing then interfaces can cause issues because we can’t declare additional methods to the interface without changing all the implementation classes, with the abstract class we can provide the default implementation and only change the implementation classes that are actually going to use the new methods.

    如果我们的基本合同一直在变化,那么接口会引起问题,因为我们不能在不更改所有实现类的情况下向接口声明其他方法,使用抽象类,我们可以提供默认实现,而仅更改实际使用的实现类使用新方法。

同时使用Abstract类和Interface (Use Abstract classes and Interface both)

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.Listjava.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.

如果方法的数量增加很多,那么提供一个框架抽象类来实现子接口并为子类提供灵活性以在接口和抽象类之间进行选择就不是一个坏主意了。

Java 8界面更改 (Java 8 interface changes)

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 Java 8 interface default static methods.

有关更多详细信息,请查看Java 8接口默认静态方法

翻译自: https://www.journaldev.com/1607/difference-between-abstract-class-and-interface-in-java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值