-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleHierarchy.java
More file actions
36 lines (31 loc) · 1.04 KB
/
Copy pathVehicleHierarchy.java
File metadata and controls
36 lines (31 loc) · 1.04 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
// 4. Design a class hierarchy for different types of vehicles. Create a base class 'Vehicle' with methods 'startEngine()' and 'stopEngine()'. Derive two classes 'Car' and 'Motorcycle' from 'Vehicle'. Add methods 'drive()' in 'Car' and 'ride()' in 'Motorcycle'. Write a Java program to demonstrate hierarchical inheritance and invoke the relevant methods for a car and a motorcycle object.
class Vehicle{
void startEngine(){
System.out.println("Engine started");
}
void stopEngine(){
System.out.println("Engine stopped");
}
}
class Car extends Vehicle{
void drive(){
System.out.println("Ram is driving car");
}
}
class Motorcycle extends Vehicle{
void ride(){
System.out.println("Ram is riding motorcycle");
}
}
public class VehicleHierarchy {
public static void main(String[] args) {
Car c = new Car();
c.startEngine();
c.drive();
c.stopEngine();
Motorcycle m = new Motorcycle();
m.startEngine();
m.ride();
m.stopEngine();
}
}