-
Notifications
You must be signed in to change notification settings - Fork 1
/
Car03.java
105 lines (85 loc) · 2.8 KB
/
Car03.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Abstract class representing a vehicle
abstract class Vehicle {
private String brand;
// Constructor
public Vehicle(String brand) {
this.brand = brand;
}
// Abstract method to start the vehicle
public abstract void start();
// Concrete method to display the brand of the vehicle
public void displayBrand() {
System.out.println("Brand: " + brand);
}
}
// Car class extending the Vehicle abstract class
class Car extends Vehicle {
// Constructor
public Car(String brand) {
super(brand);
}
// Implementing the start method for the Car class
@Override
public void start() {
System.out.println("Starting the car engine...");
}
// Car-specific method
public void specialCarMethod() {
System.out.println("This is a special method for cars.");
}
}
// Bike class extending the Vehicle abstract class
class Bike extends Vehicle {
// Constructor
public Bike(String brand) {
super(brand);
}
// Implementing the start method for the Bike class
@Override
public void start() {
System.out.println("Starting the bike engine...");
}
// Bike-specific method
public void specialBikeMethod() {
System.out.println("This is a special method for bikes.");
}
// Main class
public static void main(String[] args) {
// Create objects of Car and Bike classes
Car car = new Car("Toyota");
Bike bike = new Bike("Honda");
// Start the vehicles
car.start();
bike.start();
// Display the brands of the vehicles
car.displayBrand();
bike.displayBrand();
// Create an array of Vehicle objects
Vehicle[] vehicles = new Vehicle[2];
vehicles[0] = new Car("Ford");
vehicles[1] = new Bike("Yamaha");
// Polymorphic method calls
for (Vehicle vehicle : vehicles) {
vehicle.start();
vehicle.displayBrand();
// Check if the vehicle is a Car or Bike
if (vehicle instanceof Car) {
Car carVehicle = (Car) vehicle;
carVehicle.specialCarMethod();
} else if (vehicle instanceof Bike) {
Bike bikeVehicle = (Bike) vehicle;
bikeVehicle.specialBikeMethod();
}
System.out.println();
}
// Create an anonymous class extending Vehicle
Vehicle anonymousVehicle = new Vehicle("Anonymous") {
@Override
public void start() {
System.out.println("Starting the anonymous vehicle...");
}
};
anonymousVehicle.start();
anonymousVehicle.displayBrand();
}
}