LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help form Java coders please :) (https://www.linuxquestions.org/questions/programming-9/need-help-form-java-coders-please-607374/)

Xodiac 12-17-2007 11:26 AM

Need help form Java coders please :)
 
Quote:

Consider the following variable declarations.
Frog bill;
HoverFrog ben;
Now consider the following assignment statements.
bill = new HoverFrog();
//statement 1
ben = new Frog();
//statement 2
Which statement would cause an error? Explain your answer.

To me, the would both cause an error because they both seem to contradict eachother...but I don't know if I'm correct or not.

Any ideas? :)
Thanks

Nylex 12-17-2007 11:59 AM

You probably need to think about inheritance, but it's kinda hard to say without more details about the classes.

peart 12-17-2007 12:45 PM

Assuming HoverFrog is a specialization of Frog, I'd say #2 is an illegal assignment. But why do you ask?

95se 12-17-2007 12:50 PM

You can think about it like this; HoverFrog is always a Frog, but a Frog is not neccessarily always a HoverFrog. Whoever gave you this hw assignment should've used Toad and HypnoToad :D

Xodiac 12-17-2007 12:53 PM

It's one of many questions on my assignment for university which was due in on the 6th of december lol.

Still can't figure out the right answer.

The information I've given you is the only information I've been given myself.

Alien_Hominid 12-22-2007 04:43 AM

What can't you figure out?

All classes (e.g. String) are subclasses Object class => they are Objects. But not each Object is String. You can treat String as Object, but not vice versa. So hoverfrog is frog, but not all frogs are hoverfrogs. I hope you'll understand.

Looking_Lost 12-22-2007 05:19 AM

maybe help,

A boy can become a man, but a man cannot become a boy (at least not literally :) )

boy=new Man();
man=new Boy();

jay73 12-22-2007 05:33 AM

Without seeing the the class declaration for HoverFrog, I would say that they could both produce an error. If HoverFrog does not "extend" Frog, they would.


All times are GMT -5. The time now is 10:37 PM.