• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I overload a constructor with enum values?  RSS feed

 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say



Can I make a class with overloaded constructors depending on wich one of the values I send?
I tried but ran into some problems,

So can I

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that's possible.

Can you make use of polymorphism instead? In which way should the constructors work differently?
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have one constructur that uses instanceof and/or a switch statement to call other constructor-like methods.
 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem I ran into is that I can't send a type.DVD, but I can send the whole enum.

Well I guess instanceOf() will do. Didnt think of it
Will try tommorow its already 1AM here


The way I intended is to call a different method depending on what is sent.

Thanks!
[ December 23, 2005: Message edited by: Branko Santo ]
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is not a method.
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not even inherited.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with instanceof (wouldn't a switch be more appropriate?) is that it easily becomes a maintenance nightmare when you have to add more cases to the enum later.

That's why I suggested to use polymorphism. If you can't/don't want to move the behaviour that is differing based on the enum into the enum itself, consider using the Visitor pattern.
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why, Ilja Preuss, do you recommend the Visitor pattern in this case?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
A constructor is not a method.

It's not even inherited.


So what?
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja Preuss

Glad you noticed.

A constructor cannot be inheriten and it cannot be overidden.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
Ilja Preuss

Glad you noticed.

A constructor cannot be inheriten and it cannot be overidden.


Did you notice that the question was about over*loading*?
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, sure I noticed, but I tried to read him right.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Branko,
The reply is probably too late but...

Remember that each values are in fact classes.
You can actually define custom actions for each enum values.
(this is called "constant specific class body")

For example :


With a variable Type myType, calling myType.sayHello() would return "HELLO" for any types, except MP3 where it will return "Hello MP3".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
Why, Ilja Preuss, do you recommend the Visitor pattern in this case?


A java enum really is a class hierarchy, as Satou showed. So we can make use of polymorphism.

If we don't want to put that specific methods onto the enums themselves, we are in a position where we want to implement a polymorphic operation for a class hierarchy, without touching the hierarchy itself. That's exactly what Visitor is for.

Of course Java 5 (we are not yet using that) allows us to use the switch statement instead, but that one (as far as I know) won't give us a compile time error when we add a new enum and forget to add the case to the switch. (The Visitor pattern would give us a compile time error, because the visitor implementation needed to implement one more method.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!