aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment Question" Watch "Assignment Question" New topic
Author

Assignment Question

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
1. Class B extends A
2. B b1 = new B()
3. A a1 = new A()
4. a1 = b1
5. b1 = (B)a1
since declared type a1 "is a" A assigned an actual type of B the assignment on line 5 will compile and run, w/ cast always necessary.
I want to be absolutely sure that this is correct (I have conflicting notes), so that I can close this topic.
Thanks
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
you got it Paul
if you try to compile and run your code, you'd definitely get the answer to your question
If you remove the cast to B you will not be able to compile the code.
[ February 08, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
This is the perfect thing to try with your compiler!!


Rob
SCJP 1.4
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
I appreciate it guys, you all must think alike
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
at line 5 the cast takes care of compiling, but if you run it will give you a runtime error ?

Originally posted by Paul Salerno:
1. Class B extends A
2. B b1 = new B()
3. A a1 = new A()
4. a1 = b1
5. b1 = (B)a1
since declared type a1 "is a" A assigned an actual type of B the assignment on line 5 will compile and run, w/ cast always necessary.
I want to be absolutely sure that this is correct (I have conflicting notes), so that I can close this topic.
Thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by mark stone:
at line 5 the cast takes care of compiling, but if you run it will give you a runtime error ?


In this case, it won't because the variable a1 contains a reference to a valid B object when the assignment b1 = (B) a1; is done. If, however, the variable a1 referenced an object of type A, the compiler would allow this but it would throw a ClassCastException at runtime.
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Paul,
since you haven't installed the JDK yet, you can go to the following link http://www.jdiscuss.com and click on the "Web compiler" link at the top of the window. That's a web compiler which allows you to test some code from within your browser. Be aware that it only works in IE browsers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment Question