Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

casting to a derived class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class base {
}

class derived extends base {
}

class DownCast {
public static void main(String[] args){
derived d = (derived)new base();//1
}
}



Hi,
Why doesn't this give a compile-time error. But throws a ClassCastException during runtime.
(If 'derived' class were not to extend 'base' class, then this would have resulted in compile-time error - inconvertible types error)

Can anyone explain this.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

sarkar


Welocme To JavaRanch

The compiler checks where either side of the = operator has a relation or not,and wheather the right side reference can be assignable to the left of the = operator or not.

Means in your case

is there any relation between derived and base or not.If there is no relation then the compiler gives compile time error

EX:

String s="anil";
StringBuffer sb=new StringBuffer("Kumar");
sb=(String Buffer)s;



Thanks

Anil Kumar

[ June 14, 2007: Message edited by: anil kumar ]

[ June 14, 2007: Message edited by: anil kumar ]
[ June 14, 2007: Message edited by: anil kumar ]
 
vineeth sarkar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

In your example, we cant convert/cast String to StringBuffer. But it a compile -time error anyway.

Now, we know that we cannot assign the base class object to derived class reference. But, in my example, the code wasnt giving compile-time error. It was throwing run-time ClassCastException.

Consider this,

class base {
}

class derived extends base{
}

class DownCast {
public static void main(String[] args){
derived d = new base();//1
}
}


This gives a compile-time error.


class base {
}

class derived {
}

class DownCast {
public static void main(String[] args){
derived d = (derived)new base();//1
}
}

This gives compile-time error again. Can you explain the differnece?
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

OK

----------------------------------------------------------------------
class base {
}

class derived extends base{
}

class DownCast {
public static void main(String[] args) {
derived d = new base(); //1
}
}


This gives a compile-time error.
----------------------------------------------------------------------------
See Sarkar

Remeber these points

1)When you are assigning a reference to a varaible.First Check wheather is there any relation ship between the two types [Relation means is-A]
2)And wheather the right hand side is assignable to the lefthand side or not.Means WE CAN'T ASSIGN A REFEENCE OF A PARENT CLASS Object TO A CHILD CLASS reference varable.
3)But if you type cast that parent refernce to a Child class the compiler won't say any thing.


Example:

1) Base b=new Derived()//No problem because we can assign a refernce
of child class to a parent.
2)Derived d=new Base() //Compile time error

3)Derived d=(Derived)new Base() //compiles ,but gives run time error
--------------------------------------------------------------------------
class base {
}

class derived {
}

class DownCast {
public static void main(String[] args) {
derived d = (derived)new base(); //1
}
}
---------------------------------------------------------------

In your second example the is-A relation fails

Compiler knows this,and it gives compile time error


Thanks

Anil Kumar
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarkar,

You have come with many scenarios; Things may go straightforward, see how:



These all error are because IS-A fails. OK?

Original question:


If you refer to K&B, it is written like in such a case Compiler relies on
us. In my words, it can't do except relying on us. The reason is, here IS-A
succeeds and you have done casting also.

The problem only comes when the runtime type mismatch occurs. See the
following two cases:
Base b1 = new Base();
Base b2 = new Derived();

#1: Runtime type of the b1 will be Base
#2: Runtime type of the b2 will be Derived

So if you do like:
Derived d1 = (Derived)b2; // No problem at runtime also
See the point #2, runtime type of the b2 will be Derived so there wont
be any problem to the JVM to assign Derived class object to Derived class
ref variable.

What compiler sees at:
1- IS-A testing
2- Cast is OK or not, if not explicit casting is required to make code
compile OK, as you did like:
Derived d1 = (Derived)b2;

What JVM looks at
1- Can ref variable of the leftside of = operator hold the object of the
right side of the =.


Thanks,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic