不具體的,似是而非的java
沒有具體實現的api
好比Animal,只是對動物的大概描述多線程
能吃ide
能睡this
具體吃啥,怎麼睡咱們無從得知,建立出來的對象意義不大spa
咱們認爲這種類不該該直接建立對象,應該讓其子類建立具體的對象線程
怎麼限制?作成抽象類rest
abstract修飾的類能夠變成抽象類對象
被abstract修飾的類成爲抽象類blog
沒法直接建立對象
抽象類中能夠存在抽象方法
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{