您的位置:首页 > 编程语言 > Java开发

java抽象类(课堂)

2016-01-14 00:02 375 查看


package com.qianfeng.day10.demo3;

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

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

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

/*

*抽象类的知识点:

*属性:

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

*

*类

* 1、使用abstract进行修饰

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

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

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

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

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

*

*方法

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

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

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

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

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

* 4、private
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() {

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: