Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Data Type and Interface

Tags:

java

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

For Example We have a 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

like image 987
user2454830 Avatar asked Oct 08 '13 05:10

user2454830


4 Answers

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.

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"

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.

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

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

like image 117
Syam S Avatar answered Oct 04 '22 18:10

Syam S


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 behaviour whose 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.

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

  • Everything, that correctly implements your 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.
like image 36
Sergey Avatar answered Oct 04 '22 18:10

Sergey


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

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

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

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

like image 37
Saj Avatar answered Oct 04 '22 17:10

Saj


As per [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.

For Java programming language

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.

but the bottom line is that it is a concept

like image 40
Count Avatar answered Oct 04 '22 19:10

Count