For starters, main is not a method that you put into all classes you create. I would remove it from class junk and junksub altogether. Then, you can't make class junk a junksub. All instances of junksub are junk, but not all instances of junk are junksub. Does that make sense?
That's right, you can upcast no problem, but not downcast (class-wise). Also, I beleive that whatever class you decide to keep your one main() method in has to be declared as public. Someone correct me if I am wrong. ------------------
I still do not understand why am I getting an error when I do an explicit cast down. Eventhough I have the main declared in each class it is not causing the casting error. Thank you,
Charlie: You cannot force an object of junk()to be an object of junksub() even with an explicit cast. For example: Create a class Animal Create class Dog extends Animal You cannot forcibly make any Animal a Dog by explicit casting But a Dog is naturally an Animal (defined in the class hierarchy) I rewrote your code using the example
Hello, I am confused on this. From my books, I know that an explicit cast is required when going from a superclass to a subclass. Can anyone explain how or when this can be done?
I suggest that you read "How my dog learned Polymorphism" in the Campfire section of the JavaRanch. It discusses these concepts really well (at least it really helped me).
[This message has been edited by Kathy Lynch (edited April 06, 2001).]