File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question #1 from Doug's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question #1 from Doug Watch "Question #1 from Doug New topic
Author

Question #1 from Doug's book

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Will this code compile? If it does, what happens when it runs?

[ August 07, 2003: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
Originally posted by Thomas Paul:
Will this code compile? If it does, what happens when it runs?

[ August 07, 2003: Message edited by: Thomas Paul ]

compiles but runtime exceptions
The code compiles because Test.s maybe actually refers to an object of Superclass's subclass which implements the Printable interface ...... that's why p = (Printable) Test.s; is legal. But actually the code here is not the case as supposed, that's why runtime error arises.
is this the case?


Meng Yi
Cody Beth
Greenhorn

Joined: May 20, 2002
Posts: 11
But if we change the Printable interface to Printable class ..then compile time error of inconvertible types occurs ..
class Test {
static Superclass s = new Superclass();
static Printable p;
public static void main(String[] args) {
p = (Printable) s;
}
}
class Superclass { }
class Printable {
void print() {};
}
So does this mean that Type casting in case of interfaces are checked at run time ..and in case of class , Type casting is checked at compile-time..
Arvind Varma
Ranch Hand

Joined: Dec 24, 2002
Posts: 48
The compiler objects or suggests to casting when the operands variables involve primitive types. In other cases it assumes that the programmer is aware of what he is doing. Therefore, the code in question, will pass the compiler but will throw a reuntime exception (ClassCastException) since Superclass does not implement Printable.

Casting a class 'C' to an interface 'I' is legal only when class 'C' or any of its superclasses implement 'I'.
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Hi Yi Meng

I cannot see implements clause anywhere in the code. So how did you make out that class Superclass implements Printable. I tried compiling the code and it compiled.
Infact i didn't understood how the code compiled coz as per the code the Interface Printable is not related to any class, so how did compiler parsed the statement
p = (Printable) Test.s;
successfully. Can we type cast any class to any interface ?
[ August 08, 2003: Message edited by: Manish Sachdev ]

Regards, Manish
SCJP 1.4
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Thomas Paul:
Will this code compile? If it does, what happens when it runs?



Yes, it would compile.
It is allowed to cast any class to an interface because the subclass of that class may implement that interface:

[ August 08, 2003: Message edited by: Alton Hernandez ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It compiles and gets a runtime exception.
What change can I make to this line: class Superclass { }
that will cause this line: p = (Printable) Test.s; to produce a compile error?
For more cool stuff like this: http://www.javarules.com
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Make it final ?
In that case, the compiler knows that you cannot subclass Superclass anymore.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I�d like to have a go at explaining this one, if I may.
p = (Printable) Test.s;
1. Compile-time
From a class type to an interface type is a narrowing conversion if the class does not implement the interface and the class is not final. (Some subclass might implement the interface.)
The type of Test.s is Superclass. The type of p is Printable. From Superclass to Printable is a narrowing conversion.
2. Cast conversion context
A cast conversion context allows a narrowing conversion and requires a run-time check.
3. Run-time
From a class type to an interface type is a widening conversion if the class implements the interface.
From Superclass to Printable is not a widening conversion, because Superclass does not implement Printable. Therefore, a ClassCastException is thrown.
[ August 08, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I really like this way of thinking about, analyzing and explaining conversions, even if no one else does.
1. Decide whether a conversion from one type to another is a widening or a narrowing conversion. (For example, subclass to superclass is a widening conversion, superclass to subclass is a narrowing conversion.) Anything else is surely a compiler-error.
2a. An assignment conversion context allows widening conversions, and also narrowing conversions of some compile-time constants.
2b. A cast conversion context allows both widening conversions and narrowing conversions with a run-time check.
2c. A method invocation conversion context allows only widening conversions.
This way of thinking will help you with conversions between classes and interfaces and conversions to and from array types. Of course, the key is knowing what are the widening and narrowing, primitive and reference conversions.
[ August 08, 2003: Message edited by: Marlene Miller ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Marlene, you would definitely like Doug's book. He thinks exactly the same way you do!
By the way, making the SuperClass final is the correct answer.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you to Thomas and Jim for the reading recommendation. Now, would you please add just one more hour to each day for reading.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

So does this mean that Type casting in case of interfaces are checked at run time ..and in case of class , Type casting is checked at compile-time.

Not at all. Both classes and intefaces are checked by the compiler and Java Virtual Machine as specifies JLS 5.5 Casting Coversion


SCJP2. Please Indent your code using UBB Code
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I missed the comment that Jose responded to. Jose is correct. Both interfaces and classes have compile time validation. The difference is that the possibilities are much wider for interfaces. But, they are not limitless. If it is impossible for a class reference to point to a class that implements the interface then the ast is not allowed at compile time.
For example:

Will give an error for inconvertible types.
However, this will compile:

The only difference is that String is final therefore there is no possibility of someone writing a class that extends String and implements the AudioClip interface. I could do that with Button though since Button is not final. Therefore the Button example compiles cleanly although it is an unsafe cast that will cause a runtime exception:
java.lang.ClassCastException at ABC.main(ABC.java:6)
Exception in thread "main"
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Marlene]: Thank you to Thomas and Jim for the reading recommendation. Now, would you please add just one more hour to each day for reading.

If only we could...
[ August 10, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question #1 from Doug's book