Question 1: (Inheritance)
Consider a scenario where you have been given three classes, namely Vehicle, Bus, and
School_Bus. The School_Bus class is supposed to inherit all characteristics of both the Vehicle
and the Bus class. Explain and illustrate how you are going to achieve this considering you are using Java programming language.
class Vehicle{
public void Run(){
System.out.println("I'm driving");
}
}
class Bus extends Vehicle{
public void Run(){
System.out.println("I'm driving home");
}
}
class School_Bus extends Vehicle{
public void Run(){
System.out.println("I'm going to school");
}
}
public class Main {
public static void main(String[] args) {
Vehicle ver = new Vehicle();
Vehicle bus = new Bus();
Vehicle schoolBus = new School_Bus();
ver.Run();
bus.Run();
schoolBus.Run();
}
}
Comments
Leave a comment