Polymorphism

M

Marc-Andre Michel

Hi,

Could somebody tell me why the following code doesn't work ?

I presume the reason is that the type to cast to is not known at
compile time. If this is the reason,
what is your suggestion so A() can call the right method for the right
object type ?


public class A {
public A(Object obj) {
foo(obj); //ERROR
}

public void foo(String s) {
//...print a string
}

public void foo(Collection c) {
//...print a collection of strings
}

public static void main(String[] args) {
String s = "my string";
A a1 = new A(s);

ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
A a2 = new A(l);
}
}

Thanks,

Marc
 
M

Manish Hatwalne

Not really a well designed class, but this is what you can do in the
constructor, if you insist on using it that way (really you should be
calling appropriate public method with correct argument) -

public A(Object obj) {
if(obj instanceof String){
foo((String)obj);
}else if(obj instanceof Collection){
foo((Collection)obj);
}
}

HTH,
- Manish
 
X

xarax

Manish Hatwalne said:
Not really a well designed class, but this is what you can do in the
constructor, if you insist on using it that way (really you should be
calling appropriate public method with correct argument) -

public A(Object obj) {
if(obj instanceof String){
foo((String)obj);
}else if(obj instanceof Collection){
foo((Collection)obj);
}
}

HTH,
- Manish

Marc-Andre Michel said:
Hi,

Could somebody tell me why the following code doesn't work ?

I presume the reason is that the type to cast to is not known at
compile time. If this is the reason,
what is your suggestion so A() can call the right method for the right
object type ?


public class A {
public A(Object obj) {
foo(obj); //ERROR
}

public void foo(String s) {
//...print a string
}

public void foo(Collection c) {
//...print a collection of strings
}

public static void main(String[] args) {
String s = "my string";
A a1 = new A(s);

ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
A a2 = new A(l);
}
}

Thanks,

Marc

A better solution is to overload the constructor.

public class A
{
public A(String s)
{
foo(s);
}

public A(Collection c)
{
foo(c);
}

public void foo(String s)
{
//...print a string
}

public void foo(Collection c)
{
//...print a collection of strings
}

public static void main(String[] args)
{
String s = "my string";
A a1 = new A(s);

ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
A a2 = new A(l);
}
}


--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!
 
T

Thomas G. Marshall

Manish Hatwalne said:
Not really a well designed class, but this is what you can do in the
constructor, if you insist on using it that way (really you should be
calling appropriate public method with correct argument) -

public A(Object obj) {
if(obj instanceof String){
foo((String)obj);
}else if(obj instanceof Collection){
foo((Collection)obj);
}
}

HTH,
- Manish


That's a disaster in the making because it allows an object to be
instantiated for non String or Collection types.

You /almost/ alluded to this.

RTTI is really to be avoided if an alternative is /easily/ available. You
might consider answering a similar newbie question with two solutions. The
RTTI one above, and one with overloaded constructors. Overloaded
constructors allows the object to only be created with the types it
understands.
 
M

Manish Hatwalne

Actually, As I mentioned, it's better to use the overloaded method.
But yeah, 2 constructor solution is better than one object constructor, but
why do you want to use two constructors when you've already overloaded the
method??????

- Manish
 
T

Thomas G. Marshall

Manish Hatwalne said:
"Thomas G. Marshall"
Actually, As I mentioned, it's better to use the overloaded method.
But yeah, 2 constructor solution is better than one object
constructor, but why do you want to use two constructors when you've
already overloaded the method??????

- Manish


Do not top post once someone has already bottom posted. You're making a
mess.

In this particular case I am supplying an answer to a general question. His
code indicates that he wants a tailored object by constructor (for some bad
reason). But it does not matter. Of the three possibilities:

RTTI constructor
overloaded constructor
empty constructor with everything in overloaded methods

yours (RTTI construction) is the least desirable. RTTI is not a bad thing
per se. Hardly, it is very useful. But if it can /easily/ be avoided, it
should be.
 
G

Guest

Hi,

Could somebody tell me why the following code doesn't work ?

First error - you seem to double close most of your blocks, instead of
using a single closing brace.
I presume the reason is that the type to cast to is not known at compile
time. If this is the reason,
what is your suggestion so A() can call the right method for the right
object type ?


public class A {
public A(Object obj) {
foo(obj); //ERROR
}
}
public void foo(String s) {
//...print a string
}
}
public void foo(Collection c) {
//...print a collection of strings
}
}

I don't see foo(Object)! I see foo(String) and foo(Collection). Perhaps
you should provide a constructor that takes a String and another that
takes a Collection. Another (poor) choice would be create foo(Object)
which tests if its argument is a String or Collection and call the
appropriate method (otherwise throw an IllegalArgumentExcaption).


HTH,
La'ie Techie
 
J

Jim Cochrane

First error - you seem to double close most of your blocks, instead of
using a single closing brace.


I don't see foo(Object)! I see foo(String) and foo(Collection). Perhaps
you should provide a constructor that takes a String and another that
takes a Collection. Another (poor) choice would be create foo(Object)
which tests if its argument is a String or Collection and call the
appropriate method (otherwise throw an IllegalArgumentExcaption).

By the way, unless I missed something (I glanced at the original post,
but don't remember it in detail.), you're not really dealing with
polymorphism here (which works in tandem with dynamic binding to produce
one of the most powerful facilities of an OO language), but with function
overloading, a rather different (and not as powerful) beast. (Just FYI -
hope this helps.)

(
Note: I just did a bit of research and it looks like I need to clarify what
I said above. According to:
http://mindprod.com/jgloss/polymorphism.html
function overloading qualifies as polymorphism; however, according to:
http://www.webopedia.com/TERM/p/polymorphism.html
my definition above is correct. So perhaps it depends on whom you talk to,
but, IMO, the latter definition is better because it helps eliminate
confusion for beginners - polymorphism and dynamic binding are quite
different from function overloading.

[Oh, oh - have I started a new debate?]
)
 
T

Thomas G. Marshall

Jim Cochrane said:
First error - you seem to double close most of your blocks, instead
of
using a single closing brace.


I don't see foo(Object)! I see foo(String) and foo(Collection).
Perhaps
you should provide a constructor that takes a String and another that
takes a Collection. Another (poor) choice would be create
foo(Object)
which tests if its argument is a String or Collection and call the
appropriate method (otherwise throw an IllegalArgumentExcaption).

By the way, unless I missed something (I glanced at the original post,
but don't remember it in detail.), you're not really dealing with
polymorphism here (which works in tandem with dynamic binding to
produce
one of the most powerful facilities of an OO language), but with
function
overloading, a rather different (and not as powerful) beast. (Just
FYI -
hope this helps.)

(
Note: I just did a bit of research and it looks like I need to
clarify what
I said above. According to:
http://mindprod.com/jgloss/polymorphism.html
function overloading qualifies as polymorphism; however, according to:
http://www.webopedia.com/TERM/p/polymorphism.html
my definition above is correct. So perhaps it depends on whom you
talk to,
but, IMO, the latter definition is better because it helps eliminate
confusion for beginners - polymorphism and dynamic binding are quite
different from function overloading.

[Oh, oh - have I started a new debate?]
)

Not in the least. Overloading a method is considered /Ad-Hoc/ polymorphism.
 
M

Marc-Andre Michel

Hi,
Thanks for all the aswers and sorry for the mess...

I know that the example is not well designed but basically I had a
class A which receives an Object and simply casts it to a String. My
task is to add support for Collection of String.

It was not really clear in my first post, but what I'd like to do is
to avoid RTTI. I can't create 2 constructors (one for String and one
for Collection) cos actually foo() is called from another method, not
from the constructor. I wanted to write an example as simple as
possible but the following one may be more clear:

public interface MyInterface {
public void execute(Object obj);
}

public class A implements MyInterface {
public A() {
}

public void execute(Object obj) {
foo(obj); //ERROR - not
applicable for the arguments
}

public void foo(String s) {
//...print a string
}

public void foo(Collection c) {
//...print a collection of strings
}

public static void main(String[] args) {
MyInterface m = new A();

String s = "my string";
m.execute(s);

ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
m.execute(l);
}
}


In this new example, wether it is good or bad design I have to cope
with execute(Object) but I don't want to use RTTI. Do you think it's
possible ?
 
X

xarax

Thomas G. Marshall said:
Jim Cochrane said:
On Sat, 31 Jul 2004 06:50:21 -0700, Marc-Andre Michel wrote:

Hi,

Could somebody tell me why the following code doesn't work ?

First error - you seem to double close most of your blocks, instead
of
using a single closing brace.


I presume the reason is that the type to cast to is not known at
compile
time. If this is the reason,
what is your suggestion so A() can call the right method for the
right
object type ?


public class A {
public A(Object obj) {
foo(obj); //ERROR
}
}
public void foo(String s) {
//...print a string
}
}
public void foo(Collection c) {
//...print a collection of strings
}
}

I don't see foo(Object)! I see foo(String) and foo(Collection).
Perhaps
you should provide a constructor that takes a String and another that
takes a Collection. Another (poor) choice would be create
foo(Object)
which tests if its argument is a String or Collection and call the
appropriate method (otherwise throw an IllegalArgumentExcaption).

By the way, unless I missed something (I glanced at the original post,
but don't remember it in detail.), you're not really dealing with
polymorphism here (which works in tandem with dynamic binding to
produce
one of the most powerful facilities of an OO language), but with
function
overloading, a rather different (and not as powerful) beast. (Just
FYI -
hope this helps.)

(
Note: I just did a bit of research and it looks like I need to
clarify what
I said above. According to:
http://mindprod.com/jgloss/polymorphism.html
function overloading qualifies as polymorphism; however, according to:
http://www.webopedia.com/TERM/p/polymorphism.html
my definition above is correct. So perhaps it depends on whom you
talk to,
but, IMO, the latter definition is better because it helps eliminate
confusion for beginners - polymorphism and dynamic binding are quite
different from function overloading.

[Oh, oh - have I started a new debate?]
)

Not in the least. Overloading a method is considered /Ad-Hoc/ polymorphism.

I think the general OOP crowd, including Bertrand Meyer,
will argue that function overloading is not polymorphism.

The mindprod definition is incorrect.

The webopedia definition is correct.

Polymorphism concept applies to derived types at run-time.
Function signature overloading applies at compile-time. If
the signature cannot be matched at compile-time, an error
is generated. OTOH, polymorphism is resolved at run-time
(the compiler has already verified that the signature will
match *something*).

If anyone wants to continue this debate, please open
a new topic.
 
R

Roedy Green

I think the general OOP crowd, including Bertrand Meyer,
will argue that function overloading is not polymorphism.

Polymorphism mean "many forms", so literally the term applies to both
overloading and overriding.

The hard part to implement is overriding, so I can see why compiler
writers want to exclude languages that do only overloading as being in
the same league as theirs.
 
X

xarax

Please don't top-post. Scroll to bottom to see my reply.

Marc-Andre Michel said:
Hi,
Thanks for all the aswers and sorry for the mess...

I know that the example is not well designed but basically I had a
class A which receives an Object and simply casts it to a String. My
task is to add support for Collection of String.

It was not really clear in my first post, but what I'd like to do is
to avoid RTTI. I can't create 2 constructors (one for String and one
for Collection) cos actually foo() is called from another method, not
from the constructor. I wanted to write an example as simple as
possible but the following one may be more clear:

public interface MyInterface {
public void execute(Object obj);
}

public class A implements MyInterface {
public A() {
}

public void execute(Object obj) {
foo(obj); //ERROR - not
applicable for the arguments
}

public void foo(String s) {
//...print a string
}

public void foo(Collection c) {
//...print a collection of strings
}

public static void main(String[] args) {
MyInterface m = new A();

String s = "my string";
m.execute(s);

ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
m.execute(l);
}
}


In this new example, wether it is good or bad design I have to cope
with execute(Object) but I don't want to use RTTI. Do you think it's
possible ?
/snip/

Just define the interface with two overloaded methods.
Then implement those methods to pass their argument
to foo.

public interface MyInterface
{
public abstract void execute(String s);
public abstract void execute(Collection c);
}

public class A
implements MyInterface
{
public void foo(String s)
{
// do something with the String
}

public void foo(Collection c)
{
// do something with the Collection
}

public void execute(String s)
{
foo(s);
}

public void execute(Collection c)
{
foo(c);
}

public static void main(String[] args)
{
MyInterface m = new A();

String s = "my string";
m.execute(s);

ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
m.execute(l);
}
}


--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!
 
X

xarax

Roedy Green said:
Polymorphism mean "many forms", so literally the term applies to both
overloading and overriding.

The hard part to implement is overriding, so I can see why compiler
writers want to exclude languages that do only overloading as being in
the same league as theirs.

Roedy, you're wrong on this. Don't confuse the newbies.

Overloading is not polymophism. Never has been, never will be.
Two entirely different concepts; you can have one without the
other, or both, or neither, depending on the programming language.
 
T

Thomas G. Marshall

xarax said:
"Thomas G. Marshall"
Jim Cochrane <[email protected]> coughed up the following:
....[gash]...
[Oh, oh - have I started a new debate?]
)

Not in the least. Overloading a method is considered /Ad-Hoc/
polymorphism.

I think the general OOP crowd, including Bertrand Meyer,
will argue that function overloading is not polymorphism.

The mindprod definition is incorrect.

The webopedia definition is correct.

Polymorphism concept applies to derived types at run-time.
Function signature overloading applies at compile-time. If
the signature cannot be matched at compile-time, an error
is generated. OTOH, polymorphism is resolved at run-time
(the compiler has already verified that the signature will
match *something*).

Hence the term "ad-hoc". I view that as the term "not-really polymorphism".

For me, I would argue (as you do) that ad-hoc polymorphism is not
polymorphism.

....[rip]...
 
T

Thomas G. Marshall

Marc-Andre Michel said:
Hi,
Thanks for all the aswers and sorry for the mess...

I know that the example is not well designed but basically I had a
class A which receives an Object and simply casts it to a String. My
task is to add support for Collection of String.

It was not really clear in my first post, but what I'd like to do is
to avoid RTTI.

GOOD for you.
 
B

Ben Wilson

Marc-Andre Michel said:
Hi,

Could somebody tell me why the following code doesn't work ?

I presume the reason is that the type to cast to is not known at
compile time. If this is the reason,
what is your suggestion so A() can call the right method for the right
object type ?
....


Dear Marc:

Your code does not work because you're confusing a couple of things. Most
important of them is how casting works. I've got bad news for you, for in
your post you say "I presume the reason is that the type to cast to is not
known" however nowhere in the code that you have shown are you doing a cast.
A cast looks like (int)myShort or (Object)myVector. Passing a String to a
method that expects an Object is not casting, it's implicit conversion.

Your code will never work because you are asking the compiler to implicitly
convert an Object to, alternatively, a String or a Collection. The compiler
will not do this for you for the class Object extends neither String nor
Collection. Yes, you *can* say:

A a2 = new A(l);

because ArrayList extends Object. If you want it to work the other way
around you must use a cast. Casting of object references is done at runtime,
not compile time.

B.
 
D

David Hilsee

xarax said:
Roedy, you're wrong on this. Don't confuse the newbies.

Overloading is not polymophism. Never has been, never will be.
Two entirely different concepts; you can have one without the
other, or both, or neither, depending on the programming language.

The term "polymorphism" can apply to compile-time polymorphism or runtime
polymorphism (aka "dynamic binding"). Many people will use the term
"polymorphism" to refer to dynamic binding, but that doesn't mean that the
term can't also be used to refer to compile-time constructs like
overloading. For example, C++ templates are sometimes called compile-time
polymorphism.
 
B

Ben Wilson

Jim Cochrane said:
By the way, unless I missed something (I glanced at the original post,
but don't remember it in detail.), you're not really dealing with
polymorphism here (which works in tandem with dynamic binding to produce
one of the most powerful facilities of an OO language), but with function
overloading, a rather different (and not as powerful) beast. (Just FYI -
hope this helps.)

You missed something. Objects s and l are Strings and ArrayLists in one
method and are accessed through variables of type Object in another. This is
definitely polymorphism.
 
B

Ben Wilson

Ben Wilson said:
Your code will never work because you are asking the compiler to implicitly
convert an Object to, alternatively, a String or a Collection. The compiler
will not do this for you for the class Object extends neither String nor
Collection.

Pardon, Collection is an interface of course, so the last sentence should
have read "The compiler will not do this for you for the class Object
neither extends String nor implements Collection".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,985
Messages
2,570,199
Members
46,766
Latest member
rignpype

Latest Threads

Top