tags:

views:

137

answers:

5

Ok.

since my confusion has died down i think i may have worded my question wrong.

i have the classes "GenericMessage" "email" and "fax" in one package.

"email" and "fax" have one variable in them which is a String.

all of the above classes are in a package named "Message".

now i have created a new package named "Outbox" it contains a String array and is trying too take the Strings from "fax" and "email" into the one array.

 package Outbox;

public class Outbox extends GenericMessage
{
 GenericMessage messageList[];

 public Outbox (GenericMessage [] messageList )
 {
  messageList = new GenericMessage[50];
 }
}

my problem is that i cannot call Generic Message from the other package. how do i do this?

+2  A: 

Produce a common base class for Email and Fax

public class Message {
  //common fields and methods
}

public class Email extends Message {
  // email-specific fields and methods
}

public class Fax extends Message {
  // fax-specific fields and methods
}

Then simply use a Message array:

Message[] messageList;
Joachim Sauer
doesn't this require him being able to change Email and Fax ?
Pod
"Address" seems more appropriate than "Message".
Aaron Digulla
Yes. You didn't say in your question that you couldn't modify the classes in question. You'll have to have an Object[].
Jonathan Feinberg
@Aaron: since he calls his array messageList I assumed that he's talking about messages and not just the addresses.
Joachim Sauer
+2  A: 

Like Joachim Sauer, but a bit different:

Introduce an interface. That's neat because every subclass must implement the methods of this interface.

public interface Message {
  //methods that must be implemented
}

public class Email implements Message {
  // email-specific fields and methods
}

public class Fax implements Message {
  // fax-specific fields and methods
}

Message[] messageList;

Thanks Joachim for the code ;)

furtelwart
but wouldnt it need too take the variables from the subclasses too?not just the methods?
OVERTONE
doesn't this require him being able to change Email and Fax ?
Pod
@OVERTONE Interfaces have no fields.
Aaron Digulla
OVERTONE: Please read the official Sun documentation about interfaces. I don't think you have done this yet.Pod: I don't understand what you mean, I'm sorry.
furtelwart
You're assuming that he has the source to Fax and Email and that he can change them, is all I meant.
Pod
@Aaron Digulla, in Java interfaces can have fields. However in that case they are implicitly final, public and static.
Totophil
+1  A: 

All elements in an array must have the same type. So the first step is to assure that: Either create a common base class (or an interface) and create an array of that type. Then you can put either type into the array.

Or you can create an array of type Object (since all objects in Java have this common base class). The drawback here is that you can't see what's in the array anymore. It could be anything. That will bite you when you start working with the array: You'll need to use instanceof a lot to determine what you got and what you can do with it.

Aaron Digulla
+1  A: 

Just out of curiosity: would this compile at all... two variables in a class having the same name and two parameters of a function having the same name?

package Outbox;

public class Outbox
{
    Email messageList[];
    Fax messageList[];
    public Outbox (Email [] messageList,Fax [] messageList)
    {
        //whatever
    }
}
Amarghosh
well i had too extend the outbox too Message.i wasnt getting any errors but it still wasnt doing what i wanted.messageList wasnt defined yet though.
OVERTONE
No, it wouldn't compile. Both the duplicate field names and the constructor argument names are invalid.
Joachim Sauer
+1 won't compile, Java identifiers should have unique full names.
Totophil
A: 
package Outbox;
import Message.*;

public class Outbox
{
 GenericMessage messageList[];

 public Outbox (GenericMessage [] messageList )
 {
  messageList = new GenericMessage[50];
 }
}

never mind. i found the import command

OVERTONE