Create a class named Vehicle with instance variables make (String type), color (String type), year ( integer type) and model ( String type). All the variables have default access behaviour. Vehicle class also contains a constructor with four parameters to initialize the make, color, year and model of a specific Vehicle object.
Vehicle class also has a method named printDetails() with return type void and default access to show the output of make, color, year and model of a specific Vehicle object.
Create another class named Car that inherits Vehicle class. Car class has a private instance variable called bodyStyle (String type). The Car class also should have a constructor with five parameters. Now create the main class named Main. Inside the main method of the Main class create an object of Car class by calling the Car class constructor. Then call the printDetails() method using the created object. Finally also output body style of the Car object.
class Vehicle {
private String make;
private String color;
private int year;
private String model;
public Vehicle() {
}
public Vehicle(String make, String color, int year, String model) {
this.make = make;
this.color = color;
this.year = year;
this.model = model;
}
public void printDetails() {
System.out.println("Vehicle make: " + make);
System.out.println("Vehicle color: " + color);
System.out.println("Vehicle year: " + year);
System.out.println("Vehicle model: " + model);
}
/**
* @return the make
*/
public String getMake() {
return make;
}
/**
* @param make the make to set
*/
public void setMake(String make) {
this.make = make;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
}
class Car extends Vehicle {
private String bodyStyle;
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model);
this.bodyStyle = bodyStyle;
}
/**
* @return the bodyStyle
*/
public String getBodyStyle() {
return bodyStyle;
}
/**
* @param bodyStyle the bodyStyle to set
*/
public void setBodyStyle(String bodyStyle) {
this.bodyStyle = bodyStyle;
}
}
public class App {
/** Main Method */
public static void main(String[] args) {
Car car = new Car("Ford", "Red", 2015, "X4", "F5");
car.printDetails();
System.out.print("Car body style: " + car.getBodyStyle());
}
}
Comments
Leave a comment