-
Notifications
You must be signed in to change notification settings - Fork 3
/
absclass.java
72 lines (57 loc) · 1.7 KB
/
absclass.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
abstract class Vehicle {
String vehicleType;
String model;
int year;
public Vehicle(String model, int year) {
this.vehicleType = "none";
this.model = model;
this.year = year;
}
public abstract void start();
}
class Car extends Vehicle {
public String fuelType;
public Car(String model, int year) {
super(model, year);
this.fuelType = "none";
}
public Car(String model, int year, String fuelType) {
super(model, year);
this.fuelType = fuelType;
}
@Override
public void start() {
System.out.println("Starting the car engine.");
}
}
class Bicycle extends Vehicle {
int gearCount;
public Bicycle(String model, int year) {
super(model, year);
this.gearCount = 1;
}
public Bicycle(String model, int year, int gearCount) {
super(model, year);
this.gearCount = gearCount;
}
@Override
public void start() {
System.out.println("Pedaling the bicycle.");
}
}
public class absclass {
public static void main(String[] args) {
Car car1 = new Car("creta", 2022);
Car car2 = new Car("SUV500", 2021, "Gasoline");
Bicycle bicycle1 = new Bicycle("hondacity", 2023);
Bicycle bicycle2 = new Bicycle("harley davidson", 2019, 30);
Vehicle[] vehicles = {car1, car2, bicycle1, bicycle2};
for (Vehicle vehicle : vehicles) {
System.out.println("Vehicle Type: " + vehicle.vehicleType);
System.out.println("Model: " + vehicle.model);
System.out.println("Year: " + vehicle.year);
vehicle.start();
System.out.println();
}
}
}