1

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 ?

6
  • 8
    what is the current problem? Commented Mar 21, 2010 at 17:07
  • I think he asks, how can he have a Constructor accepting indefinite args. Commented Mar 21, 2010 at 17:10
  • then let him ask it ;) furthermore, just today there was a question about variable method arguments. Commented Mar 21, 2010 at 17:12
  • @owca - sorry, it still doesn't make much more sense Commented Mar 21, 2010 at 17:14
  • 3
    It seems like a horrible idea to have a Dog class represent both a single dog and a collection of dogs. Justin's suggestion to use a collection class for the latter is a good one. Commented Mar 21, 2010 at 17:15

4 Answers 4

5

To simplify things, an instance of Dog really should refer to a single Dog. So your constructor should look similar to (the data types are just examples):

Dog(int ID, String color, String name, ...)

PurebreedDog would subclass Dog and provide any additional constructor parameters (and members) such as breed, 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.

Sign up to request clarification or add additional context in comments.

Comments

1

In your specific case, a Dog should represent one dog, and I agree with Justin's answer.

Just for the sake of completeness, I think it's worth mentioning the composite pattern though.

There are indeed some circumstances where an aggregation of objects can acts the same way as its individual parts. A traditional example is the folder/file hierarchy. Both responds to the same methods such as size, delete, etc.

That could even be used with you Dog example if you plan to model a genealogy tree. A dog is a dog, but can have N children.

Comments

1

The way to make uniform the representation of one dog and many dogs is for each entity to be a list. The one-dog entity is simply a list with one dog in it; the multiple-dog entity, of course, has more than one dog in it. But all you need to code is the (single) Dog, and then, as Justin and ewernli said, use a Collection - probably an ArrayList of Dog.

Trying to make the un-enlisted single Dog the "same" as a list of dogs is just crazy.

1 Comment

I know, but I'm not the one responsible for creating this task :/
0

Okay, so did it the following way :

PurebreedDog class:

public class PurebreedDog extends Dog {

private String breed;
private String mother;
private String father;

public PurebreedDog(int id, String breed, String name, String color, int age,
        String owner_name, String owner_surname, String mother, String father){
    super(id, name, color, age, owner_name, owner_surname);
    this.breed = breed;
    this.mother = mother;
    this.father = father;
}

@Override
public String toString(){
    return super.toString()+"\n Pure breed dog \n"+"-------------------------------\n"+
            "BREED: "+this.breed+"\n"+"MOTHER: "+this.mother+"\n"+
            "FATHER: "+this.father+"\n-------------------------------";
}

}

Dog class:

public class Dog {

private int id;
private String name;
private String owner_name;
private String owner_surname;
private String color;
private int age;
private Dog[]pack;

public Dog(int i, String n, String c, int w, String on, String os){
    id = i;
    name = n;
    color = c;
    age = w;
    owner_name = on;
    owner_surname = os;
}

public Dog(Dog[]p){

    Dog[]group = new Dog[p.length];
    for(int i=0; i < p.length; i++){
        group[i] = p[i];
    }

    this.pack = group;

}

@Override
public String toString(){

    return "-------------------------------\n"+
            "idr: "+this.id+"\n"+"OWNER: "+this.owner_name+
            this.owner_surname+"\n"+"NAME: "+this.name+"\n"+
            "color: "+this.color+"\n-------------------------------";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.