In: Computer Science
Create a short program in Java which contains the below information:
- Has one abstract and two regular classes
- Has two interfaces (the two regular classes have to implement at least one interface
- At least one regular class must implement both interfaces
- The two classes have to extend the abstract class
- The abstract class must have a method
- At least one interface must have a method
- At least two fields in each class, this includes the superclass
- All the fields are instantiated within the constructor
interface 1 :
Vehicle.java
public interface Vehicle {
// mothod inside interface
void getManufacturer();
}
------------------------------------------------------------------------------------------------------------
interface 2 :
VehicleParts.java
public interface VehicleParts {
// two methods inside interface
void getNumberOfWheels();
void getVehicleBodyDetails();
}
-------------------------------------------------------------------------------------------------------------
abstract class
Offer.java
public abstract class Offer {
// variable inside abstract class
boolean isOffer;
// abstract method inside abstract class
abstract void getOfferDetails();
}
-------------------------------------------------------------------------------------------------------------------------------
regular class 1:
Car.java
public class Car extends Offer implements Vehicle, VehicleParts
{
// variables in the class
String varient;
double price;
Offer offer;
// parameterised constructor and is instantiated
public Car(String varient, double price, Offer
offer) {
this.varient = varient;
this.price = price;
this.offer = offer;
}
// default constructor
public Car() {
super();
}
// abstract class method implementation
@Override
void getOfferDetails() {
System.out.println("No offers
available !");
}
// method inside interface vehicleParts and locally implemented
@Override
public void getNumberOfWheels() {
System.out.println("This is a car
and have 4 wheels.");
}
// method inside interface vehicleParts and locally implemented
@Override
public void getVehicleBodyDetails() {
System.out.println("High quality
Metal Body.");
}
// method inside interface vehicle and locally implemented
@Override
public void getManufacturer() {
System.out.println("This Car is
manufactured by TOYOTA.");
}
}
----------------------------------------------------------------------------------------------------
regular class2
Bike.java
public class Bike extends Offer implements Vehicle {
// variables in the class
String model;
String brakeSystem;
Offer offer;
// parameterised constructor and is instantiated
public Bike(String model, String brakeSystem, Offer
offer) {
this.model = model;
this.brakeSystem =
brakeSystem;
this.offer = offer;
}
//default constructor
public Bike() {
super();
}
// abstract class method implementation
@Override
void getOfferDetails() {
System.out.println("Get 5000*
offer. Expire soon !");
}
// method inside interface vehicle and locally implemented
@Override
public void getManufacturer() {
System.out.println("This is
manufactured by 'ROYAL ENFIELD'");
}
}
-------------------------------------------------------------------------------------------------
test class
Showroom.java
public class Showroom {
public static void main(String[] args) {
Car newCar = new Car();
Bike newBike = new Bike();
// set values to the fields in
Car
newCar.varient = "XZPlus";
newCar.price = 1000000;
newCar.isOffer = false;
// set values to the fields in
bike
newBike.model = "Thunder";
newBike.brakeSystem = "ABS";
newBike.isOffer = true;
// print car details
newCar.getManufacturer();
newCar.getNumberOfWheels();
newCar.getOfferDetails();
System.out.println("\n");
// print bike details
newBike.getManufacturer();
newBike.getOfferDetails();
}
}