Java abstract class / interface

First, the abstract class:

  1. Keywords: abstract; abstract class, abstract method modification;

  2. Note:

    2.1 abstract class can not create objects, but can be declared references ( to force the use of multi-state ) ;

    2.2 abstract class does not necessarily contain an abstract method, comprising abstract methods must be abstract;

  Abstract methods:

    1. Format: abstract modification, {} and no method body; because replication must be subclassed, the method and the body does not make sense;

    2. Note: You must subclass replication;

    3. Note: The abstract method can not use private modification, since abstract methods must be implemented;

//父类
public abstract class father{
  public  abstract void study(); //无需加{};
}
//子类
class son extends father{
    void study(){
        System.out.println("study");
    }
}
public class test{
    public static void main (String args[]){
        new son().study();
    }
}

Second, the interface:

 

  1. The interface definition: interface ;   class definition: class ;

  2. Format Format: public static public constants and abstract methods can only be defined;

     (1 ) Constant: public static Final data type  constant name = value;

     (2 ) Method: public abstract return type  method name ([ parameter ..])

      // modifier can be omitted, the system will default;

     (3 three methods) interface:

  1 ) abstract method;

  2 ) static method, the method comprising the body; jdk1.8 ;

  3 ) The default method is generally implemented method of air; jdk1.8 ;

3. Interface Features:

( 1 ) interface extends inherit multiple interfaces , a single inheritance class;

 ( 2 endo) interface defines the interface again;

4. Interface Polymorphism : polymorphism create similar inherited; Interface Name  Instance Name = new instance () ;

 5. implementation calls:

// Interface

interface Inter{

    public static final int NUM=1;

    public abstract void run();

}

// subclass

class SubInter implements Inter{

    public void run(){

        System.out.println(123);

    }

}

public class test{

    public static void main(String[] args){

        SubInter t = new SubInter();

        System.out.println (t.NUM); // instance calls

        System.out.println (Inter.NUM); // interface name calling

        System.out.println (SubInter.NUM); // the class name calling

    }}

6. enumeration:

public enum enum name {

Instance name 1 , name instance 2 , instance name 3 ....

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Third, the difference between abstract classes and interfaces:

  

 

Guess you like

Origin www.cnblogs.com/Tractors/p/11225000.html