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
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?
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 !