-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical26.java
More file actions
56 lines (45 loc) · 1.41 KB
/
practical26.java
File metadata and controls
56 lines (45 loc) · 1.41 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
//Create a class Vehicle, which has single variable NoOfWheels. Develop two subclasses, TwoWheeler and FourWheeler. Develop subclasses of these 2 subclasses. Create instances of these classes and print appropriate details. (use super keyword).
class Vehicle {
int NoOfWheels;
Vehicle(int NoOfWheels) {
this.NoOfWheels = NoOfWheels;
}
}
class TwoWheeler extends Vehicle {
TwoWheeler() {
super(2);
}
}
class FourWheeler extends Vehicle {
FourWheeler() {
super(4);
}
}
class Bike extends TwoWheeler {
String modelName;
Bike(String modelName) {
super(); // Calls the constructor of TwoWheeler
this.modelName = modelName;
}
void displayDetails() {
System.out.println("Vehicle Type: Bike, Model: " + modelName + ", Number of Wheels: " + NoOfWheels);
}
}
class Car extends FourWheeler {
String modelName;
Car(String modelName) {
super(); // Calls the constructor of FourWheeler
this.modelName = modelName;
}
void displayDetails() {
System.out.println("Vehicle Type: Car, Model: " + modelName + ", Number of Wheels: " + NoOfWheels);
}
}
public class practical26 {
public static void main(String[] args) {
Bike myBike = new Bike("Royal Enfield");
myBike.displayDetails();
Car myCar = new Car("Honda Civic");
myCar.displayDetails();
}
}