• 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

A Question from TIJ

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look into this question !
Create an abstract class with no methods. Derive a class and add a method. Create a static method that takes a reference to the base class, downcasts it to the derived class, and calls the method. In main( ), demonstrate that it works. Now put the abstract declaration for the method in the base class, thus eliminating the need for the downcast.
Could somebody help me with this ?
Thanks,
Jenny
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IT took me a while before I managed to figure out the problem. What the question is basically trying to prove is that when you have a subclass that is referenced by a base class reference, you must downcast, *unless* that method exists in the baseclass, and then Java will downcast for you.
Here is what I came up with:
In Grandfather.java:
In Father.java:
And in Driver.java:
If you save and compile each of these files, and then run Driver, you will see '1' output on the command line.
If you then uncomment the single method declaration in Grandfather, and switch the System.out.println's in Driver, and recompile everything, you will also see '1'.
Does that help?
 
Jenny Wallace
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the solution !
My error :
I was getting stuck here at this point where I tried putting (let me use ur program's variables )
grand = new GrandFather();
I cudn't proceed further as GrandFather was abstract and I cudn't instantiate it . But if I tried removing that , I was just left with a reference "grand" and didn't know how to use it.
Understanding :
So Intially an upcast is necessary ie
grand = new Father();
Only then later, a downcast could be done !

I hope I got it right !
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now try it with Grandfather as an interface and see how useful interfaces can be !
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic