I’m working on a small project. There will be a Main Abstract class for booking, and two subclasses, one for Hotel Bookings and the other for Flight booking both of which inherit methods from the abstract class, such as Names / Addresses of the user etc.
While trying to create a constructor for a new hotel booking, I’m having an issue inheriting the methods/variables from the abstract class using the super() function.
The Error:
Constructor in class booking can not be applied to given types.
And here’s my code:
import java.util.Scanner;
abstract class Booking{
private String fname;
private String lname;
private int HouseNo;
private String Street;
private String Postcode;
Scanner in = new Scanner(System.in);
//Parameterized Constructor
Booking(String FirstName, String Lastname, int Housenumber, String thestreet, String
thePostcode)
{
fname = FirstName;
lname = Lastname;
HouseNo = Housenumber;
Street = thestreet;
thePostcode = Postcode;
}
//Acessor Methods below
String getfname()
{
fname = "sds";
return fname;
}
void setFname(String FirstName)
{
fname = FirstName;
}
String getlname()
{
return lname;
}
void setLname(String LastName)
{
lname = LastName;
}
int getHouseNo()
{
return HouseNo;
}
void setHouseNo(int HouseNumber)
{
HouseNo = HouseNumber;
}
String getStreet()
{
return Street;
}
void setStreet(String StreetName)
{
Street = StreetName;
}
String getPostcode()
{
return Postcode;
}
void setPostcode(String ThePostcode)
{
Postcode = ThePostcode;
}
abstract public String Verification();
{
}
import java.util.Scanner;
class Hotel extends Booking{
Scanner in = new Scanner(System.in);
private String TheVerification;
private int guests;
private String Roomtype;
Hotel(){
super();
TheVerification = Verification();
guests = 0;
Roomtype = Roomtype();
}
public String Verification(){
System.out.println("Please provide your car reg as verification");
TheVerification = in.next();
return TheVerification;
}
public String Roomtype(){
System.out.println("Would you like a Premiun or Regular room?");
Roomtype = in.next();
return Roomtype;
}
public void print(){
System.out.println("Roomtype" + Roomtype);
}
}
The reason you’re getting this error is because there is no default constructor in
Booking. When you callsuperfrom an inheriting class’ constructor there has to be an appropriate constructor to call.So, when you do:
That constructor is looking for a constructor like this in
Booking:However,
Bookingonly has this constructor:So, you either need to create a default constructor in
Bookingor change yourHotelconstructor to pass arguments, like so:However, I think you should just reconsider your class hierarchy. Is a
Hotelreally a subclass of aBookingor should aHotelcontain manyBookinginstances?