• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help me

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yojana
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Evevrybody
Still I am not clear.
Please anybody!
Regards
Yojana

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic