Java 抽象数据类型和接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19239471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:21:22  来源:igfitidea点击:

Abstract Data Type and Interface

java

提问by user2454830

I am new to Java. What is the difference between Abstract data type and Interface.

我是 Java 新手。抽象数据类型和接口有什么区别。

For Example We have a ListADT

例如我们有一个 ListADT

interface MyListADT<T> {
    void add(T var);
    void add(T var,int pos);
    void display();
    T remove(int pos);
    void clear();
    boolean contains(Object o);
}

Where we are defining the ADT as an interface. NoW What is the difference between ADT and Interface Or ADT is an Interface

我们将 ADT 定义为接口的地方。现在ADT和接口有什么区别或者ADT是一个接口

采纳答案by SpringLearner

What is the difference between Abstract data type and Interface.
  1. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  2. 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.. check this linkfor info
  1. 在 Java 接口中声明的变量默认为 final。抽象类可能包含非最终变量。
  2. Java 接口的成员默认是公共的。Java 抽象类可以具有类成员的常用风格,如私有、受保护等。请查看此链接以获取信息

回答by TheGraduateGuy

In java-

在java-

interface can have only abstract method which means you can only declare the method i.e . method can have any default implementation.but abstract class can have both abstract or complete method.

接口只能有抽象方法,这意味着您只能声明方法,即 . 方法可以有任何默认实现。但抽象类可以有抽象或完整方法。

if the class you are extending is abstract then your child class should either be declared as abstract or should implement all abstract method of super class. In case -in interface you can implement as many interface you want.Here also you should implement all the abstract method of all the interfaces in your class or it should be declared as abstract.

如果您要扩展的类是抽象类,那么您的子类应该声明为抽象类,或者应该实现超类的所有抽象方法。如果 -in interface 您可以实现任意数量的接口。在这里,您还应该实现类中所有接口的所有抽象方法,或者应该将其声明为抽象方法。

follow these link

按照这些链接

http://javapapers.com/core-java/abstract-and-interface-core-java-2/difference-between-a-java-interface-and-a-java-abstract-class/

http://javapapers.com/core-java/abstract-and-interface-core-java-2/difference-between-a-java-interface-and-a-java-abstract-class/

http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

What is the difference between an interface and abstract class?

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

回答by Saj

The combination of data together with its methods is called an Abstract Data Type(ADT).

数据与其方法的组合称为抽象数据类型(ADT)。

A Java Interface is a way to specify ( but not implement) an ADT.

Java 接口是一种指定(但不实现)ADT 的方法。

It specifies the names, parameters, and return types(ie, header) of the ADT methods.

它指定了 ADT 方法的名称、参数和返回类型(即头)。

The interface does not specify the data fields (except public constants), as that is an implementation detail.

该接口不指定数据字段(公共常量除外),因为这是一个实现细节。

A Java Interface specifies the requirements of an ADT as a contract between the service provider ( class that implements the ADT) and the client (the user of the class).

Java 接口将 ADT 的要求指定为服务提供者(实现 ADT 的类)和客户端(类的用户)之间的契约。

回答by Sankar

The combination of data with its methods is called an Abstract Data Type (ADT).

数据与其方法的组合称为抽象数据类型 (ADT)。

A Java Interface is a way to specify an Abstract Data Type (ADT).

Java 接口是一种指定抽象数据类型 (ADT) 的方法。

You can declare a class as abstract when it contains zero or more abstract methods or When an interface is implemented to a class where not all methods are not implemented.

当一个类包含零个或多个抽象方法时,或者当一个接口实现到一个没有实现所有方法的类时,您可以将一个类声明为抽象类。

回答by Sergey

Try to think about it like this:

试着这样想:

  • Java interface is a type, which boils down to a set of method signatures. Any type, willing to be referenced as interface must provide implementation for these signatures. In reality, there is no behaviour contract. Your implementation can do nothing and still be 'implementing' an interface.

  • Java abstract class is a type, with partially specified behaviourwhose internal implementation for some reason must be specified in his inheritor. This class does have behaviour, which can be redefined/specified in his inheritors.

  • ADT is a set of expected behaviours. You assume, that after calling adt.remove(element)you call adt.get(element)and receive null.

  • Java 接口是一种类型,归结为一组方法签名。任何愿意作为接口引用的类型都必须为这些签名提供实现。实际上,没有行为契约。你的实现什么都不做,仍然在“实现”一个接口。

  • Java 抽象类是一种类型,具有部分指定的行为,其内部实现由于某种原因必须在其继承者中指定。这个类确实有行为,可以在他的继承者中重新定义/指定。

  • ADT 是一组预期行为。您假设,在调用之后adt.remove(element)您调用adt.get(element)并接收null.

The answer to your question is: just an interface is not enough to be an ADT.

你的问题的答案是:仅仅一个接口不足以成为一个 ADT。

  • Everything, that correctly implementsyour interface MyListADT<T>is an ADT. Its external behaviour must conform the ADT concept. This means, that to be considered as ADT, your type must carry implementation, which results either in abstract class or a normal class. For example: java.util.List<T>is an interface for an ADT, but java.util.ArrayList<T>and java.util.LinkedList<T>are actually ADTs, because their actual behaviour does conform the ADT concept.
  • 正确实现您的接口的一切都是MyListADT<T>ADT。它的外部行为必须符合 ADT 概念。这意味着,要被视为 ADT,您的类型必须带有实现,这会导致抽象类或普通类。例如:java.util.List<T>是的ADT的接口,但java.util.ArrayList<T>java.util.LinkedList<T>实际上是ADT的,因为他们的实际行为不符合的ADT概念。

回答by Syam S

There seems to a confusion in this Q&A. The question was about "Abstract Data Type and Interface" and most of the answers concetrating about "Abstract Classes".

这个问答似乎有些混乱。问题是关于“抽象数据类型和接口”,大多数答案都集中在“抽象类”上。

The terms 'abstract data type' and abstract class refer to two entirely different concepts, although both of them use the word 'abstract'. An abstract data type is a self-contained, user-defined type that bundles data with a set of related operations. It behaves in the same way as a built-in type does. However, it does not inherit from other classes, nor does it serve as the base for other derived classes. If you search about it in wiki you would see "An abstract data type is defined as a mathematical model of the data objects that make up a data type as well as the functions that operate on these objects. There are no standard conventions for defining them. A broad division may be drawn between "imperative" and "functional" definition styles." For example, in Java we have List interface. It defines a data structure with set of method to operate on but wont provide any implementaion as such.

术语“抽象数据类型”和抽象类指的是两个完全不同的概念,尽管它们都使用“抽象”这个词。抽象数据类型是一种自包含的、用户定义的类型,它将数据与一组相关操作捆绑在一起。它的行为方式与内置类型相同。但是,它不从其他类继承,也不作为其他派生类的基础。如果您在 wiki 中搜索它,您会看到“抽象数据类型被定义为构成数据类型的数据对象的数学模型以及对这些对象进行操作的函数。没有定义它们的标准约定. “命令式”和“功能式”定义风格之间可能会有很大的区别。” 例如,在 Java 中我们有 List 接口。

In contrast, an abstract class is anything but an abstract data type. An abstract class is a class that is declared abstract — 'it may or may not include abstract methods'. Abstract classes cannot be instantiated, but they can be subclassed. It is not a data type. An abstract class is merely a skeletal interface, which specifies a set of services that its subclasses implement. Unfortunately, the distinction between the two concepts is often confused. Many people erroneously use the term abstract data type when they actually refer to an abstract class.

相比之下,抽象类绝不是抽象数据类型。抽象类是声明为抽象的类——“它可能包含也可能不包含抽象方法”。抽象类不能被实例化,但它们可以被子类化。它不是一种数据类型。抽象类只是一个骨架接口,它指定了其子类实现的一组服务。不幸的是,这两个概念之间的区别经常被混淆。许多人在实际指代抽象类时错误地使用了术语抽象数据类型。

In my opinion Interfaces are Java's way of implementing "Abstract Data type"

在我看来,接口是 Java 实现“抽象数据类型”的方式

You can read about "Abstract Data Type" in Wiki. In additiona to that if you want to know more about abstract data type in java you could refer this link, http://www.e-reading.ws/bookreader.php/138175/Abstract_Data_Types_in_Java.pdf, its really good.

您可以在 Wiki 中阅读“抽象数据类型”。除此之外,如果您想了解有关 Java 中抽象数据类型的更多信息,您可以参考此链接http://www.e-reading.ws/bookreader.php/138175/Abstract_Data_Types_in_Java.pdf,它真的很好。

Most of you might be familiar with abstract classes, Still you could read about it from http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

大多数人可能对抽象类很熟悉,但您仍然可以从http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html阅读它

To add up to this confusions, Java 8 introduced something called "Default Methods", by which we could actually give implementations for methods in interface. To eliminate that confusion you can refer this stackoverflow question Interface with default methods vs Abstract class in Java 8

为了增加这种混淆,Java 8 引入了一种叫做“默认方法”的东西,通过它我们可以实际为接口中的方法提供实现。为了消除这种混淆,你可以参考这个 stackoverflow 问题Interface with default methods vs Abstract class in Java 8

回答by Paresh3489227

For more clearance. Syntax and examples

为了更多的清关。语法和示例

syntax of abstract class

抽象类的语法

public abstract class MyAbstractClass  
{  
    //code  
    public abstract void method();      
} 

example of abstract class

抽象类的例子

public abstract class Animal  
{  
    abstract void walk();      
}  

public class Dog extends Animal  
{  
    void walk()  
    {  
         //Implementation is done here  
    }  
}  


syntax of interface

接口语法

public interface NameOfInterface
{
   //Any number of final, static fields
   //Any number of abstract method declarations\
}

example of interface

接口示例

interface Animal {

   public void eat();
   public void travel();
}

implementing interface

实现接口

public class MammalInt implements Animal{

   public void eat(){
      System.out.println("Mammal eats");
   }

   public void travel(){
      System.out.println("Mammal travels");
   } 

   public int noOfLegs(){
      return 0;
   }

   public static void main(String args[]){
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}

extending interface

扩展接口

//Filename: Sports.java
public interface Sports
{
   public void setHomeTeam(String name);
   public void setVisitingTeam(String name);
}

//Filename: Football.java
public interface Football extends Sports
{
   public void homeTeamScored(int points);
   public void visitingTeamScored(int points);
   public void endOfQuarter(int quarter);
}

//Filename: Hockey.java

public interface Hockey extends Sports
{
   public void homeGoalScored();
   public void visitingGoalScored();
   public void endOfPeriod(int period);
   public void overtimePeriod(int ot);
}

extending multiple interfaces

扩展多个接口

public interface Hockey extends Sports, Event

extends and implements Both

扩展和实现两者

interface A can extends interface B 
class A can extends class B         
class A implements interface A   
class A extends class B implements interface A

回答by Count

As per [wiki] http://en.wikipedia.org/wiki/Abstract_data_type

根据 [wiki] http://en.wikipedia.org/wiki/Abstract_data_type

In computer science, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

在计算机科学中,抽象数据类型 (ADT) 是某一类具有相似行为的数据结构的数学模型;或者对于具有相似语义的一种或多种编程语言的某些数据类型。抽象数据类型是间接定义的,仅由可能对其执行的操作以及对这些操作的影响(以及可能的成本)的数学约束进行定义。

For Java programming language

对于 Java 编程语言

you can take Java's List interface as an example. The interface doesn't explicitly define any behavior at all because there is no concrete List class. The interface only defines a set of methods that other classes (e.g. ArrayList and LinkedList) must implement in order to be considered a List.

可以以Java的List接口为例。该接口根本没有明确定义任何行为,因为没有具体的 List 类。该接口仅定义了其他类(例如 ArrayList 和 LinkedList)必须实现的一组方法才能被视为 List。

but the bottom line is that it is a concept

但归根结底它是一个概念