wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please help me" Watch "Please help me" New topic
Author

Please help me

Yojana
Greenhorn

Joined: Jun 12, 2000
Posts: 19
I am getting one problem.Please help me.
Class y is a subclass of class x. Will this compile?
x myX = new x();
y myY = (y)myx;
I got in the round up game like this .It will compile but runtime exception it will thrown.
Can you explain me what it is?


[This message has been edited by Yojana (edited June 15, 2000).]
Sarang Khankhojr
Greenhorn

Joined: Jan 31, 2005
Posts: 17
Yojana,
________________________________________________________
You wrote
I am getting one problem.Please help me.
Class y is a subclass of class x. Will this compile?
x myX = new x();
y myY = (y)myx;
_________________________________________________________
You are assigning a superclass reference to subclass object
which is fine with explicit cast at compileime.
But at runtime it's going to ask"Who are u really" and it's
a superclass reference really and so at runtime it will be
an error.
Hope this helps.
Sarang
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
There should be atleast one public class in a source file.
Otherwise the program is not going to compile or run..
Do u guys know what is the percentage required to pass SCJP2


Bhasker Reddy
Yojana
Greenhorn

Joined: Jun 12, 2000
Posts: 19
Hi Evevrybody
Still I am not clear.
Please anybody!
Regards
Yojana

Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
at compile time the compiler is just checking if what you are trying to do is possible if it is possible it will compile in this example the compiler checks sees you made an explicit cast by doing that your telling the compiler dont worry about it is my problem and it compiles but at runtime when we actually have to do what you want which in this case cannot be done you will get a runtime exception
for instance if you try to turn an int to a byte you need an expicit cast otherwise you will get a compiler error because you may lose information in the switch so its not allowed by making the cast it doesnt mean its going to work just our way of letting the compiler know that i know what i am doing and let me do it so it does but at runtime i will lose some info
i hope this helps if it doesnt please tell emwhat parts you understand and what you dont and ill help you further
either post it here or email me
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Hie Yojana,
I will only try to discuss casting with reference types.So, when you are casting reference types in Java, the only requirement for a successful compilation is that the old type and the new type you are casting to should be related by inheritence.Here it does not matter which is the superclass and which is the sub class.So what does this mean ?
Assume the following declarations :
--------------------------------------------------------------
class Base{}
class Sub1 extends Base{}
class sub2 extends Base{}
---------------------------------------------------------------
From this, we can draw the following hierachies
A. Base->Sub1
B. Base->Sub2
So it means we can safely write :
-----------------------------------------------------------------
1. Base base = (Base)new Sub1()
2. Sub1 sub1 = (Sub1)new Base()
3. Base base = (Base)new Sub2()
4. Sub2 sub2 =(Sub2)new Base()
However YOU CAN NOT write :
a. Sub1 sub1 =(Sub1)new Sub2()//WRONG!!!
b. Sub2 sub2 = (Sub2) new Sub1()//WRONG
because Sub1 and Sub2 are not related by inheritence, that is, in the hierachies we drew above there is not path between sub1 and sub2.
Now after compiling a cast safely, all is not done and well until run time.At runtime, your cast will succeed IFF the true class of the object stored in the old type is actually the new type or its sub class.The true class of an object is the one you use when you call new.That is refering to the objects created above we have
A) 1. creates an object of type Sub1.This will succeed at runtime because we are casting it to Base and Sub1 is a subclass of base.
B) 2. will fail at runtime because we are creating an object of type base and casting it to Sub1 which is not the superclass of base.
C) 3. will succeed with the same rationale as in A above.
D) 4. will fail with the same reason as in B above.
E)The following will also work:
Base base = new Sub1();
Sub1 sub1 =(Sub1)base
because inside base we have stored an object whose true type is actually Sub1, although the variable itself is of type base.

I hope this gives a bit of more ground.Some people might hava to verify if along the way I did not get lost with types.
Regards,
Herbert.
 
wood burning stoves
 
subject: Please help me