Abstract Data Type and Interface

前端 未结 8 1324
小鲜肉
小鲜肉 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
    

提交回复
热议问题