|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
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??
``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
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??
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??
OCPJP
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....
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
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...
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Thanks & Regards
Prashanth
Prashanth Patha wrote:Its not possible just by upcasting or downcasting of reference variable d.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
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.
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."
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Tommy Delson wrote:If you upcast Dog reference to Animal, you'll calling meth() method of Animal.
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.
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.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
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...
OCPJP
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.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
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. "
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.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Hi Tommy Delson,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.
Thanks & Regards
Prashanth
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Hi Tommy Delson,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....
Thanks & Regards
Prashanth
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
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
|