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 Is it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is it "Exception at runtime" or "Compile Fails"  for this one?" Watch "Is it "Exception at runtime" or "Compile Fails"  for this one?" New topic
Author

Is it "Exception at runtime" or "Compile Fails" for this one?

Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
From below, isn't it very clear to the compiler that parent can't be a child?!! So why isn't "Compilation Fails" the answer? It is confusing ..So does any of you guys ahave a rule of thumb about which to choose. I know experience helps but still I need a rule of thumb regarding how to answer such questions.

import java.io.*;

class Parent implements Serializable { }


class Child extends Parent implements Serializable, Runnable{public void run(){}; }

class ChildTest{
public static void main(String [] args){

Parent p=new Parent();

Child c=(Child) p;

}
}

What is the result?
1) Compilation Fails
2) Exception at runtime
[ May 24, 2006: Message edited by: Firas Zureikat ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The reason that compilation does not fail is because a cast to a subclass type is always allowed at compile-time.

Consider the equals method in Object. The parameter type is Object so any class's reference is promoted to an Object if that reference is sent to equals. You have to downcast it back to the original reference type in order to use it like you normally would.

At runtime a check is made to see if the cast is legitimate.
[ May 24, 2006: Message edited by: Keith Lynn ]
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
Ok, thanks...

For object casting purposes, all are see as generic objects at compile time so that's why this doesn't result in compile time. Right? Thanks
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
5.1.6 Narrowing Reference Conversions
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
What about?

10. class Dog { }
20. class Harrier extends Dog { }
30.
40. class DogTest {
50. public static void main(String [] args) {
60. Dog d1 = new Dog();
70. Harrier h1 = new Harrier();
80. Dog d2 = h1;
90. Harrier h2 = (Harrier) d2;
100. Harrier h3 = d2;
110. }
120. }

What the answer?

A) Compilation fails
B) An Exception at runtime

Why is the answer A. We just agreed that these sor of problems are "Exception at Runtime" is the answer, right?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Harrier h3 = (Harrier)d2;

The runtime will check the actually d2 reference object type.
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Firas-

The reason that your second example won't compile is that an assignment is being made (Harrier h3 = d2 ;) that is not legal. Futhermore, it could never be legal without a cast. The compiler knows this and complains.

When there's a cast there's a chance that the assignment is legal and so the copiler doesn't complain and lets it get resolved at runtime. There's a caveat to this. If there's a cast and that cast could never succeed, the compiler will complain. For instance, if you put a cast in front of d2 that cast it to a Truck, the compiler would complain.

So in this context, it looks like you have three options:

1) Assignments that could never be legal - Fail at compile time.


2) Assignments with casts that could never be legal - Fail at compile time.


3) Assignments with casts that might be legal - Resolved at runtime


I hope that helps,
Joshua Smith

[ May 26, 2006: Message edited by: Joshua Smith ]

[ May 26, 2006: Message edited by: Joshua Smith ]
[ May 26, 2006: Message edited by: Joshua Smith ]

Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
Ok, thanks...

Casting could still result in Compilation fails then.....So it's not always tue that casting might be seen by the compiler as a reason to let the runtime machine handle it. The comiler also checks for casting at times like the example you gave above.

Thanks
Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
Runtime exception :roll:


SCJP 5.0<br />SCWCD 1.4<br />SCBCD 1.3<br />SCEA part 1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it "Exception at runtime" or "Compile Fails" for this one?