java抽象类(课堂)

来源:互联网 发布:初级程序员证书查询 编辑:程序博客网 时间:2024/06/04 19:04


package com.qianfeng.day10.demo3;

 

// abstract ['æbstrækt] 修饰的方法只有方法签名,没有具体的实现,子类必须得重写

// abstract修饰的类,叫抽象类,可以有抽象方法,也可以没有,反过来不成立

//   存在在抽象方法的类必须是抽象类

/*

 *抽象类的知识点:

 *属性:

 * 1、没有抽象属性,有其他普通属性

 *

 *

 * 1、使用abstract进行修饰

 * 2、无法通过 new构造对象,只能使用继承,但是,存在构造方法。

 * 3、可以有抽象方法,也可以没有,反过来不成立,存在在抽象方法的类必须是抽象类

 * 4、如果抽象类中有多个抽象方法,而子类继承后,不重写父类所有的抽象方法,那么

 *        要么子类是报错的,要么让子类声明为抽象类

 * 5 final abstract  2 者对立,相矛盾,只能存其一

 *

 *方法

 * 1、抽象方法使用abstract进行修饰,只有方法签名,没有方法体,只存在抽象类中

 * 2static abstract  不能同时存在,static修饰的方法不能重写,abstract必须要重写

 *    2者对立,相矛盾,只能存其一

 * 3abstract final  不能同时存在, final修饰的方法为最终方法,不能被重写

 *   abstract必须要重写, 2者对立,相矛盾,只能存其一

 * 4private abstract 不能同时存在, private修饰的方法为私有方法,不能被继承,

 *   abstract必须要重写, 2者对立,相矛盾,只能存其一

 */

public abstract class Father {

      //Illegalmodifier for the field name;

      //onlypublic, protected, private, static, final, transient

      //&volatile are permitted

      //abstractString name;

      

      publicFather(){

            

      }

      

      publicvoid eat(){

            System.out.println("Father.eat()");

      }

      

      publicabstract void work();

      

      publicabstract void method();

      

      //Theabstract method method2 in type Father can only set a visibility modifier,

      //oneof public or protected

      //publicabstract static void method2();

      

}

 

 

 

 

 

package com.qianfeng.day10.demo3;

 

//The type Son must implement the inheritedabstract method Father.work()

public class Son extends Father {

      publicstatic void main(String[] args) {

            

            Son son = new Son();

            son.eat();

            

            //Cannot instantiate the type Father

            //Father father = new Father();

            Father father = new Son();

            

      }

 

      @Override

      publicvoid work() {//必须重写父类方法

            

      }

 

      @Override

      publicvoid method() {

            

      }

}

 

0 0
原创粉丝点击