zoukankan      html  css  js  c++  java
  • Interface Vs. Abstract Class

    此文参考:

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

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

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

    1. 接口: 被用来建立 类 与 类 之间关联的标准

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

    ```````````

    interface Move{

      int AVERAGE_SPEED = 40;

      void move();

    }

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

    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());

    }

    }

  • 相关阅读:
    Map基础知识01-ConcurrentHashMap
    数据结构基础03-红黑树
    vue element 表格增加删除修改数据
    好用弹出层
    如何给网页添加水平线
    如何搭建一个vue项目的详细步骤
    vue完成项目后,如何打包成静态文件
    jQuery 每隔5秒钟发送一个ajax请求 请求后台数据接口
    windows远程桌面连接提示身份验证错误,要求的函数不受支持的解决方法
    uniapp安卓app打包后获取位置信息失败解决方法(含高德地图key详细申请流程)
  • 原文地址:https://www.cnblogs.com/morningdew/p/5617934.html
Copyright © 2011-2022 走看看