Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a theoretical question in casting

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are two classes
the one called quiz and it defined as the father
it has a method called "num"

the other class called quiz1 defined as the son
it has the same method called "num"
and it has a method called "num1"

so i guess that the signature of quiz is

public class quiz1 extends quiz

we have a variable which is defined as:

quiz T=new quiz(); (or some thing like that

the question is:
which line will not create a bug
1.

System.out.println(T.num());
2.
System.out.println((quiz1)T.num());

regarding the second line i know that in this case we look
at quiz1 and check if it has num()
if it has then we use the "num" method of quiz
i was tald that this line compiles but later we have
a runtime error

regarding the first line i think that we will have a bug here
i dont know which type but
i know that both in the father and in the son we have this method
so which method we choose??

[ February 18, 2008: Message edited by: donaldth smithts ]
[ February 18, 2008: Message edited by: donaldth smithts ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put some print statements in your num methods and see which ones get called. If you don't understand the results, post them on here and explain what you thought would happen.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
regarding the first line i understood that the sons methos is always being
chosen over the fathers

regarding the second line:
System.out.println((quiz1)t.num());

iget a weird bug right in the start that doesnt alowing me to test the second line
it says "cannot cast from String to quiz1"
how do i fix it??
so i can learn this father son casting thing
 
Chris Waguespack
Ranch Hand
Posts: 36
Eclipse IDE Flex Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to call because the order of operations says that member access occurs before casting. Right now it is trying to take t.num() (which is of type String) and convert it into something of type quiz1.

-Chris
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i got my exeption error
"class cast exeption"

so can you tell me what is the problem with what i did??

in similar cases i was told that the father looks in the son
if he got this function and if he got then it looks inside of himself
and if he finds the method inside himself then he executes it
if there is no such method in the father he executes the method in
the son.

so whats the problem??
what are the rules in such cases
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donaldth smithts:
well i got my exeption error
"class cast exeption"

so can you tell me what is the problem with what i did??


The cast in this code...



Only allows the code to compile. The object referenced by "t" is *not* a quiz1 object, so at runtime, you will get a class cast exception.

so whats the problem??
what are the rules in such cases


In order to cast an object to another type, the object must be able to be that type. For your case, quiz1 instances are also quiz instances, but quiz instances are not always quiz1 instances -- which in this example run, is the case.

Henry
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic