-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleInheritanceDemo.java
More file actions
137 lines (112 loc) · 4.63 KB
/
Copy pathVehicleInheritanceDemo.java
File metadata and controls
137 lines (112 loc) · 4.63 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.Scanner;
// 1. Base Class
class Vehicle {
protected String regNumber;
protected int speed;
protected String color;
protected String ownerName;
// Constructor to initialize base fields
public Vehicle(String regNumber, int speed, String color, String ownerName) {
this.regNumber = regNumber;
this.speed = speed;
this.color = color;
this.ownerName = ownerName;
}
public void showData() {
System.out.println("\n--- This is Vehicle class ---");
}
}
// 2. Derived Class: Bus
class Bus extends Vehicle {
private int routeNumber;
public Bus(String regNumber, int speed, String color, String ownerName, int routeNumber) {
// Calling the parent class constructor using 'super'
super(regNumber, speed, color, ownerName);
this.routeNumber = routeNumber;
}
// Overriding the base method
@Override
public void showData() {
super.showData(); // Invoke parent method
System.out.println("Bus Details:");
System.out.println("Registration Number: " + this.regNumber);
System.out.println("Speed: " + this.speed + " km/h");
System.out.println("Color: " + this.color);
System.out.println("Owner Name: " + this.ownerName);
System.out.println("Route Number: " + this.routeNumber);
}
}
// 3. Derived Class: Car
class Car extends Vehicle {
private String manufacturerName;
public Car(String regNumber, int speed, String color, String ownerName, String manufacturerName) {
// Calling the parent class constructor using 'super'
super(regNumber, speed, color, ownerName);
this.manufacturerName = manufacturerName;
}
// Overriding the base method
@Override
public void showData() {
super.showData(); // Invoke parent method
System.out.println("Car Details:");
System.out.println("Registration Number: " + this.regNumber);
System.out.println("Speed: " + this.speed + " km/h");
System.out.println("Color: " + this.color);
System.out.println("Owner Name: " + this.ownerName);
System.out.println("Manufacturer: " + this.manufacturerName);
}
}
// 4. Main Driver Class (PascalCase)
public class VehicleInheritanceDemo {
public static void main(String[] args) {
// Using a single Scanner for better resource management
Scanner scanner = new Scanner(System.in);
// --- Bus Input ---
System.out.println("--- Enter Bus Details ---");
System.out.print("Enter Registration Number: ");
String busReg = scanner.nextLine();
System.out.print("Enter Speed: ");
int busSpeed = getValidInt(scanner);
scanner.nextLine(); // Consume leftover newline
System.out.print("Enter Color: ");
String busColor = scanner.nextLine();
System.out.print("Enter Owner Name: ");
String busOwner = scanner.nextLine();
System.out.print("Enter Route Number: ");
int busRoute = getValidInt(scanner);
scanner.nextLine(); // Consume leftover newline
// Instantiate Bus object
Bus bus = new Bus(busReg, busSpeed, busColor, busOwner, busRoute);
// --- Car Input ---
System.out.println("\n--- Enter Car Details ---");
System.out.print("Enter Registration Number: ");
String carReg = scanner.nextLine();
System.out.print("Enter Speed: ");
int carSpeed = getValidInt(scanner);
scanner.nextLine(); // Consume leftover newline
System.out.print("Enter Color: ");
String carColor = scanner.nextLine();
System.out.print("Enter Owner Name: ");
String carOwner = scanner.nextLine();
System.out.print("Enter Manufacturer Name: ");
String carManuf = scanner.nextLine();
// Instantiate Car object
Car car = new Car(carReg, carSpeed, carColor, carOwner, carManuf);
// --- Display Output ---
System.out.println("\n=============================");
System.out.println(" VEHICLE RECORDS ");
System.out.println("=============================");
bus.showData();
System.out.println("-----------------------------");
car.showData();
scanner.close();
}
// Helper Method to prevent Scanner crashes during numeric input
private static int getValidInt(Scanner scanner) {
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Please enter a valid number: ");
scanner.next(); // Consume invalid input
}
return scanner.nextInt();
}
}