Interface Vs. Abstract Class

此文參考:php

http://www.cnblogs.com/liu-5525/p/5614463.htmlhtml

http://www.studytonight.com/java/abstract-class.phpjava

http://www.studytonight.com/java/java-interface.php編程

 

1. 接口: 被用來創建 類 與 類 之間關聯的標準設計模式

Interface is a pure abstract class.  Methods are declared without body.ide

```````````設計

interface Move{code

  int AVERAGE_SPEED = 40;orm

  void move();htm

}

````````````````

 

 

compler will comprehend above codes as below

`````````````````````````````````````````

public static final int AVERAGE_SPEED = 40;

public abstract void move();

````````````````````````````````````````````

 

 

 

compiler automatically converts methods of interface as public and abstract, and 

data members as public, static and final by default

 

Rules for using interface:

1. methods inside interface must not be static, final, native or strictfp.

2. interface can extend one or more other interface.

3. interface can be nested inside another interface

 

 

 

 

2. 抽象類:只要類中有一個抽象方法,此類就被標記爲抽象類,實際上抽象類除了被繼承之外沒有任何意義。

If a class contain any abstract method then the class is decalred as abstract class. An abstract class is never instantiated. It used to provide abstraction

`````abstract class class_name{}

 

Abstract methods: decalred without body

`````abstract return_type function_name();

 

 ````````````````````````````````````

abstract class A{

  abstract void callme();

  public void normal(){System.out.print();}

}

`````````````````````````````````

1. An abstract class must contain at least one abstract method

2. Abstact class can have constractors, member variables and normal mehods

3. abstract class are never instantiated

4. when you extend abstract class with abstract method, you must define the sbatract method in the child class, or make the child class abstract

 

 

區別:

通常的應用裏,最頂級的是 接口(interface),而後是抽象類實現接口, 最後纔到具體類實現。不是很建議具體類直接實現接口,通常的設計模式是面向接口編程,而非面向現實編程

 

 

**** 接口的另一個應用是實現 多重繼承, 咱們知道一個類能夠implement multiple interface, but it can only extend one abstract class. we can make use of interface&inner class to implement mutiple extends

 

abstract class Example1{

abstract String getName();

}

 

 

abstract class Example2{

abstract int getAge();

}

 

public class MainExample{

// inner class

class Test1 extends Examples1{

   String getName(){return "username"; }

}

class Test2 extends Examples2{

  int getAge(){return 1;}

}

 

public String getName(){

 return new Test1().getName();

}

public int getAge(){

return new Test2().getAge();

}

}

 

 

 

another implementation

 

public class Example1{

public String getName() {return "userName";}

 

 

public class example2{

public int getAge(){return 2;}

}

 

 

public class MainExample{

public class Test1 extends Example1{

public String getName(){ return super.getName();}

}

 

pubic class Test2 extends Example2{

public int getAge(){return super.getAge();}

}

 

public String showName(){

return new Test1().getName();

}

public int showAge(){

return new Test2().getAge();

}

 

public static void main(String[] args){

 MainExample example = new MainExample();

System.out.println(example.showName());

System.out.println(example.showAge());

}

 

}

相關文章
相關標籤/搜索