• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

downcasting or upcasting of ref variable

 
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i want to call the Animal class met(), what to add in the code given below?I want to do it using only either by Downcasting or Upcasting of reference variable..... please reply....
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags when you post a code snap. Use the Edit button and insert the code tags. It'll be more readable!
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot call the meth method of Animal class on an instance of Doc class. Since Dog class' meth method overrides the meth method of Animal class, you'll have to create an instance of Animal class to call the method...
 
Shanu Pandey
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??
 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??



hi Shanu Pandey

it is not possible ...
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??



Why? If you want to use the Animal definition, why did you override it? If you want to use both, then you made a poor design, because then clearly you need two different methods.
 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??



it is not possible. super class method override by your sub class method so super class method is not available in your sub class object. if your method names or different it is available in sub class.

please correct if am wrong...
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanu Pandey wrote:if i want to call the Animal class met(), what to add in the code given below?I want to do it using only either by Downcasting or Upcasting of reference variable..... please reply....




Why don't you give it a TRY ? Put in coding what you want to do and test it, let us know what do you think and we'll follow up from there.

Hints: Yes, it's possible for either way.

Tips: Reread K & B book on page 154 under "Reference Variable Casting (Objective 5.2)" topic. It will clear your doubt...if not, come up with some questions of doubt that you might have.








 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naveen Madarapu wrote:

Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??



it is not possible. super class method override by your sub class method so super class method is not available in your sub class object. if your method names or different it is available in sub class.

please correct if am wrong...



It's POSSIBLE! See my response post above...
 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not possible just by upcasting or downcasting of reference variable d.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashanth Patha wrote:Its not possible just by upcasting or downcasting of reference variable d.



Are you sure and know what you are talking about??

Why don't CONFIRM or DIALOGUE with codes, and do thoroughly research and testing instead of saying out loud without a second thought??

I've done the coding and testing to PROVE it POSSIBLE, but just waiting what others response result before I can follow up.

Reread K & B on page 154 under "Reference Variable Casting (Objective 5.2)" topic, do some coding and test. Post your result on this thread so, we all can can see.

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's interesting, Tommy. Why don't you provide us with a complete working example that demonstrates calling both the superclass method and the method overridden by the subclass, using only one object and without resorting to reflective code.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's interesting, Tommy. Why don't you provide us with a complete working example that demonstrates calling both the superclass method and the method overridden by the subclass, using only one object and without resorting to reflective code.



Racape from Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??

Based on the question I think Shanu doesn't understand the concept, "using Animal a= new Animal() & a.meth()" you can "invoke the Animal class meth() by using Dog reference" upcasting by UPCAST Dog reference to Animal and from Animal reference you can call meth() method of Animal.

If you upcast Dog reference to Animal, you'll calling meth() method of Animal.

If you downcast Animal reference on a Dog instance you'll calling meth() method of Dog.

In summary, you can only call meth() method of Animal or Dog based on the reference type in using upcast or downcast. You can not calling meth() method on Animal reference using same or one object simply it make NO SENSE.

If you downcast to Dog so, you have to use Dog's reference, and Dog reference is calling Dog's meth() method, not Animal meth() method. In other word, why would you want to do that at FIRST place since you have an override meth() method in Dog class.



Stephan van Hulst wrote:
"calling both the superclass method and the method overridden by the subclass, using only one object and without resorting to reflective code."



I'm not sure what you're asking. The rule is set so, you can't do anything about it and if you do something about it, it making NO SENSE.


If you don't have an override meth() method in a Dog class you CAN call meth() method in Animal class using one Animal Reference (i.e animal). You can upcast or downcast as you like, the only method it will invoke is meth() method in Animal class.



I'll follow up with codes once others have tested and tried....



 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tommy Delson wrote:If you upcast Dog reference to Animal, you'll calling meth() method of Animal.


No. You can't have a Dog reference to an Animal, because this will result in a ClassCastException. If you have a Dog reference to a Dog, and you upcast it to Animal, you will be calling the meth() method of Dog regardless. This is called polymorphism.

I think you are misunderstanding the original question. The poster wants to perform Animal's meth() method, using an instance of Dog. This is not possible, because it is overridden by Dog.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Tommy Delson wrote:If you upcast Dog reference to Animal, you'll calling meth() method of Animal.


No. You can't have a Dog reference to an Animal, because this will result in a ClassCastException. If you have a Dog reference to a Dog, and you upcast it to Animal, you will be calling the meth() method of Dog regardless. This is called polymorphism.

I think you are misunderstanding the original question. The poster wants to perform Animal's meth() method, using an instance of Dog. This is not possible, because it is overridden by Dog.




"If you upcast Dog reference to Animal, you'll calling meth() method of Animal." My bad that I didn't stated clearly that if you don't have meth() method in the Dog's class. In this scenario, Animal's meth() method will invoke.


No. You can't have a Dog reference to an Animal, because this will result in a ClassCastException. If you have a Dog reference to a Dog, and you upcast it to Animal, you will be calling the meth() method of Dog regardless. This is called polymorphism.




NO ClassCastException in this case, and you're right "you upcast it to Animal, you will be calling the meth() method of Dog regardless". See a piece of code below that I've tested before, and confirmed that it calling Dog's meth() method.

 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tommy Delson wrote:

Naveen Madarapu wrote:

Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??



it is not possible. super class method override by your sub class method so super class method is not available in your sub class object. if your method names or different it is available in sub class.

please correct if am wrong...



It's POSSIBLE! See my response post above...




i think you are misunderstand the question shanu pandey asking about to invoke the Animal class meth() by using Dog reference (either upcasting or downcasting the dog reference).

in your code where your invoke the Animal class meth() method.
take a look about this code.


 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naveen Madarapu wrote:

i think you are misunderstand the question shanu pandey asking about to invoke the Animal class meth() by using Dog reference (either upcasting or downcasting the dog reference).

in your code where your invoke the Animal class meth() method.
take a look about this code.




Did you read thoroughly my responses to Stephan Van?

I didn't state clearly, and here is the recap " "If you upcast Dog reference to Animal, you'll calling meth() method of Animal." My bad that I didn't stated clearly that if you don't have meth() method in the Dog's class. In this scenario, Animal's meth() method will invoke. "


 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tommy Delson wrote:
Did you read thoroughly my responses to Stephan Van?

I didn't state clearly, and here is the recap " "If you upcast Dog reference to Animal, you'll calling meth() method of Animal." My bad that I didn't stated clearly that if you don't have meth() method in the Dog's class. In this scenario, Animal's meth() method will invoke. "



Sorry Tommy but you're still wrong - presumably because you still don't understand the discussion in this thread?
Given the original (OPs) code sample you cannot get the behaviour he requested through up/downcasting.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pauly bradley wrote:

Tommy Delson wrote:
Did you read thoroughly my responses to Stephan Van?

I didn't state clearly, and here is the recap " "If you upcast Dog reference to Animal, you'll calling meth() method of Animal." My bad that I didn't stated clearly that if you don't have meth() method in the Dog's class. In this scenario, Animal's meth() method will invoke. "



Sorry Tommy but you're still wrong - presumably because you still don't understand the discussion in this thread?
Given the original (OPs) code sample you cannot get the behaviour he requested through up/downcasting.



Have you thoroughly read everything in this thread and understood the question asking? I think you're not simply you didn't get the last part on my response to Stephan Van.

I've stated that "In summary, you can only call meth() method of Animal or Dog based on the reference type in using upcast or downcast. You can not calling meth() method on Animal reference using same or one object simply it make NO SENSE. "

The POINT is, why would they WANT to that at FIRST place? Again, it making NO SENSE and in contrary with the rule.

Now tell me where do you see I don't understand the discussion in this topic ?? I've acknowledged that the behavior requested originally through up/down cast only works when Dog or subclass doesn't have or override meth() method. The codes I have provided also proved that and I've make it clearly on the response.


Recap from original post "if i want to call the Animal class met(), what to add in the code given below?I want to do it using only either by Downcasting or Upcasting of reference variable.....".

Do you see the word "either" ?? "Either" means one and "or" also means one or the other, NOT both.

Now another question from Shanu:

Shanu Pandey wrote:yeah.I know that i can do it by using Animal a= new Animal() & a.meth() .But i want to invoke the Animal class meth() by using Dog reference and upcasting or downcasting.is it possible or not??if yes how??


And on my LAST response to Stephan Van what did I say??? I wouldn't repeat it again and let you read through. Tell me who not understand??

I won't blame you if you didn't have a chance to read all the response in this topic....you're not alone, it happen other time and I'm not an exception here.





 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets keep things calm

The original question is about a case where Dog class' method overrides Animal class' method in which case its not possible to call the overridden method of Animal class using an instance of Dog class. What Tommy is saying is a different case where what he said is true, if you don't override the method in Dog class the method of Animal class would be called. Period...
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tommy Delson wrote:

Prashanth Patha wrote:Its not possible just by upcasting or downcasting of reference variable d.



Are you sure and know what you are talking about??

Why don't CONFIRM or DIALOGUE with codes, and do thoroughly research and testing instead of saying out loud without a second thought??

I've done the coding and testing to PROVE it POSSIBLE, but just waiting what others response result before I can follow up.

Reread K & B on page 154 under "Reference Variable Casting (Objective 5.2)" topic, do some coding and test. Post your result on this thread so, we all can can see.

Hi Tommy Delson,
why are you so hasty ... For the question given by Shanu Pandey , i replied the answer in a straight forward way. you said ,"Why don't CONFIRM or DIALOGUE with codes, and do thoroughly research and testing instead of saying out loud without a second thought??

I've done the coding and testing to PROVE it POSSIBLE, but just waiting what others response result before I can follow up. "

its not correct attitude... Once again see the question by Shanu Pandey and my straight forward answer, if you found anything wrong.. i will agree with your words...
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool down guys, don't take it seriously and take it professionally. We all try to help here, if any word that I said that made anyone feel uncomfortable please ignore it.

My POINT here is I tried to convey the explanation in details with coding so, those who ask question able to understand easily instead of straight answer YES or NO.

YES or NO answer doesn't help anyone understand the problem or solution so, be more details and intuitive. I'm sure Shanu Pandey will greatly appreciate with a details answer whether it possible or not, that just help one understand more about it. If that was me I too appreciate that.

Recap:
"Prashanth Patha wrote:Its not possible just by upcasting or downcasting of reference variable d."


This is the POINT why I said it's POSSIBLE based on "upcasting or downcasting of reference variable d". I've tested and proved it in coding upcast/downcast is possible for calling meth() method in both Animal & Dog class.

There are 2 cases in this scenario, one with an override and one with NO override meth() method in Dog class. What I'm trying to coney is a possibility of up/down cast reference variable "d".

Hope that clear up the confusion...

Take it easy....
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tommy Delson wrote: Cool down guys, don't take it seriously and take it professionally. We all try to help here, if any word that I said that made anyone feel uncomfortable please ignore it.

My POINT here is I tried to convey the explanation in details with coding so, those who ask question able to understand easily instead of straight answer YES or NO.

YES or NO answer doesn't help anyone understand the problem or solution so, be more details and intuitive. I'm sure Shanu Pandey will greatly appreciate with a details answer whether it possible or not, that just help one understand more about it. If that was me I too appreciate that.

Recap:
"Prashanth Patha wrote:Its not possible just by upcasting or downcasting of reference variable d."


This is the POINT why I said it's POSSIBLE based on "upcasting or downcasting of reference variable d". I've tested and proved it in coding upcast/downcast is possible for calling meth() method in both Animal & Dog class.

There are 2 cases in this scenario, one with an override and one with NO override meth() method in Dog class. What I'm trying to coney is a possibility of up/down cast reference variable "d".

Hope that clear up the confusion...

Take it easy....

Hi Tommy Delson,
Cool..and you also take it easy, next time when i post the answer, i will follow your advice to give explanation with YES or NO.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a friendly advice. We advice people to KeepItDown here at ranch. When I say 'you did it' and when I say 'YOU did it' that makes a big difference. Using upper case gives a feel that you are being rude...
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic