I need to create class Dog and PurebredDog extending Dog. Problem is that Dog can be at once single object and array of objects (Dogs and PurebreedDogs :
Dog pack[]={new Dog(76589,"As","black",18,
"Ann","Kowalsky"),
new PurebreedDog(45321,"Labrador","Elf","black",25,
"Angus","Mati","Barbara","Smith"),
new Dog(102467,"Gamma","brown",89,
"Josh","Coke"),
new PurebreedDog(9678,"York","Theta","brown",8,
"Emka","Figaro","Alice","Cat")};
for(int i=0; i < pack.length; i++)
System.out.println(pack[i]+"\n\n");
How to write proper constructor for Dog ?
You could do :
public Dog(String name, etc){
}
but how to write constructor for array of dogs ?
public Dog(Dog[]tab) ?
And then how to recall it’s elements ? Is pack[] a 2d array ?
To simplify things, an instance of
Dogreally should refer to a single Dog. So your constructor should look similar to (the data types are just examples):PurebreedDogwould subclassDogand provide any additional constructor parameters (and members) such asbreed, etc.To deal with multiple dogs, I recommend you store instances of the class in a
List,HashTable, or other type of data structure that is designed to hold multiple elements. The actual structure you use will depend upon your requirements.