接口和抽象类有什么区别?

发布于 2024-08-15 04:57:09 字数 21 浏览 3 评论 0 原文

接口和抽象类到底有什么区别?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

水波映月 2024-08-22 04:57:09

接口

接口是一个契约:编写接口的人说,“嘿,我接受这样的事情”,使用接口的人说“好吧,我写的类看起来就是这样的”。

接口是一个空壳。只有方法的签名,这意味着方法没有主体。界面什么也做不了。这只是一个模式。

例如(伪代码):

// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}

实现一个接口消耗很少的CPU,因为它不是一个类,只是一堆名称,因此不需要执行任何昂贵的查找。当它很重要时,例如在嵌入式设备中,它就很棒。


抽象类

与接口不同,抽象类是类。它们的使用成本更高,因为当您继承它们时需要进行查找。

抽象类看起来很像接口,但它们还有更多的东西:您可以为它们定义行为。更多的是一个人说,“这些类应该看起来像那样,并且它们有共同点,所以填空!”。

例如:

// I say all motor vehicles should look like this:
abstract class MotorVehicle
{

    int fuel;

    // They ALL have fuel, so lets implement this for everybody.
    int getFuel()
    {
         return this.fuel;
    }

    // That can be very different, force them to provide their
    // own implementation.
    abstract void run();
}

// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
    void run()
    {
        print("Wrroooooooom");
    }
}

实现

虽然抽象类和接口应该是不同的概念,但实现使该声明有时不真实。有时,他们甚至不是你想象的那样。

在 Java 中,这条规则被严格执行,而在 PHP 中,接口是没有声明方法的抽象类。

在 Python 中,抽象类更像是一种可以从 ABC 模块中获得的编程技巧,并且实际上使用元类,因此也使用类。接口与这种语言中的鸭子类型更相关,它是调用描述符的约定和特殊方法(__method__ 方法)之间的混合。

和往常一样,编程也有理论、实践和另一种语言的实践:-)

Interfaces

An interface is a contract: The person writing the interface says, "hey, I accept things looking that way", and the person using the interface says "OK, the class I write looks that way".

An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern.

For example (pseudo code):

// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}

Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there isn't any expensive look-up to do. It's great when it matters, such as in embedded devices.


Abstract classes

Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them.

Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them. It's more about a person saying, "these classes should look like that, and they have that in common, so fill in the blanks!".

For example:

// I say all motor vehicles should look like this:
abstract class MotorVehicle
{

    int fuel;

    // They ALL have fuel, so lets implement this for everybody.
    int getFuel()
    {
         return this.fuel;
    }

    // That can be very different, force them to provide their
    // own implementation.
    abstract void run();
}

// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
    void run()
    {
        print("Wrroooooooom");
    }
}

Implementation

While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes untrue. Sometimes, they are not even what you think they are.

In Java, this rule is strongly enforced, while in PHP, interfaces are abstract classes with no method declared.

In Python, abstract classes are more a programming trick you can get from the ABC module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it's a mix between conventions and special methods that call descriptors (the __method__ methods).

As usual with programming, there is theory, practice, and practice in another language :-)

雪落纷纷 2024-08-22 04:57:09

抽象类接口是:

  • 抽象类可以有常量、成员、方法存根(没有主体的方法)和定义的方法,而接口可以只有常量方法存根

  • 抽象类的方法和成员可以定义为任何可见性,而接口的所有方法必须定义为public(它们默认定义为public) .

  • 继承抽象类时,具体子类必须定义抽象方法,而抽象类可以从父类扩展另一个抽象类和抽象方法不必定义。

  • 同样,扩展另一个接口的接口不负责实现父接口的方法。这是因为接口无法定义任何实现。

  • 子类只能扩展单个类(抽象类或具体类),而接口可以扩展或者类可以实现多个其他接口

  • 子类可以定义具有相同或较少限制的可见性的抽象方法,而实现接口的类必须定义具有完全相同的可见性(公共)的方法。

The key technical differences between an abstract class and an interface are:

  • Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.

  • Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).

  • When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined.

  • Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.

  • A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.

  • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

素衣风尘叹 2024-08-22 04:57:09

接口仅包含功能的定义/签名,如果我们有一些通用的功能以及通用的签名,那么我们需要使用抽象类。通过使用抽象类,我们可以同时提供行为和功能。继承抽象类的其他开发人员可以轻松使用此功能,因为他们只需要填写空白即可。

Interface 抽象类
Interface 支持多种实现。 抽象类不支持多重继承。
接口不包含数据成员 抽象类包含数据成员
接口不包含构造函数 抽象类包含构造函数
接口仅包含不完整成员(成员签名) 抽象类同时包含不完整(抽象)和完整成员
接口不能具有访问修饰符默认所有内容都假定为 public 抽象类可以包含 subs、函数、属性的访问修饰符
接口的成员不能是静态的 只有抽象类的完整成员可以是静态的

取自:

http://www.dotnetbull.com/2011/11/difference- Between-abstract-class-and。 html

http:// www.dotnetbull.com/2011/11/what-is-abstract-class-in-c-net.html
http://www.dotnetbull.com/2011 /11/what-is-interface-in-c-net.html

An Interface contains only the definition / signature of functionality, and if we have some common functionality as well as common signatures, then we need to use an abstract class. By using an abstract class, we can provide behavior as well as functionality both in the same time. Another developer inheriting abstract class can use this functionality easily, as they would only need to fill in the blanks.

Interface Abstract class
Interface support multiple implementations. Abstract class does not support multiple inheritance.
Interface does not contain Data Member Abstract class contains Data Member
Interface does not contain Constructors Abstract class contains Constructors
An interface Contains only incomplete member (signature of member) An abstract class Contains both incomplete (abstract) and complete member
An interface cannot have access modifiers by default everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties
Member of interface can not be Static Only Complete Member of abstract class can be Static

Taken from:

http://www.dotnetbull.com/2011/11/difference-between-abstract-class-and.html

http://www.dotnetbull.com/2011/11/what-is-abstract-class-in-c-net.html
http://www.dotnetbull.com/2011/11/what-is-interface-in-c-net.html

难以启齿的温柔 2024-08-22 04:57:09

可以在这里找到解释: http://www.developer.com/lang/php/article.php/3604111/PHP-5-OOP-Interfaces-Abstract-Classes-and-the-Adapter-Pattern。嗯

抽象类是这样的类
仅部分实施
程序员。它可能包含一个或多个
抽象方法。抽象方法
只是一个函数定义
用于告诉程序员
方法必须在孩子身上实施
类。

接口类似于抽象
班级;确实接口占据了
与类和抽象相同的命名空间
类。因此,您不能
定义一个同名的接口
作为一个班级。接口是一个完整的
抽象类;它的任何方法都没有
被实现而不是一个类
从它的子分类,据说
实现该接口。

无论如何,我发现这个接口的解释有点令人困惑。更常见的定义是:接口定义了实现类必须履行的契约。接口定义由公共成员的签名组成,没有任何实现代码。

An explanation can be found here: http://www.developer.com/lang/php/article.php/3604111/PHP-5-OOP-Interfaces-Abstract-Classes-and-the-Adapter-Pattern.htm

An abstract class is a class that is
only partially implemented by the
programmer. It may contain one or more
abstract methods. An abstract method
is simply a function definition that
serves to tell the programmer that the
method must be implemented in a child
class.

An interface is similar to an abstract
class; indeed interfaces occupy the
same namespace as classes and abstract
classes. For that reason, you cannot
define an interface with the same name
as a class. An interface is a fully
abstract class; none of its methods
are implemented and instead of a class
sub-classing from it, it is said to
implement that interface.

Anyway I find this explanation of interfaces somewhat confusing. A more common definition is: An interface defines a contract that implementing classes must fulfill. An interface definition consists of signatures of public members, without any implementing code.

究竟谁懂我的在乎 2024-08-22 04:57:09

我不想强调这些差异,这些差异在许多答案中已经说过(关于接口中变量的公共静态最终修饰符以及对抽象类中受保护的私有方法的支持)

简单来说,我想说:

接口:通过多个不相关的对象实现契约

抽象类:在多个相关对象之间实现相同或不同的行为

Oracle 文档页面 提供了相同的一般指南。

抽象类的优点

  1. 代码和成员可以在相关类之间共享。
  2. 支持非静态/非最终字段。

界面优点

  1. 在不相关的类之间建立关系。例如,许多不相关的对象可以实现 SerializedExternalized 接口。
  2. 定义合同
  3. 多重继承

抽象类与具体类建立“是”关系。接口为类提供“具有”功能。

如果您正在寻找 Java 作为编程语言,这里还有一些更新:

Java 8 缩小了接口<之间的差距/code> 和 abstract 类在某种程度上通过提供 default 方法功能。 接口没有方法的实现现在不再有效。

接口用例:

  1. 定义一个合约,并且必须由其实现者来实现。

    举一个对象自定义序列化的例子。我让你的类实现 Externalized 接口,你必须实现以下方法的合约

     void writeExternal(ObjectOutput out)
     无效readExternal(ObjectInput in)
    
  2. FactoryMethod(创建模式)的实现可以通过接口完成,并且由实现者实现

    示例:工厂模式。什么时候使用工厂方法?

  3. 策略设计模式(行为模式)的实现:你有一系列策略,并且希望在运行时切换到不同的策略

    示例:策略模式的真实示例


  4. 您可以在命令设计模式(行为)中找到接口的使用

    示例:使用命令设计模式

抽象类用例:

  1. 基类中没有实现(具有共享数据),只有子类必须定义自己的实现。您需要抽象类而不是接口,因为您想与子类共享状态。

  2. 抽象类用于抽象工厂设计模式实现(创建模式)

    示例:工厂模式。什么时候使用工厂方法?

  3. 您可以在设计模式中找到许多抽象类的用例。

    示例:桥接模式(结构模式)

    桥接模式是否将抽象与实现解耦?< /a>


  4. 装饰器设计模式中使用抽象类

    示例:何时使用装饰器模式?

  5. 抽象类最适合必须创建骨架的模板方法模式。

    示例:JDK 中的模板设计模式,找不到定义要按顺序执行的方法集的方法

I don't want to highlight the differences, which have been already said in many answers ( regarding public static final modifiers for variables in interface & support for protected, private methods in abstract classes)

In simple terms, I would like to say:

interface: To implement a contract by multiple unrelated objects

abstract class: To implement the same or different behavior among multiple related objects

oracle documentation page provides general guidelines for the same.

Abstract class pros :

  1. Code and members can be shared among related classes.
  2. Supports non-static / non-final fields.

Interface pros :

  1. Establish relation among unrelated classes. e.g. many unrelated objects can implement Serializable OR Externalizable interface.
  2. Define the contract
  3. Multiple inheritance

abstract class establishes "is a" relation with concrete classes. interface provides "has a" capability for classes.

If you are looking for Java as programming language, here are a few more updates:

Java 8 has reduced the gap between interface and abstract classes to some extent by providing a default method feature. An interface does not have an implementation for a method is no longer valid now.

Interface use cases:

  1. Define a contract and it has to be implemented by its implementer.

    Take an example of custom serialization of object. My making your class as implementing Externalizable interface, you have to implement the contract for below methods

     void writeExternal(ObjectOutput out)
     void readExternal(ObjectInput in)
    
  2. Implementation of FactoryMethod ( creational pattern) can be done through interface and its implementation by implementer

    example : Factory Pattern. When to use factory methods?

  3. Implementation of Strategy design pattern ( behavioral pattern) : You have a family of strategies and you want to switch to different strategies at run time

    example : Real World Example of the Strategy Pattern

  4. You can find use of interface in Command design pattern (behavioral )

    example: Using Command Design pattern

abstract class use cases:

  1. You don't have implementation in base class (with shared data) and only sub-classes have to define their own implementation. You need abstract class instead of interface since you want to share state with sub-classes.

  2. Abstract classes are used in Abstract Factory design pattern implementation ( creational pattern )

    example: Factory Pattern. When to use factory methods?

  3. You can find many use cases of abstract classes in design patterns.

    example: Bridge pattern ( Structural pattern )

    Does the Bridge Pattern decouples an abstraction from implementation?

  4. Abstract classes are used in Decorator design pattern

    example : When to Use the Decorator Pattern?

  5. Abstract class is best fit for template method pattern where you have to create skeleton.

    example: Template design pattern in JDK, could not find a method defining set of methods to be executed in order

怪我太投入 2024-08-22 04:57:09

要点是:

  • 抽象是面向对象的。它提供“对象”应具有的基本数据和/或它应能够执行的功能。它关注对象的基本特征:它有什么以及它能做什么。因此,从同一抽象类继承的对象共享基本特征(泛化)。
  • 界面以功能为导向。它定义了对象应具有的功能。不管是什么对象,只要能实现接口中定义的这些功能就可以了。它忽略了其他一切。一个对象/类可以包含多个(组)功能;因此一个类可以实现多个接口。

The main point is that:

  • Abstract is object oriented. It offers the basic data an 'object' should have and/or functions it should be able to do. It is concerned with the object's basic characteristics: what it has and what it can do. Hence objects which inherit from the same abstract class share the basic characteristics (generalization).
  • Interface is functionality oriented. It defines functionalities an object should have. Regardless what object it is, as long as it can do these functionalities, which are defined in the interface, it's fine. It ignores everything else. An object/class can contain several (groups of) functionalities; hence it is possible for a class to implement multiple interfaces.
ζ澈沫 2024-08-22 04:57:09

一些重要的区别:

以表格的形式:

Difference

As Joe 从 javapapers 陈述

1.主要区别是Java接口的方法是隐式抽象的并且不能有实现。 Java 抽象类可以
有实现默认行为的实例方法。

2.Java接口中声明的变量默认是final的。抽象类可以包含非最终变量。

3.Java接口的成员默认是public的。 Java 抽象类可以具有类成员的常见风格,例如 private、
受保护等..

4.Java接口应该使用关键字“implements”来实现; Java 抽象类应该使用关键字“extends”进行扩展。

5.一个接口只能扩展另一个Java接口,一个抽象类可以扩展另一个Java类并实现多个Java
接口。

6.一个Java类可以实现多个接口,但只能扩展一个抽象类。

7.Interface是绝对抽象的,无法实例化; Java 抽象类也不能被实例化,但如果
main() 存在。

8.与java抽象类相比,java接口速度较慢,因为它需要额外的间接寻址。

Some important differences:

In the form of a table:

Difference

As stated by Joe from javapapers:

1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can
have instance methods that implements a default behavior.

2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private,
protected, etc..

4.Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java
interfaces.

6.A Java class can implement multiple interfaces but it can extend only one abstract class.

7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a
main() exists.

8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

紫南 2024-08-22 04:57:09

当您想要在继承层次结构中提供多态行为时,请使用抽象类。

当您希望完全不相关的类具有多态行为时,请使用接口。

When you want to provide polymorphic behaviour in an inheritance hierarchy, use abstract classes.

When you want polymorphic behaviour for classes which are completely unrelated, use an interface.

百善笑为先 2024-08-22 04:57:09

我正在建造一座300层的大楼 大楼

的蓝图界面

  • 例如,Servlet(I)

大楼建造到200层 - 部分完成---抽象

  • 部分实现,用于例如,通用和 HTTP servlet

构建完成 -具体

  • 完整实现,例如,自己的 servlet

接口

  • 我们对实现一无所知,只知道需求。我们可以
    去找一个接口。
  • 每个方法默认都是public和abstract的
  • 它是一个100%纯抽象类
  • 如果我们声明public,我们就不能声明private和protected
  • 如果我们声明abstract,我们就不能声明final、static、synchronized、strictfp和native
  • 每个接口都有public、static和final
  • 序列化和瞬态不适用,因为我们无法在接口中创建实例
  • 非易失性,因为它是最终的
  • 每个变量都是静态的
  • 当我们在接口内声明变量时,我们需要初始化变量,同时
  • 不允许声明实例和静态块

抽象

  • 部分实现
  • 它有一个抽象方法。另外,它使用了具体的
  • 对抽象类方法修饰符没有限制
  • 对抽象类变量修饰符没有限制
  • 我们不能声明除abstract之外的其他修饰符
  • 对初始化变量没有限制

摘自DurgaJobs网站

I am constructing a building of 300 floors

The building's blueprint interface

  • For example, Servlet(I)

Building constructed up to 200 floors - partially completed---abstract

  • Partial implementation, for example, generic and HTTP servlet

Building construction completed-concrete

  • Full implementation, for example, own servlet

Interface

  • We don't know anything about implementation, just requirements. We can
    go for an interface.
  • Every method is public and abstract by default
  • It is a 100% pure abstract class
  • If we declare public we cannot declare private and protected
  • If we declare abstract we cannot declare final, static, synchronized, strictfp and native
  • Every interface has public, static and final
  • Serialization and transient is not applicable, because we can't create an instance for in interface
  • Non-volatile because it is final
  • Every variable is static
  • When we declare a variable inside an interface we need to initialize variables while declaring
  • Instance and static block not allowed

Abstract

  • Partial implementation
  • It has an abstract method. An addition, it uses concrete
  • No restriction for abstract class method modifiers
  • No restriction for abstract class variable modifiers
  • We cannot declare other modifiers except abstract
  • No restriction to initialize variables

Taken from DurgaJobs Website

我的黑色迷你裙 2024-08-22 04:57:09

让我们再讨论一下这个问题:

首先要告诉大家的是,1/1 和 1*1 的结果是相同的,但并不意味着乘法和除法是相同的。显然,他们保持着良好的关系,但请注意,你们俩是不同的。

我将指出主要区别,其余部分已经解释过:

抽象类对于对类层次结构进行建模非常有用。乍一看任何需求,我们都部分清楚要构建什么确切,但我们知道要构建什么。因此您的抽象类是您的基类。

接口对于让其他层次结构或类知道我能够做什么非常有用。当你说我有能力做某事时,你必须有那个能力。接口会将其标记为类必须实现相同的功能。

Let's work on this question again:

The first thing to let you know is that 1/1 and 1*1 results in the same, but it does not mean that multiplication and division are same. Obviously, they hold some good relationship, but mind you both are different.

I will point out main differences, and the rest have already been explained:

Abstract classes are useful for modeling a class hierarchy. At first glance of any requirement, we are partially clear on what exactly is to be built, but we know what to build. And so your abstract classes are your base classes.

Interfaces are useful for letting other hierarchy or classes to know that what I am capable of doing. And when you say I am capable of something, you must have that capacity. Interfaces will mark it as compulsory for a class to implement the same functionalities.

2024-08-22 04:57:09

如果您有一些可由多个类使用的通用方法,请选择抽象类。
否则,如果您希望类遵循某些明确的蓝图,请使用接口。

以下示例演示了这一点。

Java 中的抽象类:

abstract class Animals
{
    // They all love to eat. So let's implement them for everybody
    void eat()
    {
        System.out.println("Eating...");
    }
    // The make different sounds. They will provide their own implementation.
    abstract void sound();
}
 
class Dog extends Animals
{
    void sound()
    {
        System.out.println("Woof Woof");
    }
}
 
class Cat extends Animals
{
    void sound()
    {
        System.out.println("Meoww");
    }
}

以下是 Java 中接口的实现:

interface Shape
{
    void display();
    double area();
}
 
class Rectangle implements Shape 
{
    int length, width;
    Rectangle(int length, int width)
    {
        this.length = length;
        this.width = width;
    }
    @Override
    public void display() 
    {
        System.out.println("****\n* *\n* *\n****"); 
    }
    @Override
    public double area() 
    {
        return (double)(length*width);
    }
} 
 
class Circle implements Shape 
{
    double pi = 3.14;
    int radius;
    Circle(int radius)
    {
        this.radius = radius;
    }
    @Override
    public void display() 
    {
        System.out.println("O"); // :P
    }
    @Override
    public double area() 
    { 
        return (double)((pi*radius*radius)/2);
    }
}

简而言之,一些重要的要点:

  1. Java 接口中声明的变量默认为 Final。抽象类可以有非最终变量。

  2. Java 接口中声明的变量默认是静态的。抽象类可以有非静态变量。

  3. Java 接口的成员默认是公共的。 Java 抽象类可以具有类成员的常见风格,例如 private、protected 等。

If you have some common methods that can be used by multiple classes go for abstract classes.
Else if you want the classes to follow some definite blueprint go for interfaces.

Following examples demonstrate this.

Abstract class in Java:

abstract class Animals
{
    // They all love to eat. So let's implement them for everybody
    void eat()
    {
        System.out.println("Eating...");
    }
    // The make different sounds. They will provide their own implementation.
    abstract void sound();
}
 
class Dog extends Animals
{
    void sound()
    {
        System.out.println("Woof Woof");
    }
}
 
class Cat extends Animals
{
    void sound()
    {
        System.out.println("Meoww");
    }
}

Following is an implementation of interface in Java:

interface Shape
{
    void display();
    double area();
}
 
class Rectangle implements Shape 
{
    int length, width;
    Rectangle(int length, int width)
    {
        this.length = length;
        this.width = width;
    }
    @Override
    public void display() 
    {
        System.out.println("****\n* *\n* *\n****"); 
    }
    @Override
    public double area() 
    {
        return (double)(length*width);
    }
} 
 
class Circle implements Shape 
{
    double pi = 3.14;
    int radius;
    Circle(int radius)
    {
        this.radius = radius;
    }
    @Override
    public void display() 
    {
        System.out.println("O"); // :P
    }
    @Override
    public double area() 
    { 
        return (double)((pi*radius*radius)/2);
    }
}

Some Important Key points in a nutshell:

  1. The variables declared in Java interface are by default final. Abstract classes can have non-final variables.

  2. The variables declared in Java interface are by default static. Abstract classes can have non-static variables.

  3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..

你在看孤独的风景 2024-08-22 04:57:09

实际上很简单。

您可以将接口视为只允许具有抽象方法而没有其他任何内容的类。

因此,接口只能“声明”而不能定义您希望类具有的行为。

抽象类允许您声明(使用抽象方法)以及定义(使用完整方法实现)您希望该类具有的行为。

常规类只允许您定义(而不是声明)您希望该类具有的行为/操作。

最后一件事,

在 Java 中,您可以实现多个接口,但只能扩展一个接口(抽象类或类)...

这意味着已定义行为的继承仅限于每个类只允许一个...即,如果您想要一个封装来自类 A、B 和 C 的行为的类,您需要执行以下操作:类 A 扩展 B,类 C 扩展 A .. 它是一种具有多重继承的有点迂回的方式...

另一方面,接口,你可以简单地这样做:接口C实现A,B

所以实际上Java仅在“声明的行为”即接口中支持多重继承,并且仅支持具有定义行为的单继承..除非你按照我描述的方式进行...

希望这是有道理的。

It's pretty simple actually.

You can think of an interface as a class which is only allowed to have abstract methods and nothing else.

So an interface can only "declare" and not define the behavior you want the class to have.

An abstract class allows you to do both declare (using abstract methods) as well as define (using full method implementations) the behavior you want the class to have.

And a regular class only allows you to define, not declare, the behavior/actions you want the class to have.

One last thing,

In Java, you can implement multiple interfaces, but you can only extend one (Abstract Class or Class)...

This means inheritance of defined behavior is restricted to only allow one per class... ie if you wanted a class that encapsulated behavior from Classes A,B&C you would need to do the following: Class A extends B, Class C extends A .. its a bit of a round about way to have multiple inheritance...

Interfaces on the other hand, you could simply do: interface C implements A, B

So in effect Java supports multiple inheritance only in "declared behavior" ie interfaces, and only single inheritance with defined behavior.. unless you do the round about way I described...

Hopefully that makes sense.

白芷 2024-08-22 04:57:09

接口与抽象类的比较是错误的。应该有另外两个比较:1)接口与类和2)抽象与最终类

接口与类

接口是两个对象之间的契约。例如,我是一名邮递员,而你是一个要投递的包裹。我希望您知道您的送货地址。当有人给我一个 Package 时,它​​必须知道它的交付地址:

interface Package {
  String address();
}

Class 是一组遵守契约的对象。例如,我是“盒子”组的一个盒子,我遵守邮递员要求的合同。同时我遵守其他契约:

class Box implements Package, Property {
  @Override
  String address() {
    return "5th Street, New York, NY";
  }
  @Override
  Human owner() {
    // this method is part of another contract
  }
}

Abstract vs Final

抽象类是一组不完整的对象。它们无法使用,因为它们缺少一些部件。例如,我是一个抽象的 GPS 感知框 - 我知道如何检查我在地图上的位置:

abstract class GpsBox implements Package {
  @Override
  public abstract String address();
  protected Coordinates whereAmI() {
    // connect to GPS and return my current position
  }
}

这个类如果被另一个类继承/扩展,会非常有用。但就其本身而言 - 它是无用的,因为它不能拥有对象。抽象类可以是最终类的构建元素。

最终类是一组完整的对象,可以使用,但不能修改。他们确切地知道如何工作和做什么。例如,我是一个总是转到其构造期间指定的地址的 Box:

final class DirectBox implements Package {
  private final String to;
  public DirectBox(String addr) {
    this.to = addr;
  }
  @Override
  public String address() {
    return this.to;
  }
}

在大多数语言中,如 Java 或 C++,可以只有一个类,既不是抽象类,也不是最终类。这样的类可以被继承,也可以被实例化。不过,我认为这并不严格符合面向对象的范例。

同样,将接口与抽象类进行比较是不正确的。

The comparison of interface vs. abstract class is wrong. There should be two other comparisons instead: 1) interface vs. class and 2) abstract vs. final class.

Interface vs Class

Interface is a contract between two objects. E.g., I'm a Postman and you're a Package to deliver. I expect you to know your delivery address. When someone gives me a Package, it has to know its delivery address:

interface Package {
  String address();
}

Class is a group of objects that obey the contract. E.g., I'm a box from "Box" group and I obey the contract required by the Postman. At the same time I obey other contracts:

class Box implements Package, Property {
  @Override
  String address() {
    return "5th Street, New York, NY";
  }
  @Override
  Human owner() {
    // this method is part of another contract
  }
}

Abstract vs Final

Abstract class is a group of incomplete objects. They can't be used, because they miss some parts. E.g., I'm an abstract GPS-aware box - I know how to check my position on the map:

abstract class GpsBox implements Package {
  @Override
  public abstract String address();
  protected Coordinates whereAmI() {
    // connect to GPS and return my current position
  }
}

This class, if inherited/extended by another class, can be very useful. But by itself - it is useless, since it can't have objects. Abstract classes can be building elements of final classes.

Final class is a group of complete objects, which can be used, but can't be modified. They know exactly how to work and what to do. E.g., I'm a Box that always goes to the address specified during its construction:

final class DirectBox implements Package {
  private final String to;
  public DirectBox(String addr) {
    this.to = addr;
  }
  @Override
  public String address() {
    return this.to;
  }
}

In most languages, like Java or C++, it is possible to have just a class, neither abstract nor final. Such a class can be inherited and can be instantiated. I don't think this is strictly in line with object-oriented paradigm, though.

Again, comparing interfaces with abstract classes is not correct.

辞取 2024-08-22 04:57:09

唯一的区别是一个可以参与多重继承而另一个不能。

接口的定义随着时间的推移而发生变化。您认为接口只有方法声明并且只是契约吗? Java 8 之后的 static Final 变量和默认定义又如何呢?

Java 中引入接口是因为多重继承的钻石问题,这就是他们真正的意图去做。

接口是为了解决多重继承问题而创建的构造,可以具有抽象方法、默认定义和静态最终变量。

请参阅为什么 Java 允许在接口中使用静态最终变量,而它们只是作为契约?

The only difference is that one can participate in multiple inheritance and other cannot.

The definition of an interface has changed over time. Do you think an interface just has method declarations only and are just contracts? What about static final variables and what about default definitions after Java 8?

Interfaces were introduced to Java because of the diamond problem with multiple inheritance and that's what they actually intend to do.

Interfaces are the constructs that were created to get away with the multiple inheritance problem and can have abstract methods, default definitions and static final variables.

See Why does Java allow static final variables in interfaces when they are only intended to be contracts?.

喵星人汪星人 2024-08-22 04:57:09

接口:Turn(左转、右转。)

抽象类:Wheel。

类:Steering Wheel,派生自 Wheel,公开接口 Turn

一个用于对可在各种事物中提供的行为进行分类,另一个用于对事物的本体进行建模。

Interface: Turn ( Turn Left, Turn Right.)

Abstract Class: Wheel.

Class: Steering Wheel, derives from Wheel, exposes Interface Turn

One is for categorizing behavior that can be offered across a diverse range of things, the other is for modelling an ontology of things.

坠似风落 2024-08-22 04:57:09

简而言之,差异如下:

接口抽象类之间的语法差异:

  1. 抽象类的方法和成员可以具有任何可见性。 接口的所有方法都必须是公共//从 Java 9 开始不再适用
  2. 抽象类的具体子类必须定义所有抽象方法。 Abstract 子类可以具有抽象方法。扩展另一个接口的接口不需要为从父接口继承的方法提供默认实现。
  3. 子类只能扩展一个类。一个接口可以扩展多个接口。一个类可以实现多个接口。
  4. 子类可以定义具有相同或更少限制性可见性的抽象方法,而实现接口的类必须将所有接口方法定义为公共方法。
  5. 抽象类可以有构造函数,但不能有接口
  6. Java 9 中的接口具有私有静态方法。

现在在接口中:

public static - 支持
公共摘要 - 支持
公共默认 - 支持
私有静态 - 支持
私有摘要 - 编译错误
私有默认值 - 编译错误
私有 - 支持

In short the differences are the following:

Syntactical Differences Between Interface and Abstract Class:

  1. Methods and members of an abstract class can have any visibility. All methods of an interface must be public. //Does not hold true from Java 9 anymore
  2. A concrete child class of an Abstract Class must define all the abstract methods. An Abstract child class can have abstract methods. An interface extending another interface need not provide default implementation for methods inherited from the parent interface.
  3. A child class can only extend a single class. An interface can extend multiple interfaces. A class can implement multiple interfaces.
  4. A child class can define abstract methods with the same or less restrictive visibility, whereas class implementing an interface must define all interface methods as public.
  5. Abstract Classes can have constructors but not interfaces.
  6. Interfaces from Java 9 have private static methods.

In Interfaces now:

public static - supported
public abstract - supported
public default - supported
private static - supported
private abstract - compile error
private default - compile error
private - supported

笑看君怀她人 2024-08-22 04:57:09

许多初级开发人员错误地认为接口、抽象类和具体类是同一事物的轻微变体,并纯粹出于技术原因选择其中之一:我需要多重继承吗?我需要一些地方来放置常用方法吗?除了具体的类之外,我还需要关心其他东西吗?这是错误的,隐藏在这些问题中的是主要问题:“我”。当您为自己编写代码时,您很少会想到其他当前或未来的开发人员正在处理您的代码。

接口和抽象类虽然从技术角度来看很相似,但具有完全不同的含义和目的。

摘要

  1. 接口定义契约,某些实现将为您履行

  2. 抽象类提供默认行为您的实现可以重用。

替代总结

  1. 接口用于定义公共 API
  2. 抽象类用于内部使用,并用于定义 SPI

隐藏实现细节的重要性

具体类以非常具体的方式执行实际工作。例如,ArrayList 使用连续的内存区域以紧凑的方式存储对象列表,它提供快速随机访问、迭代和就地更改,但在插入、删除、有时甚至会添加;同时,LinkedList 使用双链接节点来存储对象列表,它提供了快速迭代、就地更改和插入/删除/添加,但在随机访问方面很糟糕。这两种类型的列表针对不同的用例进行了优化,如何使用它们非常重要。当您试图从与您频繁交互的列表中挤出性能时,并且当您选择列表类型时,您应该仔细选择要实例化的列表。

另一方面,列表的高级用户并不真正关心它是如何实际实现的,他们应该与这些细节隔离。让我们想象一下,Java 没有公开 List 接口,而只有一个具体的 List 类,这实际上就是现在的 LinkedList 。所有 Java 开发人员都会定制他们的代码以适应实现细节:避免随机访问、添加缓存以加快访问速度,或者只是自己重新实现 ArrayList,尽管它与所有其他内容不兼容实际上仅适用于 List 的代码。那太糟糕了……但现在想象一下,Java 大师实际上意识到链表对于大多数实际用例来说都很糟糕,并决定为他们唯一可用的 List 类切换到数组列表。这会影响世界上每个 Java 程序的性能,人们不会对此感到高兴。罪魁祸首是实施细节是可用的,而开发人员认为这些细节是他们可以依赖的永久合同。这就是为什么隐藏实现细节并仅定义抽象契约很重要。这就是接口的目的:定义方法接受什么样的输入,以及期望什么样的输出,而不暴露所有会诱使程序员调整代码以适应可能因未来更新而改变的内部细节的所有内容。 。

抽象类位于接口和具体类之间。它应该帮助实现共享常见或无聊的代码。例如,AbstractCollection 提供了基于 size 为 0 的 isEmpty 的基本实现,contains 作为迭代和比较,addAll如重复add,等等。这使得实现能够专注于区分它们的关键部分:如何实际存储和检索数据。

API 与 SPI

接口是代码不同部分之间的低内聚网关。它们允许库存在和发展,而不会在内部发生变化时破坏每个库用户。它称为“应用程序编程接口”,而不是“应用程序编程类”。在较小的规模上,它们还允许多个开发人员通过记录良好的接口分离不同的模块,从而在大型项目上成功协作。

抽象类是实现接口时使用的高内聚帮助器,假设有一定程度的实现细节。或者,抽象类用于定义 SPI(服务提供者接口)。

API 和 SPI 之间的区别很微妙,但很重要:对于 API,重点在于谁使用它,而对于 SPI,重点在于谁实现它。

向 API 添加方法很容易,该 API 的所有现有用户仍然可以编译。向 SPI 添加方法很困难,因为每个服务提供者(具体实现)都必须实现新方法。如果使用接口来定义 SPI,则每当 SPI 合同发生变化时,提供商都必须发布新版本。如果使用抽象类,则新方法可以根据现有抽象方法来定义,或者定义为空的抛出未实现的异常存根,这至少允许旧版本的服务实现仍然编译并运行。

关于 Java 8 和默认方法的注释

尽管 Java 8 引入了接口的默认方法,这使得接口和抽象类之间的界限更加模糊,但这并不是为了实现可以重用代码,而是为了更容易更改所服务的接口既作为 API 又作为 SPI(或者错误地用于定义 SPI 而不是抽象类)。

使用哪一个?

  1. 事物是否应该被代码的其他部分或其他外部代码公开使用?向其添加一个接口,以隐藏公共抽象合约的实现细节,这是该事物的一般行为。
  2. 这个事物是否应该有多个实现并且有很多共同的代码?创建一个接口和一个抽象的、不完整的实现。
  3. 是否只有一种实现,而没有其他人会使用它?只要把它变成一个具体的类就可以了。
    1. “曾经”是很长一段时间,您可以谨慎行事,仍然在其上添加一个界面。

推论:相反的做法通常是错误的:使用事物时,始终尝试使用您实际需要的最通用的类​​/接口。换句话说,不要将变量声明为 ArrayList theList = new ArrayList(),除非您实际上非常强烈地依赖它作为 array 列表,并且没有其他类型的列表会为您节省。使用 List theList = new ArrayList 代替,甚至使用 Collection theCollection = new ArrayList(如果它是一个列表,而不是任何其他类型的集合)实际上并不重要。

Many junior developers make the mistake of thinking of interfaces, abstract and concrete classes as slight variations of the same thing, and choose one of them purely on technical grounds: Do I need multiple inheritance? Do I need some place to put common methods? Do I need to bother with something other than just a concrete class? This is wrong, and hidden in these questions is the main problem: "I". When you write code for yourself, by yourself, you rarely think of other present or future developers working on or with your code.

Interfaces and abstract classes, although apparently similar from a technical point of view, have completely different meanings and purposes.

Summary

  1. An interface defines a contract that some implementation will fulfill for you.

  2. An abstract class provides a default behavior that your implementation can reuse.

Alternative summary

  1. An interface is for defining public APIs
  2. An abstract class is for internal use, and for defining SPIs

On the importance of hiding implementation details

A concrete class does the actual work, in a very specific way. For example, an ArrayList uses a contiguous area of memory to store a list of objects in a compact manner which offers fast random access, iteration, and in-place changes, but is terrible at insertions, deletions, and occasionally even additions; meanwhile, a LinkedList uses double-linked nodes to store a list of objects, which instead offers fast iteration, in-place changes, and insertion/deletion/addition, but is terrible at random access. These two types of lists are optimized for different use cases, and it matters a lot how you're going to use them. When you're trying to squeeze performance out of a list that you're heavily interacting with, and when picking the type of list is up to you, you should carefully pick which one you're instantiating.

On the other hand, high level users of a list don't really care how it is actually implemented, and they should be insulated from these details. Let's imagine that Java didn't expose the List interface, but only had a concrete List class that's actually what LinkedList is right now. All Java developers would have tailored their code to fit the implementation details: avoid random access, add a cache to speed up access, or just reimplement ArrayList on their own, although it would be incompatible with all the other code that actually works with List only. That would be terrible... But now imagine that the Java masters actually realize that a linked list is terrible for most actual use cases, and decided to switch over to an array list for their only List class available. This would affect the performance of every Java program in the world, and people wouldn't be happy about it. And the main culprit is that implementation details were available, and the developers assumed that those details are a permanent contract that they can rely on. This is why it's important to hide implementation details, and only define an abstract contract. This is the purpose of an interface: define what kind of input a method accepts, and what kind of output is expected, without exposing all the guts that would tempt programmers to tweak their code to fit the internal details that might change with any future update.

An abstract class is in the middle between interfaces and concrete classes. It is supposed to help implementations share common or boring code. For example, AbstractCollection provides basic implementations for isEmpty based on size is 0, contains as iterate and compare, addAll as repeated add, and so on. This lets implementations focus on the crucial parts that differentiate between them: how to actually store and retrieve data.

APIs versus SPIs

Interfaces are low-cohesion gateways between different parts of code. They allow libraries to exist and evolve without breaking every library user when something changes internally. It's called Application Programming Interface, not Application Programming Classes. On a smaller scale, they also allow multiple developers to collaborate successfully on large scale projects, by separating different modules through well documented interfaces.

Abstract classes are high-cohesion helpers to be used when implementing an interface, assuming some level of implementation details. Alternatively, abstract classes are used for defining SPIs, Service Provider Interfaces.

The difference between an API and an SPI is subtle, but important: for an API, the focus is on who uses it, and for an SPI the focus is on who implements it.

Adding methods to an API is easy, all existing users of the API will still compile. Adding methods to an SPI is hard, since every service provider (concrete implementation) will have to implement the new methods. If interfaces are used to define an SPI, a provider will have to release a new version whenever the SPI contract changes. If abstract classes are used instead, new methods could either be defined in terms of existing abstract methods, or as empty throw not implemented exception stubs, which will at least allow an older version of a service implementation to still compile and run.

A note on Java 8 and default methods

Although Java 8 introduced default methods for interfaces, which makes the line between interfaces and abstract classes even blurrier, this wasn't so that implementations can reuse code, but to make it easier to change interfaces that serve both as an API and as an SPI (or are wrongly used for defining SPIs instead of abstract classes).

Which one to use?

  1. Is the thing supposed to be publicly used by other parts of the code, or by other external code? Add an interface to it to hide the implementation details from the public abstract contract, which is the general behavior of the thing.
  2. Is the thing something that's supposed to have multiple implementations with a lot of code in common? Make both an interface and an abstract, incomplete implementation.
  3. Is there ever going to be only one implementation, and nobody else will use it? Just make it a concrete class.
    1. "ever" is long time, you could play it safe and still add an interface on top of it.

A corollary: the other way around is often wrongly done: when using a thing, always try to use the most generic class/interface that you actually need. In other words, don't declare your variables as ArrayList theList = new ArrayList(), unless you actually have a very strong dependency on it being an array list, and no other type of list would cut it for you. Use List theList = new ArrayList instead, or even Collection theCollection = new ArrayList if the fact that it's a list, and not any other type of collection doesn't actually matter.

破晓 2024-08-22 04:57:09

我迟到了 10 年,但我想尝试任何方式。几天前写了一篇关于同样内容的文章。想到将其发布在这里。

太长;博士;当您看到“Is A”关系时,请使用继承/抽象类。当您看到“有”关系时,创建成员变量。当您看到“依赖于外部提供者”时,请实现(而不是继承)接口。

面试问题接口和抽象类有什么区别?您如何决定何时使用什么?
我大多得到以下一个或全部答案:
答案1:您不能创建抽象类和接口的对象。

ZK(这是我名字的缩写):你不能创建其中任何一个的对象。所以这没有区别。这是接口和抽象类之间的相似之处。柜台
问题:为什么不能创建抽象类或接口的对象?

答案 2: 抽象类可以有一个函数体作为部分/默认实现。

ZK:反问:所以如果我将其更改为纯抽象类,将所有虚函数标记为抽象,并且不为任何虚函数提供默认实现。这会使抽象类和接口相同吗?之后它们可以互换使用吗?

答案 3:接口允许多重继承,而抽象类则不允许。

ZK:反问:你真的从接口继承吗?或者您只是实现一个接口并从抽象类继承?实现和继承有什么区别?
这些反问问题让候选人感到困惑,让大多数人摸不着头脑,或者直接转向下一个问题。这让我认为人们需要有关面向对象编程的这些基本构建块的帮助。
原始问题和所有反问题的答案可以在英语和 UML 中找到。
您必须至少了解以下内容才能更好地理解这两个结构。

普通名词:普通名词是同一类或同类事物“共同”的名称。例如水果、动物、城市、汽车等。

专有名词:专有名词是物体、地点或事物的名称。苹果、卡特彼勒、纽约、本田雅阁等。

汽车是一个普通名词。本田雅阁是一个专有名词,并且可能是一个复合专有名词,一个使用两个名词组成的专有名词。

来到 UML 部分。您应该熟悉以下关系:

  • Is A
  • Has A
  • Uses

让我们考虑以下两个句子。
- 本田雅阁是汽车吗?
- 本田雅阁有车吗?

哪一个听起来正确?简单的英语和理解能力。本田雅阁和汽车公司有着“Is A”的关系。本田雅阁里面没有汽车。它“是一辆”汽车。本田雅阁“有一个”音乐播放器。

当两个实体共享“Is A”关系时,它是继承的更好候选者。 Has a 关系是创建成员变量的更好候选者。
建立后,我们的代码如下所示:

abstract class Car
{
   string color;
   int speed;
}
class HondaAccord : Car
{
   MusicPlayer musicPlayer;
}

现在本田不生产音乐播放器。或者至少这不是他们的主要业务。

于是他们联系其他公司并签订了合同。如果您在这里接收电源并在这两条线上接收输出信号,那么这些扬声器就会正常播放。

这使得音乐播放器成为界面的完美候选者。只要连接工作正常,您并不关心谁为其提供支持。

您可以将 LG 的 MusicPlayer 替换为 Sony 或其他方式。这不会改变本田雅阁的任何事情。

为什么你不能创建一个抽象类的对象?

因为你不能走进陈列室并说给我一辆车。您必须提供一个专有名词。什么车?应该是本田雅阁。那时销售代理可以为您提供一些东西。

为什么不能创建接口的对象?
因为你不能走进陈列室并说给我一份音乐播放器合同。这不会有帮助。消费者和提供商之间的接口只是为了促进达成协议。您将如何处理协议副本?它不会播放音乐。

为什么接口允许多重继承?

接口不能被继承。接口已实现。
界面是与外部世界交互的候选者。
本田雅阁有一个加油接口。它具有用于给轮胎充气的接口。以及用于给足球充气的同一软管。因此,新代码将如下所示:

abstract class Car
{
    string color;
    int speed;
}
class HondaAccord : Car, IInflateAir, IRefueling
{
    MusicPlayer musicPlayer;
}

英文将如下所示:“本田雅阁是一款支持轮胎充气和加油的汽车”。

I am 10 yrs late to the party but would like to attempt any way. Wrote a post about the same on medium few days back. Thought of posting it here.

tl;dr; When you see “Is A” relationship use inheritance/abstract class. when you see “has a” relationship create member variables. When you see “relies on external provider” implement (not inherit) an interface.

Interview Question: What is the difference between an interface and an abstract class? And how do you decide when to use what?
I mostly get one or all of the below answers:
Answer 1: You cannot create an object of abstract class and interfaces.

ZK (That’s my initials): You cannot create an object of either. So this is not a difference. This is a similarity between an interface and an abstract class. Counter
Question: Why can’t you create an object of abstract class or interface?

Answer 2: Abstract classes can have a function body as partial/default implementation.

ZK: Counter Question: So if I change it to a pure abstract class, marking all the virtual functions as abstract and provide no default implementation for any virtual function. Would that make abstract classes and interfaces the same? And could they be used interchangeably after that?

Answer 3: Interfaces allow multi-inheritance and abstract classes don’t.

ZK: Counter Question: Do you really inherit from an interface? or do you just implement an interface and, inherit from an abstract class? What’s the difference between implementing and inheriting?
These counter questions throw candidates off and make most scratch their heads or just pass to the next question. That makes me think people need help with these basic building blocks of Object-Oriented Programming.
The answer to the original question and all the counter questions is found in the English language and the UML.
You must know at least below to understand these two constructs better.

Common Noun: A common noun is a name given “in common” to things of the same class or kind. For e.g. fruits, animals, city, car etc.

Proper Noun: A proper noun is the name of an object, place or thing. Apple, Cat, New York, Honda Accord etc.

Car is a Common Noun. And Honda Accord is a Proper Noun, and probably a Composit Proper noun, a proper noun made using two nouns.

Coming to the UML Part. You should be familiar with below relationships:

  • Is A
  • Has A
  • Uses

Let’s consider the below two sentences.
- HondaAccord Is A Car?
- HondaAccord Has A Car?

Which one sounds correct? Plain English and comprehension. HondaAccord and Cars share an “Is A” relationship. Honda accord doesn’t have a car in it. It “is a” car. Honda Accord “has a” music player in it.

When two entities share the “Is A” relationship it’s a better candidate for inheritance. And Has a relationship is a better candidate for creating member variables.
With this established our code looks like this:

abstract class Car
{
   string color;
   int speed;
}
class HondaAccord : Car
{
   MusicPlayer musicPlayer;
}

Now Honda doesn't manufacture music players. Or at least it’s not their main business.

So they reach out to other companies and sign a contract. If you receive power here and the output signal on these two wires it’ll play just fine on these speakers.

This makes Music Player a perfect candidate for an interface. You don’t care who provides support for it as long as the connections work just fine.

You can replace the MusicPlayer of LG with Sony or the other way. And it won’t change a thing in Honda Accord.

Why can’t you create an object of abstract classes?

Because you can’t walk into a showroom and say give me a car. You’ll have to provide a proper noun. What car? Probably a honda accord. And that’s when a sales agent could get you something.

Why can’t you create an object of an interface?
Because you can’t walk into a showroom and say give me a contract of music player. It won’t help. Interfaces sit between consumers and providers just to facilitate an agreement. What will you do with a copy of the agreement? It won’t play music.

Why do interfaces allow multiple inheritance?

Interfaces are not inherited. Interfaces are implemented.
The interface is a candidate for interaction with the external world.
Honda Accord has an interface for refueling. It has interfaces for inflating tires. And the same hose that is used to inflate a football. So the new code will look like below:

abstract class Car
{
    string color;
    int speed;
}
class HondaAccord : Car, IInflateAir, IRefueling
{
    MusicPlayer musicPlayer;
}

And the English will read like this “Honda Accord is a Car that supports inflating tire and refueling”.

再见回来 2024-08-22 04:57:09

这并不是原始问题的答案,但一旦你找到了它们之间差异的答案,你就会陷入何时使用的困境:
何时使用接口或抽象类?何时使用两者?

我对 OOP 的了解有限,但到目前为止,将接口视为语法中的形容词的等价物对我来说一直有效(如果此方法是假的,请纠正我!)。例如,接口名称就像您可以赋予类的属性或功能,并且一个类可以拥有其中的许多属性或功能:ISerialized、ICountable、IList、ICacheable、IHappy,...

Not really the answer to the original question, but once you have the answer to the difference between them, you will enter the when-to-use-each dilemma:
When to use interfaces or abstract classes? When to use both?

I've limited knowledge of OOP, but seeing interfaces as an equivalent of an adjective in grammar has worked for me until now (correct me if this method is bogus!). For example, interface names are like attributes or capabilities you can give to a class, and a class can have many of them: ISerializable, ICountable, IList, ICacheable, IHappy, ...

黄昏下泛黄的笔记 2024-08-22 04:57:09

您可以发现接口抽象类之间有明显的区别。

接口

  • 接口仅包含抽象方法。
  • 强制用户在实现接口时实现所有方法。
  • 仅包含最终变量和静态变量。
  • 使用接口关键字声明。
  • 接口的所有方法都必须定义为公共方法。
  • 接口可以扩展或者类可以实现多个其他接口
    接口。

抽象类

  • 抽象类包含抽象和非抽象方法。

  • 继承时不强制用户实现所有方法
    抽象类。

  • 包含各种变量,包括原始变量和非原始变量

  • 使用abstract关键字声明。

  • 抽象类的方法和成员可以用任何定义
    可见性。

  • 子类只能扩展单个类(抽象或具体)。

You can find clear difference between interface and abstract class.

Interface

  • Interface only contains abstract methods.
  • Force users to implement all methods when implements the interface.
  • Contains only final and static variables.
  • Declare using interface keyword.
  • All methods of an interface must be defined as public.
  • An interface can extend or a class can implement multiple other
    interfaces.

Abstract class

  • Abstract class contains abstract and non-abstract methods.

  • Does not force users to implement all methods when inherited the
    abstract class.

  • Contains all kinds of variables including primitive and non-primitive

  • Declare using abstract keyword.

  • Methods and members of an abstract class can be defined with any
    visibility.

  • A child class can only extend a single class (abstract or concrete).

反差帅 2024-08-22 04:57:09

要点:

  • 抽象类可以有属性、数据字段、方法(完整/
    不完整)两者。
  • 如果方法或属性在抽象关键字中定义,则必须在派生类中重写。(其作为紧密耦合的工作
    功能)
  • 如果为抽象类中的方法或属性定义抽象关键字,则不能定义方法体并获取/设置值
    属性并且必须在派生类中重写。
  • 抽象类不支持多重继承。
  • 抽象类包含构造函数。
  • 抽象类可以包含子类、函数、属性的访问修饰符。
  • 只有抽象类的完整成员才能是静态的。
  • 接口只能从另一个接口继承,不能从抽象类继承,而抽象类可以从另一个抽象类或另一个接口继承。

优点:

  • 它是一种强制所有子类执行相同层次结构或标准的契约。
  • 如果各种实现属于同一类型并且使用共同的行为或状态,那么最好使用抽象类。
  • 如果我们向抽象类添加新方法,那么我们可以选择提供默认实现,因此所有现有代码都可以正常工作。
  • 它比接口允许更快的执行。(接口需要更多时间在相应的类中查找实际方法。)
  • 它可以用于紧耦合和松耦合。

在这里找到详细信息...
http://pradeepatkari.wordpress。 com/2014/11/20/interface-and-abstract-class-in-c-oops/

Key Points:

  • Abstract class can have property, Data fields ,Methods (complete /
    incomplete) both.
  • If method or Properties define in abstract keyword that must override in derived class.(its work as a tightly coupled
    functionality)
  • If define abstract keyword for method or properties in abstract class you can not define body of method and get/set value for
    properties and that must override in derived class.
  • Abstract class does not support multiple inheritance.
  • Abstract class contains Constructors.
  • An abstract class can contain access modifiers for the subs, functions, properties.
  • Only Complete Member of abstract class can be Static.
  • An interface can inherit from another interface only and cannot inherit from an abstract class, where as an abstract class can inherit from another abstract class or another interface.

Advantage:

  • It is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
  • If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
  • If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
  • Its allow fast execution than interface.(interface Requires more time to find the actual method in the corresponding classes.)
  • It can use for tight and loosely coupling.

find details here...
http://pradeepatkari.wordpress.com/2014/11/20/interface-and-abstract-class-in-c-oops/

望喜 2024-08-22 04:57:09

最简单的总结是,接口是:

  1. 完全抽象,除了默认静态方法;虽然它具有 defaultstatic 方法的定义(方法签名 + 实现),但它仅具有其他方法的声明(方法签名)。
  2. 遵守比类更宽松的规则(一个类可以实现多个接口,一个接口可以从多个接口继承)。所有变量都是隐式常量,无论是否指定为 public static final 。所有成员都隐式地public,无论是否如此指定。
  3. 通常用作保证实现类将具有指定的功能和/或与实现相同接口的任何其他类兼容。

同时,抽象类是:

  1. 从完全抽象到完全实现的任何地方,并且倾向于具有一个或多个抽象方法。可以包含声明和定义,声明标记为abstract
  2. 一个成熟的类,并遵守管理其他类的规则(只能从一个类继承),条件是它不能被实例化(因为不能保证它已完全实现)。可以有非常量成员变量。可以实现成员访问控制,将成员限制为 protectedprivate 或私有包(未指定)。
  3. 通常用于提供可由多个子类共享的尽可能多的实现,或者提供程序员能够提供的尽可能多的实现。

或者,如果我们想将其全部归结为一句话:接口是实现类拥有的,但是抽象类是子类是什么

The shortest way to sum it up is that an interface is:

  1. Fully abstract, apart from default and static methods; while it has definitions (method signatures + implementations) for default and static methods, it only has declarations (method signatures) for other methods.
  2. Subject to laxer rules than classes (a class can implement multiple interfaces, and an interface can inherit from multiple interfaces). All variables are implicitly constant, whether specified as public static final or not. All members are implicitly public, whether specified as such or not.
  3. Generally used as a guarantee that the implementing class will have the specified features and/or be compatible with any other class which implements the same interface.

Meanwhile, an abstract class is:

  1. Anywhere from fully abstract to fully implemented, with a tendency to have one or more abstract methods. Can contain both declarations and definitions, with declarations marked as abstract.
  2. A full-fledged class, and subject to the rules that govern other classes (can only inherit from one class), on the condition that it cannot be instantiated (because there's no guarantee that it's fully implemented). Can have non-constant member variables. Can implement member access control, restricting members as protected, private, or private package (unspecified).
  3. Generally used either to provide as much of the implementation as can be shared by multiple subclasses, or to provide as much of the implementation as the programmer is able to supply.

Or, if we want to boil it all down to a single sentence: An interface is what the implementing class has, but an abstract class is what the subclass is.

鹿港巷口少年归 2024-08-22 04:57:09

继承有两个目的:

  • 允许对象将父类型数据成员和方法实现视为自己的。

  • 允许期望引用超类型对象的代码使用对一种类型的对象的引用。

    允许期望引用超类型对象的代码

在支持广义多重继承的语言/框架中,通常不需要将类型分类为“接口”或“抽象类”。然而,流行的语言和框架将允许一种类型将另一种类型的数据成员或方法实现视为自己的数据成员或方法实现,即使它们允许一种类型可替换任意数量的其他类型。

抽象类可以具有数据成员和方法实现,但只能由不从任何其他类继承的类继承。接口对实现它们的类型几乎没有限制,但不能包含任何数据成员或方法实现。

有时,类型可以替代许多不同的东西是很有用的。有时,对象将父类型数据成员和方法实现视为自己的数据成员和方法实现也是有用的。区分接口和抽象类允许在最相关的情况下使用这些功能。

Inheritance is used for two purposes:

  • To allow an object to regard parent-type data members and method implementations as its own.

  • To allow a reference to an objects of one type to be used by code which expects a reference to supertype object.

In languages/frameworks which support generalized multiple inheritance, there is often little need to classify a type as either being an "interface" or an "abstract class". Popular languages and frameworks, however, will allow a type to regard one other type's data members or method implementations as its own even though they allow a type to be substitutable for an arbitrary number of other types.

Abstract classes may have data members and method implementations, but can only be inherited by classes which don't inherit from any other classes. Interfaces put almost no restrictions on the types which implement them, but cannot include any data members or method implementations.

There are times when it's useful for types to be substitutable for many different things; there are other times when it's useful for objects to regard parent-type data members and method implementations as their own. Making a distinction between interfaces and abstract classes allows each of those abilities to be used in cases where it is most relevant.

十二 2024-08-22 04:57:09

代表实际实现的抽象类和接口之间的区别。

接口:它是一个关键字,用于定义对象的模板或蓝图,它强制所有对象子类将遵循相同的原型,至于实现,所有子类都可以根据其要求自由实现功能。

我们应该使用接口的其他一些用例。

两个外部对象之间的通信(我们应用程序中的第三方集成)通过接口完成,这里接口充当合同。

抽象类 Abstract,它是一个关键字,当我们在任何类之前使用该关键字时,它就成为抽象类。它主要用于当我们需要定义模板以及一些默认功能时使用对象后跟所有子类,这样就删除了冗余代码和我们可以使用抽象类的更多用例,例如我们不希望其他类可以直接实例化该对象的对象类,只有派生类可以使用该功能。

抽象类示例:

 public abstract class DesireCar
  {

 //It is an abstract method that defines the prototype.
     public abstract void Color();

  // It is a default implementation of a Wheel method as all the desire cars have the same no. of wheels.   
 // and hence no need to define this in all the sub classes in this way it saves the code duplicasy     

  public void Wheel() {          

               Console.WriteLine("Car has four wheel");
                }
           }


    **Here is the sub classes:**

     public class DesireCar1 : DesireCar
        {
            public override void Color()
            {
                Console.WriteLine("This is a red color Desire car");
            }
        }

        public class DesireCar2 : DesireCar
        {
            public override void Color()
            {
                Console.WriteLine("This is a red white Desire car");
            }
        }

接口示例:

  public interface IShape
        {
          // Defines the prototype(template) 
            void Draw();
        }


  // All the sub classes follow the same template but implementation can be different.

    public class Circle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("This is a Circle");
        }
    }

    public class Rectangle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("This is a Rectangle");
        }
    }

Differences between abstract class and interface on behalf of real implementation.

Interface: It is a keyword and it is used to define the template or blue print of an object and it forces all the sub classes would follow the same prototype,as for as implementation, all the sub classes are free to implement the functionality as per it's requirement.

Some of other use cases where we should use interface.

Communication between two external objects(Third party integration in our application) done through Interface here Interface works as Contract.

Abstract Class: Abstract,it is a keyword and when we use this keyword before any class then it becomes abstract class.It is mainly used when we need to define the template as well as some default functionality of an object that is followed by all the sub classes and this way it removes the redundant code and one more use cases where we can use abstract class, such as we want no other classes can directly instantiate an object of the class, only derived classes can use the functionality.

Example of Abstract Class:

 public abstract class DesireCar
  {

 //It is an abstract method that defines the prototype.
     public abstract void Color();

  // It is a default implementation of a Wheel method as all the desire cars have the same no. of wheels.   
 // and hence no need to define this in all the sub classes in this way it saves the code duplicasy     

  public void Wheel() {          

               Console.WriteLine("Car has four wheel");
                }
           }


    **Here is the sub classes:**

     public class DesireCar1 : DesireCar
        {
            public override void Color()
            {
                Console.WriteLine("This is a red color Desire car");
            }
        }

        public class DesireCar2 : DesireCar
        {
            public override void Color()
            {
                Console.WriteLine("This is a red white Desire car");
            }
        }

Example Of Interface:

  public interface IShape
        {
          // Defines the prototype(template) 
            void Draw();
        }


  // All the sub classes follow the same template but implementation can be different.

    public class Circle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("This is a Circle");
        }
    }

    public class Rectangle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("This is a Rectangle");
        }
    }
泛滥成性 2024-08-22 04:57:09

抽象类与接口的主题主要是关于语义的。

抽象类在不同的编程语言中通常充当接口的超集,但有一点除外,那就是您可以实现多个接口,但只能继承一个类。

接口定义了某些东西必须能够做什么;就像合同一样,但不提供它的实现。

抽象类定义什么是,并且它通常在子类之间托管共享代码。

例如,Formatter 应该能够format() 某些内容。描述类似内容的常见语义是创建一个带有 format() 声明的接口 IFormatter ,其行为类似于契约。但 IFormatter 并没有描述某物是什么,而只是描述它应该能够做什么。描述某事物实际是什么的常见语义是创建一个类。在本例中,我们创建一个抽象类...因此我们创建一个实现该接口的抽象类Formatter。这是一段描述性很强的代码,因为我们现在知道我们有一个 Formatter 并且我们现在知道每个 Formatter 必须能够做什么。

另外一个非常重要的主题是文档(至少对于某些人来说......)。在您的文档中,您可能想在子类中解释 Formatter 实际上是什么。有一个抽象类 Formatter 非常方便,您可以在子类中链接到该抽象类的文档。这是非常方便和通用的。另一方面,如果您没有抽象类 Formatter 而只有一个接口 IFormatter,您就必须在每个子类中解释什么是 Formatter code> 实际上是这样,因为接口是一个契约,并且您不会在接口的文档中描述 Formatter 实际上是什么 - 至少这不是常见的事情,并且您会破坏大多数开发人员认为正确的语义。

注意:让抽象类实现接口是一种非常常见的模式。

The topic of abstract classes vs interfaces is mostly about semantics.

Abstract classes act in different programming languages often as a superset of interfaces, except one thing and that is, that you can implement multiple interfaces, but inherit only one class.

An interface defines what something must be able to do; like a contract, but does not provide an implementation of it.

An abstract class defines what something is and it commonly hosts shared code between the subclasses.

For example a Formatter should be able to format() something. The common semantics to describe something like that would be to create an interface IFormatter with a declaration of format() that acts like a contract. But IFormatter does not describe what something is, but just what it should be able to to. The common semantics to describe what something actually is, is to create a class. In this case we create an abstract class... So we create an abstract class Formatter which implements the interface. That is a very descriptive code, because we now know we have a Formatter and we now know what every Formatter must be able to do.

Also one very important topic is documentation (at least for some people...). In your documentation you probably want to explain within your subclasses what a Formatter actually is. It is very convenient to have an abstract class Formatter to which documentation you can link to within your subclasses. That is very convenient and generic. On the other hand if you do not have an abstract class Formatter and only an interface IFormatter you would have to explain in each of your subclasses what a Formatter actucally is, because an interface is a contract and you would not describe what a Formatter actually is within the documentation of an interface — at least it would be not something common to do and you would break the semantics that most developers consider to be correct.

Note: It is a very common pattern to make an abstract class implement an interface.

一场春暖 2024-08-22 04:57:09

我想再添加一个有意义的差异。
例如,您有一个包含数千行代码的框架。现在,如果您想使用增强UI() 方法在整个代码中添加新功能,那么最好将该方法添加到抽象类中而不是接口中。因为,如果您在接口中添加此方法,那么您应该在所有已实现的类中实现它,但如果您在抽象类中添加该方法,则情况并非如此。

I'd like to add one more difference which makes sense.
For example, you have a framework with thousands of lines of code. Now if you want to add a new feature throughout the code using a method enhanceUI(), then it's better to add that method in abstract class rather in interface. Because, if you add this method in an interface then you should implement it in all the implemented class but it's not the case if you add the method in abstract class.

夏尔 2024-08-22 04:57:09

为了给出一个简单但清晰的答案,它有助于设置上下文:当您不想提供完整的实现时,可以使用两者。

主要区别是接口根本没有实现(只有没有主体的方法),而抽象类也可以有带有主体的成员和方法,即可以部分实现。

To give a simple but clear answer, it helps to set the context : you use both when you do not want to provide full implementations.

The main difference then is an interface has no implementation at all (only methods without a body) while abstract classes can have members and methods with a body as well, i.e. can be partially implemented.

海风掠过北极光 2024-08-22 04:57:09

相似之处
两者都强制扩展或实现它们的类覆盖基本方法。

差异

  1. 一个类可以实现多个接口。
  2. 一个类只能从一个抽象类扩展。
  3. 接口中声明的字段必须是staticfinal,因为从此类实现创建的所有对象都共享相同的值。
  4. 在抽象类中,字段可以命名但不能赋值。子类可以覆盖它们。

用例

  1. 抽象类用于密切相关或具有几乎相同功能和行为的子类中。
  2. 接口用于您想要强制执行某种事物或行为的不相关的类,因为它只是一个没有实现的契约。

Similarities
Both forces classes extending or implementing them to override base methods.

Differences

  1. A class can implement multiple interfaces.
  2. A class can only extend from one abstract class.
  3. Fields declared in interfaces must be static and final because all objects that created from such implementation share same values.
  4. In Abstract classes, fields can be named and not assigned. subclasses can override them.

Usecases

  1. Abstract classes are used in subclasses that are closely related, or have almost same functionalities and behaviours.
  2. Interfaces are used for unrelated classes that you want to force a certain thing or behaviour, because its Just a contract without implementation.
哆兒滾 2024-08-22 04:57:09

抽象类是无法创建对象或无法实例化的类。
抽象方法使类变得抽象。
需要继承抽象类才能覆盖抽象类中声明的方法。
对访问说明符没有限制。
抽象类可以包含构造函数和其他具体(非抽象方法)方法,但接口不能包含。

接口是方法的蓝图/模板。(例如,给出纸上的房子(接口房子),不同的架构师将使用他们的想法来构建它(实现房屋接口的架构师的类)。
它是抽象方法、默认方法、静态方法、最终变量和嵌套类的集合。
所有成员都将是 Final 或 public ,不允许 protected 和 private 访问说明符。不允许创建对象。
必须创建一个类才能使用实现接口并覆盖接口中声明的抽象方法。接口是松耦合的一个很好的例子(动态多态/动态绑定)
接口实现了多态性和抽象性。它告诉要做什么,但如何做是由实现类定义的。
对于例如。有一家汽车公司,它希望其正在制造的所有汽车具有相同的某些功能,因此该公司将制造具有这些功能的接口车辆,并且不同类别的汽车(例如 Maruti Suzkhi、Maruti 800)将覆盖这些特性(功能)。

当我们已经有了抽象类时为什么还要接口呢?
Java仅支持多级和层次继承,但借助接口我们可以实现多重继承。

An abstract class is a class whose object cannot be created or a class which cannot be instantiated.
An abstract method makes a class abstract.
An abstract class needs to be inherited in order to override the methods that are declared in the abstract class.
No restriction on access specifiers.
An abstract class can have constructor and other concrete(non abstarct methods ) methods in them but interface cannot have.

An interface is a blueprint/template of methods.(eg. A house on a paper is given(interface house) and different architects will use their ideas to build it(the classes of architects implementing the house interface) .
It is a collection of abstract methods , default methods , static methods , final variables and nested classes.
All members will be either final or public , protected and private access specifiers are not allowed.No object creation is allowed.
A class has to be made in order to use the implementing interface and also to override the abstract method declared in the interface. An interface is a good example of loose coupling(dynamic polymorphism/dynamic binding)
An interface implements polymorphism and abstraction.It tells what to do but how to do is defined by the implementing class.
For Eg. There's a car company and it wants that some features to be same for all the car it is manufacturing so for that the company would be making an interface vehicle which will have those features and different classes of car(like Maruti Suzkhi , Maruti 800) will override those features(functions).

Why interface when we already have abstract class?
Java supports only multilevel and hierarchal inheritance but with the help of interface we can implement multiple inheritance.

悲欢浪云 2024-08-22 04:57:09

在接口中,所有方法都必须只是定义,而不应实现单个方法。

但是在抽象类中必须有一个仅具有定义的抽象方法,但其他方法也可以在抽象类中具有实现...

In an interface all methods must be only definitions, not single one should be implemented.

But in an abstract class there must an abstract method with only definition, but other methods can be also in the abstract class with implementation...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文