Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
Thanks alot for the response but what I guess I don't get is why does explicit casting exist for objects? Since upcasting is implicit, it doesn't require a cast in the () format as my first example. This compiles just fine:
public class BaseTest2{
public static void main(String [] args){
BaseTest2 base = new BaseTest2();
Sub2 sub = new Sub2();
base = sub;
}
}
class Sub2 extends BaseTest2{}
What situations wouldn't throw a runtime exception, or is it simply there because it is required for primitives and the exception just lets you know that "hey, downcasting is never leagal under any circumstances???"
originaly posted by Sabarish
It is not that downcasting is always illegal at runtime. In some cases u may use a superclass reference to hold an object of the subclass. In that case, u can get that object back into a subclass reference by using the downcast. I have modified ur program to demonstrate this...
code:
--------------------------------------------------------------------------------
public class BaseTest2 {
public static void main(String [] args) { //Below line is modified to hold subclass object BaseTest2 base = new Sub2(); //The following should now be valid at runtime too Sub2 sub = (Sub2)base; }}
class Sub2 extends BaseTest2 {}
--------------------------------------------------------------------------------
Such a downcast is to instruct the compiler that u know what ur doing, that is u know that ur casting the object reference into a more specialized type. In some cases this may be desirable. For ex suppose Sub2 has a method m2() which is not in BaseTest2. In that case u can't invoke that method using the reference base even though base points to a valid Sub2 type object and has the m2() method. U will have to cast the reference to a Sub2 type reference to make the program compile and invoke m2().
That should be the reason why ClassCastException is a RuntimeException. Only the programmer can predict whether the cast will work or throw an exception !
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Originally posted by Barkat Mardhani:
Hi Ron:
Your example is talking about classcast exception when you cast one derived class to another derived class. Whereas, Alan is talking about casting a super class object to a derived class....
public class BaseTest2
{
public static void main(String [] args)
{
BaseTest2 base = new BaseTest2();
Sub sub = (Sub)base;
}
}
class Sub2 extends BaseTest2
{
}
Compiles fine, but throws a ClassCastException. Now, I understand why the cast is required for compiling, but why is there an exception?
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
"You are sad," the Knight said in an anxious tone: "let me sing you a song to comfort you."
"Is it very long?" Alice asked, for she had heard a good deal of poetry that day.
"It's long," said the Knight, "but it's very, very beautiful. Everybody that hears me sing it -- either it brings the tears into their eyes, or else -- -"
"Or else what?" said Alice, for the Knight had made a sudden pause.
"Or else it doesn't, you know. The name of the song is called 'Haddocks' Eyes.' "
"Oh, that's the name of the song, is it?" Alice said, trying to feel interested.
"No, you don't understand," the Kinght said, looking a little vexed. "That's what the name is called. The name really is 'The Aged Aged Man.'"
"Then I ought to have said, 'That's what the song is called'?" Alice corrected herself.
"No, you oughtn't: that's another thing. The song is called 'Ways and Means': but that's only what it's called, you know!"
"Well, what is the song, then?" said Alice, who was by this time completely bewildered.
"I was coming to that," the Knight said. "The song really is 'A sitting on a Gate': and the tune's my own invention."
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Originally posted by Alan Phillips:
How can I access the methods in a superclass from a subclass which overides the method.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Originally posted by Alan Phillips:
working through it really helped me understand that a superclass can be a subclass or a superclass, a subclass can ONLY be a subclass.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
This is why Ron and I are getting the feeling that you're still a bit off. The runtime type of an object never changes: once it's instantiated, it will remain the same type no matter what kind of casting you do. Casting merely allows you to reference an object in a more general (upcasting) or specific (downcasting) manner. Casting does not change the type of an object Casting simply allows you to assign the value of one reference variable to another. As an added precaution, the Java Runtime checks if the reference type is compatible with the actual object type; if it isn't a ClassCastException is thrown.
Sorry, but this is incorrect. The runtime type is = to the type assigned, not the type instantiated. The type instantiated declares what it CAN be (itself or subclass but not its superclass). Try it with getClass()!The runtime type(as defined by the API can change . If it didn't you couldn't call the methods of the superclass which I've demonstrated you can!2. The runtime type of the actual object that obj references is determined by the argument to new, which in line 2 is SubClass, and in line 4 is SuperClass.
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|