7

What is the difference between an abstract data type and an 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.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
user2454830
  • 193
  • 2
  • 2
  • 11
  • 5
    possible duplicate of [What is the difference between an interface and abstract class?](http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class) – David says Reinstate Monica Oct 08 '13 at 05:08
  • 2
    you should have researched it before asking this question .This question/answer is available on so many places then you ca easily find it. – TheGraduateGuy Oct 08 '13 at 05:16
  • 3
    Is there any difference between Abstract Class and Abstract Data Type – user2454830 Oct 08 '13 at 05:33
  • 1
    DownVoter can leave a comment here. Thaknks – user2454830 Oct 08 '13 at 06:15
  • possible duplicate of [Abstract class vs Interface in Java](http://stackoverflow.com/questions/10040069/abstract-class-vs-interface-in-java) – nawfal Jul 07 '14 at 10:09
  • 7
    For anyone looking at this in the future - this question is -not- a duplicate of any of those proposed questions. It is quite specifically about Abstract Data Types, which several answers have mentioned (and the majority of the answers completely missed), and not Abstract Classes. – ty1824 Nov 04 '14 at 09:36
  • This is one of the messiest question and answer page I have found here on SO lol – Zimano Jan 09 '18 at 15:56

8 Answers8

27

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

Community
  • 1
  • 1
Syam S
  • 8,421
  • 1
  • 26
  • 36
8

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.
Sergey
  • 437
  • 5
  • 12
5

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

Saj
  • 849
  • 7
  • 12
  • 1
    There are some odd extra characters that I'm seeing in your text after every word. Can you fix that? – Flexo Oct 08 '13 at 08:34
4

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

Count
  • 1,395
  • 2
  • 19
  • 40
0

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

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://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

What is the difference between an interface and abstract class?

Community
  • 1
  • 1
TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35
  • I am not getting clearly. All are saying about the differences between the Interface and Abstract Class. But the questions is about Abstract Data Type and Interfaces. Please check it clearly. Or You are all saying about Interface is an abstract data type or Abstract Class is an Abstract Data Type. Which one is correct. I am not playing games here. – user2454830 Oct 08 '13 at 05:53
0

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

A Java Interface is a way to specify an Abstract Data Type (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.

Don Dilanga
  • 2,722
  • 1
  • 18
  • 20
Sankar
  • 162
  • 2
  • 13
  • Wasn't me, but maybe because http://stackoverflow.com/a/19239583/19405 (with an earlier timestamp) says the same thing but with more detail. – nobody May 14 '14 at 17:38
0

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
Paresh3489227
  • 845
  • 11
  • 22
-7
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 link for info
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • I am not getting your answers clearly... So, Your discussion is Abstract Data Type is an interface or you have any other answer. – user2454830 Oct 08 '13 at 06:00
  • 2
    The
 combination
 of 
data 
together
 with 
its 
methods
 is
 called 
an 
Abstract
 Data
 Type
 (ADT). A
 Java
 Interface
 is 
a
 way 
to
 specify
 an 
ADT. – user2454830 Oct 08 '13 at 06:00
  • 1
    @user2454830 when you write String x="hi" at this we say x is a variable of String datatype. DataType means to what variable you are assiging.Hope you know class so not explaining about what class is – SpringLearner Oct 08 '13 at 06:03
  • Now I understood.. that... MyListADT listADT = new MyListADTImple(); Where MyListADT is an interfacfe which is inturn an Abstract Data Type. Am I Right...? – user2454830 Oct 08 '13 at 06:05
  • Please see the above comment. – user2454830 Oct 08 '13 at 06:06
  • Can you see the answer posted by Sankar and Saj at Applexus Technolo. I think they are correct... in one way. If they are correct You can accept that answers by giving upvoting... – user2454830 Oct 08 '13 at 06:09
  • Refresh your page with CTRL+F5 – user2454830 Oct 08 '13 at 06:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38761/discussion-between-user2454830-and-javabeginner) – user2454830 Oct 08 '13 at 06:11
  • But I have clicked on UpVote on your answer... I have just modified your answer slightly. Please accept it.. so that it will give tick for your answer. Not sure – user2454830 Oct 08 '13 at 06:27
  • 3
    The question was not about the differences between an abstract **class** and an abstract data type –  Jul 14 '16 at 14:14