aspose file tools*
The moose likes Beginning Java and the fly likes a theoretical question in casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a theoretical question in casting" Watch "a theoretical question in casting" New topic
Author

a theoretical question in casting

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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

Joined: Aug 05, 2005
Posts: 3576
    
  15
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.


Joanne
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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

Joined: Jul 26, 2007
Posts: 36

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

Joined: Feb 01, 2008
Posts: 191
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
Sheriff

Joined: Sep 28, 2004
Posts: 18849
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a theoretical question in casting