• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting in java  RSS feed

 
Priyavrat Kumar
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to call superclass run method without creating any other object, by using upcast or downcast.

Output should be - Generic run

Replace comment with some code,
But, no other objects should be created.

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you question correctly, then the answer is yes on the down cast, but only a maybe on the up cast:

Let say we have a class Mamal, a class Quadroped, and a class LongNose. If we make our Classes appropriate then we can say that if we have an Elephant class, then it has a LongNose, and it is a Quadroped, and it is a Mamal, so at any point in our code we can cast Elephant to any of it's ancestors and all will be well. Because we have an "is a" type of relationship with each ancestor. But now let's say we have an object that is instantiated as a LongNose, we cannot expect it to have any distinct Elephant properties--those outside of the LongNose and it's ancestors. So the problem becomes your definition of up casting and down casting. Can I put an Elephant in a collection that stores Mamals--yes, and when I bring that object back out, can I cast it to Quadroped, or LongNose, or Elephant--yes, I can.

So like I said it depends on your definition of up casting and down casting... or perhaps more appropriately: what did you start with.


Priyavrat Kumar wrote:Is it possible to call superclass run method without creating any other object, by using upcast or downcast.

Output should be - Generic run
But, no other objects should be created.

 
Priyavrat Kumar
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you replace comment with some code that can superclass run method(Only new object creation is not allowed)?

Les Morgan wrote:If I understand you question correctly, then the answer is yes on the down cast, but only a maybe on the up cast:

Let say we have a class Mamal, a class Quadroped, and a class LongNose. If we make our Classes appropriate then we can say that if we have an Elephant class, then it has a LongNose, and it is a Quadroped, and it is a Mamal, so at any point in our code we can cast Elephant to any of it's ancestors and all will be well. Because we have an "is a" type of relationship with each ancestor. But now let's say we have an object that is instantiated as a LongNose, we cannot expect it to have any distinct Elephant properties--those outside of the LongNose and it's ancestors. So the problem becomes your definition of up casting and down casting. Can I put an Elephant in a collection that stores Mamals--yes, and when I bring that object back out, can I cast it to Quadroped, or LongNose, or Elephant--yes, I can.

So like I said it depends on your definition of up casting and down casting... or perhaps more appropriately: what did you start with.


Priyavrat Kumar wrote:Is it possible to call superclass run method without creating any other object, by using upcast or downcast.

Output should be - Generic run
But, no other objects should be created.

 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of two alternatives if the constraint is only about using the dog's instance created at line 11-



 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyavrat Kumar wrote:Is it possible to call superclass run method without creating any other object, by using upcast or downcast.

I hate to disagree with my colleague here; but based on what you've shown us, the answer is 'no'.

Let me clarify: There is no way to get a.run() to run Animal's run() method simply by casting, because 'a' (at least in the case you've shown us) is actually a Dog - even though you've declared it as an Animal.

If you know any C++, the way it was explained to me was: "All Java instance methods are 'virtual' by default".
Now that's not exactly correct (and Java doesn't have a 'virtual' keyword), but it's good enough to explain it for newbies who are coming to Java from C++.

Specifically, if a method is overridden, then the copy of the method that gets run is determined by the object's runtime type - which in this case is Dog - and no amount of casting can change that.


PS: When you override methods, get into the habit of using the '@Override' annotation, viz:First: It acts as a good reminder for you when you're reading your code.
Second: It can prevent many common mistakes, such as this one:the above code will compile just fine, but if you add '@Override', it will give you an error. I'll leave you to work out why.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!