Reputation: 3
I have two constructors I want to use in order to differentiate two types of objects in the same array, though the "Type is mismatched". So I know I have to declare the array differently, I'm just not sure what to do.
For the first three places in the array I want to use the Pilots class, and for the others the Passenger class.
public class Project_Space
{
public static void main(String[] args)
{
// 2D Array of Passengers objects- the Passengers class is in the Passengers.java file
//Pilot objects are constructed from Pilots.java file
Passengers[][] Members ;
int num_flights= 6; //create the "x" bound/size of the array
int num_passengers= 9; //create the "y" bound/size of the array
Members = new Passengers[num_flights][num_passengers]; //define the size of the array 6flights/9Passengers
//The Members array at index 0 is the first flight
//Members[0][0] = new Pilots(1,-1,"","","",0); //This spot in the array is for the Flight number in the first spot. Everything else are place holders for data that doesn't pertain to the company
Members[0][1] = new Pilots(1,"1877963200","Amadeus","Durrutti","Buckminster Cornwallis","1288211435", 11); //This spot in the array is for the first passenger of flight #1
Members[0][2] = new Pilots(2,"6054350085","Sirius","Sassafrass","Ali Bababa","1776812631", 9);
Members[0][3] = new Passengers(1,"7065253333","Amy","Hartman","Betty Sue","7708889999", 3, 50000,"0554055405540554");
Members[0][4] = new Passengers(2,"7545251337","Amanda","Zion","Beatrice Twix","7448656577", 4, 2000,"0554055405541111");
Members[0][5] = new Passengers(3,"8904448899","Janet","Graves","Neal Wade","4445556666", 5, 3000, "9031029266161432");
Members[0][6] = new Passengers(4,"8902234567","Kristen","Pickering","Christopher Soto","5685461208", 6, 51500, "0985028135114275");
Members[0][7] = new Passengers(5,"5000893778","Julianna","Estrada","Jill Hansen","2770779833", 7, 0, "0213595590286251");
Members[0][8] = new Passengers(6,"2080670437","Regena","Mckenzie","Vicki Cook","6224215759", 8, 250, "8204699533830238");
Upvotes: 0
Views: 172
Reputation: 4057
This is a nice example of OOP.
If Pilots and Passengers are both sub-classes of the same base class, this will work fine. For instance, both classes may contain similar fields, like name and id.
Declare the base class Person with those fields and any other common methods that both classes contain. Then you will be able to declare the array as Person[][] Members;
public class Person {
String firstName,lastName;
int id;
//constructors, getter and setter methods
}
Then Passenger and Pilot can both inherit from Person like this:
public class Pilot extends Person {
int totalHoursLogged;
String airline;
int salary;
//constructors and methods that differentiate Pilots from Passengers.
}
Upvotes: 1
Reputation: 68715
Create an array of Object
class or define a parent class for Pilots
and Passengers
and then create an array of your superclass. You will be able to add any child class objects to parent class array.
Upvotes: 0