package chapter20230823;
public class Car {
String color;
int door;
void drive() {
System.out.println("drive, Brrrrr~");
}
void stop() {
System.out.println("stop!!");
}
}
class FireEngine extends Car {
void water() {
System.out.println("water!!!");
}
}
package chapter20230823;
public class test01 {
public static void main(String[] args) {
FireEngine fireEngine = new FireEngine();
fireEngine.drive();
fireEngine.water();
Car car = fireEngine;
car.drive();
FireEngine fireEngine2 = (FireEngine)car;
fireEngine2.drive();
fireEngine2.water();
}
}
package chapter20230823;
public class test02 {
public static void main(String[] args) {
FireEngine fireEngine = new FireEngine();
if(fireEngine instanceof FireEngine) {
System.out.println("This is a FireEngine instance.");
}
if(fireEngine instanceof Car) {
System.out.println("This is a Car instance.");
}
if(fireEngine instanceof Object) {
System.out.println("This is a Object instance.");
}
System.out.println();
Car polyFireEngine = new FireEngine();
if(polyFireEngine instanceof FireEngine) {
System.out.println("This is a FireEngine instance.");
}
if(polyFireEngine instanceof Car) {
System.out.println("This is a Car instance.");
}
if(polyFireEngine instanceof Object) {
System.out.println("This is a Object instance.");
}
System.out.println();
Car car = new Car();
if(car instanceof FireEngine) {
System.out.println("This is a FireEngine instance.");
}
if(car instanceof Car) {
System.out.println("This is a Car instance.");
}
if(car instanceof Object) {
System.out.println("This is a Object instance.");
}
System.out.println();
}
}