implementing an interface...

hello I want to do something like that:

publicinterface Interf{

publicvoid aMethod(Object o);

}

and then have:

publicclass AClassimplements Interf{

publicvoid aMethod(String s);

}

and it doesnt work, I mean aMethod is not overriden. Shouldnt be? String is Object's subclass. Polimorphism?If someone could explain me way this is not possible, why they didnt make it possible? Anyway also if someone think how else I could do something like that, have a general interface implemented for more specific tasks, would be appreciated!

[992 byte] By [fraksiaa] at [2007-11-27 3:45:09]
# 1

public class AClass implements Interf{

public void aMethod(String s);

}

You didn't actually implement this method, you have to give it a body:

public class AClass implements Interf{

public void aMethod(String s) {

// your implmentation here

}

}

hunter9000a at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 2

> it doesnt work, I mean aMethod is not overriden.

> Shouldnt be? String is Object's subclass.

What? I don't understand the question. AClass should be declared as abstract or implement the method aMethod.

aMetod in AClass is more restrictive than the method declared in the interface so it does not fulfill the contact.

Kaj

kajbja at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 3
> You didn't actually implement this method, you have> to give it a body:Note that the original post has different types for the argument.Kaj
kajbja at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 4
yeah, I'm sorry thats what I meant just put two brackets. The thing is why arguments cannot be of a subclass of the arguments of the overiden method.That's the point!Message was edited by: fraksia
fraksiaa at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 5
> > You didn't actually implement this method, you> have> > to give it a body:> > Note that the original post has different types for> the argument.> > KajI missed that, good catch.
hunter9000a at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 6

Also, since the interface expects that it can pass ANY subclass of Object, restricting your implementation to String-only won't suffice.

You could do

public void aMethod(Object o)

{

if (o != null) {

aMethod(o.toString());

}

}

public void aMethod(String s)

{

//..your stuff

}

xiarcela at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 7
Am I getting something wrong? This was just an example I do not want to use a string, I want to know why the method cannot be overiden with different arguments but of subclass of the original arguments.
fraksiaa at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 8

> yeah, I'm sorry thats what I meant just put two

> brackets. The thing is why arguments cannot be of a

> subclass of the arguments of the overiden

> method.That's the point!

>

> Message was edited by:

> fraksia

If you implement the method to take a String, but the interface takes an Object, then you've restricted the number of types the method can take, thus you aren't fulfilling the contract of the interface. What happens when you want to pass a List to the interface? It's a type of Object, so it should fit the bill, but your implementation only takes Strings. So you can't use it, even though it complies with the interface.

hunter9000a at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 9
You're really looking for [url= http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html]generics[/url]
georgemca at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 10

Hhm, yes I see what you mean but the thing is that I actually want to restrict the types passed on to the method. Your solution, or an instanceof check are solution but it would seem prettier, I believe, to be able just to restrict the types passed without after checking. But yes, I see the contradiction with the Interface declaration... thanks anyway.

fraksiaa at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 11

> Hhm, yes I see what you mean but the thing is that I

> actually want to restrict the types passed on

> to the method. Your solution, or an instanceof check

> are solution but it would seem prettier, I believe,

> to be able just to restrict the types passed without

> after checking. But yes, I see the contradiction with

> the Interface declaration... thanks anyway.

George is right, generics are just the thing you want.

hunter9000a at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 12

> Hhm, yes I see what you mean but the thing is that I

> actually want to restrict the types passed on

> to the method. Your solution, or an instanceof check

> are solution but it would seem prettier, I believe,

> to be able just to restrict the types passed without

> after checking. But yes, I see the contradiction with

> the Interface declaration... thanks anyway.

Did you see the reply that mentioned Generics?

Kaj

kajbja at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 13

interface Interf<T> {

public void aMethod(T o);

}

public class AClass implements Interf<String> {

public void aMethod(String s) {

}

}

DrLaszloJamfa at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...
# 14
yes, thank you Im checking it!Exactly what I needed!!Message was edited by: fraksia
fraksiaa at 2007-7-12 8:48:51 > top of Java-index,Java Essentials,Java Programming...