Problem Statement Write the code to implement the concept of inheritance forVehi
ID: 3614545 • Letter: P
Question
Problem Statement
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance betweenclasses. You have to write four classes in java i.e. one superclass, two sub classes and one driver class.
Vehicle is the super class whereas Bus and Truck are sub classesof Vehicle class. Transport is a driver class which contains mainmethod.
Detailed description:
Detailed description of Vehicle (Super class):
The class Vehicle must have following attributes:
The Vehicle class must have following methods:
Detailed description of Truck (Sub class):
The class Truck must have following attribute:
Cargo weight limit (Kilo grams)
The above class must have following methods:
Detailed description of Bus (Sub class):
The class Bus must have following attribute:
No of passengers
The above class must have following methods:
Create a class Transport whichcontains the main method. Perform the following within mainmethod:
Explanation / Answer
Without actually doing the whole code for you--which would keep youfrom actually learning how to implement these concepts (which arevery, very important in Object-Oriented design), I'll walk youthrough what you should do. When approaching a programmingassignment such as this one, always code your super class first,since this is the class from which you will derive you otherclasses. Your superclass in this case is the vehicle class. So goahead and write the header of this class:
public class Vehicle {
//this is where you will declare your class variables (also calledattributes)
//this is where you will declare your class methods
}
Now you know you will derive two classes from this superclass--oneis the bus class and the other is the truck class. Go ahead andwrite the headers for these classes as well:
public class Bus extends Vehicle {
//this is where you will declare your class variables (also calledattributes)
//this is where you will declare your class methods
}
public class Truck extends Vehicle {
//this is where you will declare your class variables (also calledattributes)
//this is where you will declare your class methods
}
Your last required class is the Transport class, which will containyour main method--declare this as well:
public class Transport {
public static void main (String[] args) {
//This is where your program will start running
}
}
Now you will replace everywhere I have comment marks with actualcode. Your assignment tells you what to put in each case. Forexample, for the vehicle class, you will declare variables forvehicle model, reg number, vehicle speed, fuel capacity, fuelconsumption:
private String model;
private int regNumber;
private int speed;
private int fuelCapacity;
private int fuelConsumption;
(Of course, if your instructor wants the vehicle registrationnumber also as a string, change int to string--feel free to changeany variable type or any of the visibility modifiers. The design isup to you and your instructor)
In any case, since all of these variables are declared in theVehicle class (the super class), all classes derived from Vehicle(such as Bus and Truck) will contain these variables. You do notneed to declare them again in those classes. The same goes for themethods you write in Vehicle. You can always overridemethods--which your instructor wants you to do for a fewmethods--to have that specific method do something a littledifferent in the class you are overriding the method in. Think ofoverriding as tweaking the method that is in your superclass.
I think this is a good starting point for you. Start here and fillin your methods, variables, etc according to what your teacherwants. I don't want to write out all of the code for you, becausethen you won't learn it. If you get stuck, post your code for allyour class (there will be 4) and your specific question.
Good luck and get cracking!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.