• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about Polymorphism and casting.  RSS feed

 
ben istaharov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

Assume we have
a parent class SimpleTurtle
and DrunkTurtle , JumpyTurtle extends SimpleTurtle.

I do this:
SimpleTurtle[] army = new SimpleTurtle[5];
army[1] = new DrunkTurtle();

now, the question is:

what happens when I do this:
army[1].______

why here (At the line i paint) i can only see the methods of simpleTurtle and not also the methods of the drunk one?

what actually happend? we go to check the method on simpleturtle and after that check at the drunkturtle, and if the method exist in the drunk one then it ovveride?

what about casting ? why it help? how does it work?

thanks,
Ben



 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Array reference points to a parent class which is probably why you see only the methods defined in the parent class even though your first Array element has an object type of a sub class!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben istaharov wrote:why here (At the line i paint) i can only see the methods of simpleTurtle and not also the methods of the drunk one?

Because army[1] is defined as a SimpleTurtle.

Yes, it's possible that the compiler could "remember" that it was actually set to a DrunkTurtle - especially in your specific case, where you're using constants (literals) for your indexes as opposed to variables - but there are many other cases where it can't, so I suspect the designers opted for consistency.

what actually happend? we go to check the method on simpleturtle and after that check at the drunkturtle, and if the method exist in the drunk one then it ovveride?

If you're asking how an overridden method is called, then - yes, that's basically how it works. If SimpleTurtle defines a method that is overridden in DrunkTurtle, then army[1] can "see" the method (because it's defined in both classes), but when the method is actually called, the version used will be the one defined by the object - which is, in fact, a DrunkTurtle.

And that's basically how polymorphism works.

what about casting ? why it help? how does it work?

Simple answer: it doesn't - or, more accurately: it doesn't very often. Especially when you start to learn about generics.

When you cast, what you are saying to the compiler is: "I know you think this is only a SimpleTurtle, but I happen to KNOW that it's actually a DrunkTurtle, so I'm telling you that's what it is".

And if you get it wrong, you'll get a ClassCastException.

Does that answer your question(s)?

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
Yes, it's possible that the compiler could "remember" that it was actually set to a DrunkTurtle - . . .
Possible, yes. Going to, no.

As you say, there are some instances where it would be possible to remember the actual runtime type of an array element, but in other cases it would be impossible. So why spend hours (weeks?) trying to program a compiler to remember such details, particularly if they are part of the runtime? And remembering such details will slow the compiling process without necessarily adding correctness to the program.
As you said, the correct approach is to use polymorphism. I agree with you that you should avoid casts as much as possible (they are far worse on reference types than primitives, and I think it is unfortunate that the same word “cast” is used for both processes).

As a design question, ben istaharov: why are you adding methods to your subclasses? If you have all the drunk methods in the simple class, then they can be overridden. Your simple class may end up with methods which do nothing, but they do something in their overridden versions.
 
ben istaharov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thank you all for your comments.
Campbell Ritchie: i have some methods in the sub class that are not in the parent class. The only way to call them is by doing casting? By the way i cant change or add anything to the parent class (home work request).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben istaharov wrote:First, thank you all for your comments.
Campbell Ritchie: i have some methods in the sub class that are not in the parent class. The only way to call them is by doing casting? By the way i cant change or add anything to the parent class (home work request).

But presumably you are allowed to create an interface (RealTurtle?) that defines ALL the methods that any of your subtypes might want to implement.

Then all you need to do is add 'implements RealTurtle' to your subclasses and change your array to a RealTurtle[], and it will work polymorphically.

However, it's possible that they want you to understand how casting works - and if that's the case, then you'll need to read up on the instanceof operator.

Just a little taster for you:but it's not exactly "object-oriented".

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really do have to call methods which are in the subclass and not in the superclass, yes you will have to cast. You get horrible code like this:-But I still think such methods in subclasses only are not good object‑orientation. It is verging on treating a DrunkTurtle AS IF IT WERE NOT A SimpleTurtle.

Note the positions of the (( and )) for the cast.
 
ben istaharov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.K , I see.
I'll try to ask our Lecturer if its o.k to create an interface.
I'm sure didn't think of that option till you said it.
thanks for your solutions and answers its helped me a lot.
Have a Good day !!
Ben
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben istaharov wrote:thanks for your solutions and answers its helped me a lot.

Glad to hear.

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
ben istaharov wrote:thanks for your solutions and answers its helped me a lot.

Glad to hear. . . .
Same here

You will probably find that an interface necessitates changing the simple turtle class, however. One has to be careful when setting questions not to be too prescriptive, otherwise there is a risk of getting a not‑quite‑object‑oriented answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!