• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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 !
 
What are you doing in my house? Get 'em tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic