Reputation: 69
So I have a class called Room
which has the following constructor:
public class Room
{
public Room(String roomId, String description, double dailyRate)
{
this.roomId = roomId;
this.description = description;
this.dailyRate = dailyRate;
this.status = 'A';
}
}
I have sub class called PremiumRoom
as such:
public class PremiumRoom extends Room
{
public PremiumRoom(String roomId, String description, double dailyRate, int freeNights, double discountRate, double nextBookingDiscountVoucher)
{
super(roomId, description, dailyRate);
this.freeNights = freeNights;
this.discountRate = discountRate;
this.nextBookingDiscountVoucher = nextBookingDiscountVoucher;
}
}
and finally this is where i create the array:
public class TestProgram {
public static final Room[] rooms = new Room[]
{
new Room ("GARDEN0001", "NorthWest Garden View", 45.0),
new Room ("POOL0001", "Poolside Terrace", 90.0),
new Room ("GARDEN0003", "North Garden View", 35.0),
new Room ("GARDEN0005", "West Garden View", 35.0),
new Room ("POOL0002", "Poolside Private", 125.0),
new Room ("GARDEN0004", "South Garden View", 52.0)
};
}
how do i go about creating an object from the premiumroom sub class? For example, new Room ("POOL0001", "Poolside Terrace", 90.0)
suppose to be something like new PremiumRoom ("POOL0001", "Poolside Terrace", 90.0, 1, 150, 50)
, which contains the additional parameters.
How do i do it?
Upvotes: 0
Views: 56
Reputation: 26961
Create a PremiumRoom like this:
Room premium = new PremiumRoom ("POOL0001", "Poolside Terrace", 90.0, 1, 150, 50);
You will be able to insert it to the array...
When you retrieve the rooms you must use instanceof
to determine which class of Room
is and then cast to PremiumRoom
Upvotes: 4