Abstract class
Knowledge Point one: abstract class can not be instantiated
abstract class Color {public // create an abstract class
public void Show () { // definition of a method
}
}
Red class the extends Color {public // Red create a class that inherits Color class
public void show () { // Red rewritten in the show () method
System.out.println ( "I am the red");
}
}
class Demo {public // Create a Demo Test Method class
public static void main (String [] args) {
Color Red new new c1 = (); // instantiate the object
c1.show (); // show method c1 is cited
// Color c2 = new Color () ; // this is the error occurs, because the abstract class is not instantiated
}
}
Knowledge Point two: the abstract methods
The method must be rewritten abstract subclass of abstract class
abstract class Color {public // create an abstract class
abstract public void Show (); // create an abstract method. (Note that no particular braces)
}
Red class the extends Color {public // create Red class, class inheritance Color
@Override
public void Show () { // subclass must override abstract methods abstract parent class, or an error // TODO Auto-Generated Stub Method, System.out.println ( "I am the red"); } }
Blue Color class the extends {public // create Blued class, class inheritance Color
@Override
public void Show () { // subclass must override abstract methods abstract parent class, or an error
// TODO Auto-Generated Stub Method,
System.out.println ( "I am blue");
}
}
public class Demo { //创建Demo类,作为测试类
public static void main(String[] args) {
Color c1 = new Red(); //实例化对象
Color c3=new Blue();
c1.show(); //引用c1的show方法
c3.show();
}
}
练一练
代码如下:
public abstract class Animal { //父类
public Animal() {
System.out.println("创建一个动物");
}
public abstract void eat(); //定义吃的抽象方法
abstract public void reproduce(); //定义繁殖的抽象方法
}
public abstract class Bird extends Animal{ //鸟类
String feather ; //创建属性羽毛
public Bird(String feather) { //创建Bird()带参构造方法
System.out.println("创建了一个鸟类");
this.feather=feather;
}
public void growfeather() { //长羽毛这是一个具体的方法,不是抽象的方法
System.out.println("长满"+feather+"羽毛");
}
abstract public void move(); //鸟类动的方式很多,所以将鸟类的移动定义成一个抽象方法
public void reproduce() { //定义抽象类鸟类的繁殖方式为下蛋
System.out.println("下蛋");
}
}
public class Seagull extends Bird{ //创建海鸥类,继承Bird类
public Seagull(String feather) { //创建Seagull的带参构造方法
super(feather); //用super显示父类带参构造方法
System.out.println("我是一只海鸥");
}
@Override
public void move() { //知识点二
System.out.println("海鸥飞翔");
}
@Override
public void eat() { //知识点二
System.out.println("海鸥吃鱼");
}
}
public class Chicken extends Bird{ //创建鸡类,并且继承Bird类
public Chicken(String feather) { //创建Chicken带参构造方法
super(feather);
System.out.println("我是一只小鸡");
}
@Override
public void move() { //知识点二
System.out.println("小鸡会跑");
}
@Override
public void eat() { //知识点二
System.out.println("小鸡吃米");
}
}
public class Demo { //创建Demo类,作为测试类
public static void main(String[] args) {
Seagull jack=new Seagull("白色"); //实例化对象
jack.eat();
jack.move();
jack.growfeather();
jack.reproduce();
System.out.println();
Chicken rose =new Chicken("黄色");
rose.eat();
rose.move();
rose.growfeather();
rose.reproduce();
}
}
运行结果:
好了,今天的抽象类学习就到这里了。