抽象类

1. 用Abstract关键字来修饰一个类时,该类称为抽象类;用Abstract关键字来修饰一个方法时,该方法称为抽象方法;

2. 含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写;

3. 抽象类不能被实例化;

4. 抽象方法只需要声明,不需要实现;

5.Demo

Demo_1

class Animal{
	private String name;

	public Animal(String name) {
		super();
		this.name = name;
	}
	
	public abstract void enjoy();
}
// 错误

 Demo_2

abstract class Animal{
	private String name;

	public Animal(String name) {
		super();
		this.name = name;
	}
	
	public abstract void enjoy();
}
// 正确

 Demo_3

abstract class Animal{
  private String name;

  public Animal(String name) {
	super();
	this.name = name;
  }
  public abstract void enjoy();
}
class Cat extends Animal{
  private String furColor;
  public Cat(String name, String furColor) {
	super(name);
	this.furColor = furColor;
  }
  @Override
  public void enjoy() {
	// TODO Auto-generated method stub	
  }
}
class Dog extends Animal{
  private String eyesColor;
  public Dog(String name, String eyesColor) { super(name); this.eyesColor = eyesColor;   }   @Override   public void enjoy() {  // TODO Auto-generated method stub   } } public class Test2 {   public static void main(String[] args) { // TODO Auto-generated method stub Dog d = new Dog("bigYelwow","Yellow"); // 正确 Animal a = new Animal("cat"); // 错误   } }
原文地址:https://www.cnblogs.com/bosongokay/p/6743865.html