This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2023
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harrier h3 = (Harrier)d2;

The runtime will check the actually d2 reference object type.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime exception :roll:
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!