不具体的,似是而非的
没有具体实现的
比如Animal,只是对动物的大概描述
能吃
能睡
具体吃啥,怎么睡我们无从得知,创建出来的对象意义不大
我们认为这种类不应该直接创建对象,应该让其子类创建具体的对象
怎么限制?做成抽象类
abstract修饰的类可以变成抽象类
被abstract修饰的类成为抽象类
无法直接创建对象
抽象类中可以存在抽象方法
package com.qf.abs;
?
public class Demo01 {
    public static void main(String[] args) {
        // 抽象类无法直接创建对象
        // Animal animal = new Animal();
    }
}
?
abstract class Animal{
    
    public abstract void eat();
    
}
?
抽象类无法直接创建对象,但是可以有子类
抽象类的子类继承抽象类之后可以获取到抽象类中的非私有普通属性和方法
抽象类的子类如果想使用抽象类中的抽象方法,必须重写这些方法之后才能使用
如果不使用抽象方法,做成抽象类也是可以的
package com.qf.abs;
?
public class Demo01 {
    public static void main(String[] args) {
        // 抽象类无法直接创建对象
        // Animal animal = new Animal();
        Husky husky = new Husky();
        System.out.println(husky.type);
        husky.breath();
        husky.eat();
    
    }
}
?
/**
 * 抽象类Animal
 *  描述动物的一个类
 *  只做了大概的描述,没有具体的实现
 *  需要子类继承此类后做具体的描述
 * @author Dushine2008
 *
 */
abstract class Animal{
    
    // 属性
    String type = "动物";
    String section;
    
    public abstract void eat();
    
    public abstract void sleep();
    
    public void breath() {
        System.out.println("所有的动物都依赖氧气存活...");
    }
    
}
?
?
abstract class Dog extends Animal{
    
}
?
class Husky extends Dog{
?
    
创建抽象类Car
创建Car的子类
Auto
Bus
Tractor
在子类重写继承的抽象方法和自己独有的方法
使用多态的思想创建对象并调用这些方法
package com.qf.abs;
?
public class Demo02 {
    public static void main(String[] args) {
        Car car01 = new Auto();
        car01.start();
        car01.stop();
        
        // 向下转型
        Auto auto = (Auto) car01;
        auto.manned();
        
        System.out.println("========================");
        
        Car car02 = new Bus();
        car02.start();
        car02.stop();
        
        // 拆箱
        Bus bus = (Bus) car02;
        bus.manned();
    
        System.out.println("========================");
        
        Car car03 = new Tractor();
        car03.start();
        car03.stop();
        
        Tractor tractor = (Tractor) car03;
        tractor.doFarmWork();
        
    }
}
?
/**
 * 车的顶层类
 *  抽象类
 *  方法没有具体的实现
 * @author Dushine2008
 *
 */
abstract class Car{
    // 属性
    int weight;
    int height;
    String brand;
    int price;
    
    // 方法
    public abstract void start();
    
    public abstract void stop();
}
?
/**
 * 奥拓车
 *  继承Car
 *  重写启动和停止的方法
 * @author Dushine2008
 *
 */
class Auto extends Car{
?