aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes downcasting or upcasting of ref variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "downcasting or upcasting of ref variable" Watch "downcasting or upcasting of ref variable" New topic
Author

downcasting or upcasting of ref variable

Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

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....
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

UseCodeTags when you post a code snap. Use the Edit button and insert the code tags. It'll be more readable!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

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??
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

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 ...


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

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...


OCPJP
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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.









OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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...
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

Its not possible just by upcasting or downcasting of reference variable d.


Thanks & Regards
Prashanth

Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Apr 13, 2011
Posts: 206
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
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Apr 13, 2011
Posts: 206
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

Joined: May 24, 2011
Posts: 64

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

Joined: Apr 13, 2011
Posts: 206
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. "


pauly bradley
Greenhorn

Joined: May 17, 2011
Posts: 8
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

Joined: Apr 13, 2011
Posts: 206
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: May 06, 2011
Posts: 62

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

Joined: Apr 13, 2011
Posts: 206
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

Joined: May 06, 2011
Posts: 62

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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: downcasting or upcasting of ref variable