Abstract Data Type and Interface

前端 未结 8 1325
小鲜肉
小鲜肉 2021-02-04 18:26

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

For Example We have a ListADT

interface MyListADT {
    void          


        
相关标签:
8条回答
  • 2021-02-04 19:09

    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
    
    0 讨论(0)
  • 2021-02-04 19:10

    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

    0 讨论(0)
  • 2021-02-04 19:10

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

    0 讨论(0)
  • 2021-02-04 19:11

    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.
    0 讨论(0)
  • 2021-02-04 19:11

    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?

    0 讨论(0)
  • 2021-02-04 19:17

    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.

    0 讨论(0)
提交回复
热议问题