*
The moose likes Beginning Java and the fly likes creating objecct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating objecct" Watch "creating objecct" New topic
Author

creating objecct

Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

PLEASE PROVIDE REASON ABOUT THE ERROR



Why do we fall? So that we can learn to pick ourselves up.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
What is the error? And please KeepItDown
changu mani
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
During compile time, compiler is not aware of what kind of object ob is pointing to. So it is necessary to do external casting.
Do this way, the compiler error goes away.

Sub ob1= (Sub)ob;
Abc ob2=(Abc)ob;
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

changu mani wrote:During compile time, compiler is not aware of what kind of object ob is pointing to. So it is necessary to do external casting.
Do this way, the compiler error goes away.

Sub ob1= (Sub)ob;
Abc ob2=(Abc)ob;


The first line is legal. The second will give ClassCastException at runtime.
Niranjan Aithal
Greenhorn

Joined: Mar 17, 2012
Posts: 6
changu mani wrote:During compile time, compiler is not aware of what kind of object ob is pointing to. So it is necessary to do external casting.
Do this way, the compiler error goes away.

Sub ob1= (Sub)ob;
Abc ob2=(Abc)ob;


Looks like you read: public class Sub extends Abc
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating objecct