File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Narrowing of reference type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Narrowing of reference type" Watch "Narrowing of reference type" New topic
Author

Narrowing of reference type

manu chandra
Greenhorn

Joined: Oct 08, 2012
Posts: 15

consider the following example:

public class A
{

public void m1()
{
System.out.println("class a");
}

}

public class B extends A
{
public static void main(String[] s)
{
A a=new B(); //This holds good.

B b=(B)new A(); //compile time no error but run time error;
}
}

My question is is there any use of doing a downcast like this just to avoid compile time error and to convert the code to bytecode?


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

No. All you are doing is postponing the error to runtime. You should (at great lengths) try to discover all errors at compile time, not try to put them off till runtime. So this is just bad.

Doing B b = (B)new A(); can never succeed because the A is not a B.


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

In my opinion there isn't any practical purpose to produce code which is guaranteed to crash when you run it.

There could be other purposes, though, for example if you weren't allowed to go home until your code compiled. Then you might want to produce bad code which compiles rather than good code which doesn't compile. However that situation isn't going to apply to made-up code with classes called A and B.
manu chandra
Greenhorn

Joined: Oct 08, 2012
Posts: 15
So practically it means nothing? It does not have any advantage right?
manu chandra
Greenhorn

Joined: Oct 08, 2012
Posts: 15
manu chandra wrote: So practically it does not have any advantage right?And as far as i think its a bad practice
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

I guess there are two public class in the same package. so it will give a compile time error.
and before casting it is better to check the with "InstanceOf" operator. which will give you a compile time safety.


OCPJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Ishan Pandya wrote:. . . check the with "InstanceOf" operator. which will give you a compile time safety.
At least two errors there, apart from the spelling error.
  • 1: We are not interested in compile time safety. We want as many errors as possible at compile time. The exception cannot occur at compile time anyway.
  • 2: All you achieve with the instanceof test, which cannot succeed, is obscuring the mistake in code which is never called. It is a version of if(false)... which the compiler cannot detect.
  • Ishan Pandya
    Ranch Hand

    Joined: Feb 06, 2012
    Posts: 223

    ok Ritchie I understand. So is there any way to get compile time error for "Runtime ClassCastException"? as in can we stop ClassCastException at compile time? as you said that we want as many errors at compile time.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39478
        
      28
    Generics is one method for moving runtime errors to compile time. But that may not be appropriate for this situation.
    The correct answer is to design your inheritance hierarchy correctly, so you can use polymorphism and not need those casts in the first place.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Narrowing of reference type