problem casting to an interface

Hi,

The "IDInterface anId=((MsgInterface)anMsg).getMsgId();" line in the following code gets me with:

.../SwarmSystem/src/swarm/sys/mail/Mailbox.java:85: cannot find symbol

symbol : method getMsgId()

location: class java.lang.Object

IDInterface anId=((MsgInterface)anMsg).getMsgId();

That is i cast to an interface -although there should be really no need to, since i use generics- and then call the "getMsgId" method. The cast, however, doesn't work and the method seems to be called on an java.lang.Object object.

publicclass Mailbox<MsgInterface>

{

privateboolean empty=true;

privateint size=0;

private Collection<MsgInterface> msgBox=new HashSet<MsgInterface>();

private ID id;

private String label="";

private Level logLevel;

public Mailbox(ID id, String label,Level logLevel)

{

this.id=id;

this.label=label;

this.logLevel=logLevel;

initLogger(logLevel);

}//Mailbox

...

publicsynchronized Collection<MsgInterface> removeAndGetMessages()

{

while (empty)

{

try

{

wait();

}

catch(InterruptedException e)

{

}

}

Collection<MsgInterface> msgBoxCopy=new HashSet<MsgInterface>();

msgBoxCopy.addAll(msgBox);

//logging

String nsMsgId="";

if (!(logLevel==Level.OFF))

{

Iterator<MsgInterface> iMsgBox=msgBox.iterator();

while (iMsgBox.hasNext())

{

MsgInterface anMsg=iMsgBox.next();

IDInterface anId=((MsgInterface)anMsg).getMsgId();

nsMsgId=nsMsgId+ anId.toString()+" ";

}

}

cat.info("removing all messages from mailbox "+id.toString()+": "+nsMsgId);

//logging

msgBox.clear();

empty=true;

notifyAll();

return msgBoxCopy;

}//removeAndGetMessages

...

}

publicinterface MsgInterface

{

public IDInterface getMsgId();

public IDInterface getSender();

public IDInterface getReceiver();

public Object getLoad();

publicvoid setLoad(Object data);

}

publicinterface IDInterface

{

public String getType();

public String getBody();

publicint getIntBody();

}

[4320 byte] By [uiga] at [2007-11-26 22:20:54]
# 1
Is there some other error, like maybe that it doesn't know about MsgInterface?The only way I can see that error occurring is if it doesn't know about MsgInterface, and so puts a placeholder of Object in for it.
jverda at 2007-7-10 11:18:15 > top of Java-index,Java Essentials,Java Programming...
# 2
And no, you shouldn't need the cast, not because of generics, but becaue anMsg is declared to be of type MsgInterface.
jverda at 2007-7-10 11:18:15 > top of Java-index,Java Essentials,Java Programming...
# 3
Are you sure you've compiled everything since your last change, and there aren't other copies of these source files laying around?Something looks very not right here.
jverda at 2007-7-10 11:18:15 > top of Java-index,Java Essentials,Java Programming...
# 4

Iterator<MsgInterface> iMsgBox=msgBox.iterator();

while (iMsgBox.hasNext())

{

MsgInterface anMsg=iMsgBox.next();

IDInterface anId=((MsgInterface)anMsg).getMsgId(); //problem

nsMsgId=nsMsgId+ anId.toString()+" "

}

The above problem can be solved by casting to one of the existing MsgInterface implementation (SysMsg for example below):

Iterator<MsgInterface> iMsgBox=msgBox.iterator();

while (iMsgBox.hasNext())

{

MsgInterface anMsg=iMsgBox.next();

IDInterface anId=((SysMsg)anMsg).getMsgId(); //no problem

nsMsgId=nsMsgId+ anId.toString()+" "

}

However, this is not the case since i do not know -i cannot- the runtime class and since there is no need to cast to the runtime class, but to the interface. Any ideas appreciated...

uiga at 2007-7-10 11:18:15 > top of Java-index,Java Essentials,Java Programming...
# 5
What i need to clear out is: can one use an interface as a generics type parameter? ie Vector<anInterface>
uiga at 2007-7-10 11:18:15 > top of Java-index,Java Essentials,Java Programming...