Unable to run the following program
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
class Super
{
Super()
{
System.out.println("Constructor for Super class");
}
}
public class Sub extends Super
{
Sub()
{
System.out.println("Constructor for Sub class");
}
public static void main(String[] args)
{
Sub subObj=(Sub)new Super();
}
}
Hi,
The above program(Sub.java) is compiling but while running it is giving ClassCastException.. Why???
{
Super()
{
System.out.println("Constructor for Super class");
}
}
public class Sub extends Super
{
Sub()
{
System.out.println("Constructor for Sub class");
}
public static void main(String[] args)
{
Sub subObj=(Sub)new Super();
}
}
Hi,
The above program(Sub.java) is compiling but while running it is giving ClassCastException.. Why???
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
An instance of Super class is not an instance of its subclass Sub. You can not cast it to Sub.
[ August 25, 2006: Message edited by: wise owen ]
[ August 25, 2006: Message edited by: wise owen ]
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As far as compilation is concerned, compiler doesn't know anything about
'new Super()' only JVM can know which object is being created. Compiler doesn't have any problem because you are downcasting 'something' to 'Sub' and assigning the value to 'Sub' reference which is perfectly acceptable for compiler but when it comes to JVM (at runtime) it complains that
"I can't downcast Super "object" (not reference) to another class (even to its subclass)."
downcasting in object reference is acceptable in following case:
//code
Super superReference= new Sub();
Sub subReference=(Sub) superReference;
because here we are downcasting super class reference (which refer to sub class object) to sub class one.
'new Super()' only JVM can know which object is being created. Compiler doesn't have any problem because you are downcasting 'something' to 'Sub' and assigning the value to 'Sub' reference which is perfectly acceptable for compiler but when it comes to JVM (at runtime) it complains that
"I can't downcast Super "object" (not reference) to another class (even to its subclass)."
downcasting in object reference is acceptable in following case:
//code
Super superReference= new Sub();
Sub subReference=(Sub) superReference;
because here we are downcasting super class reference (which refer to sub class object) to sub class one.
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Good explanation, Vaibhav.
When you put an explicit cast Sub subObj=(Sub)new Super();, you are telling the compiler to ignore the type of the reference and that you are make the compiler believe that at runtime the reference will point to the correct object.
However, in your case, at runtime the reference is not pointing to an object of class Sub (or its subclass). So the JVM throws the exception.
Note: You can think of this statement: Sub subObj=(Sub)new Super();
as :
Super tempReference = new Super();
Sub subObj=(Sub)tempReference;
When you put an explicit cast Sub subObj=(Sub)new Super();, you are telling the compiler to ignore the type of the reference and that you are make the compiler believe that at runtime the reference will point to the correct object.
However, in your case, at runtime the reference is not pointing to an object of class Sub (or its subclass). So the JVM throws the exception.
Note: You can think of this statement: Sub subObj=(Sub)new Super();
as :
Super tempReference = new Super();
Sub subObj=(Sub)tempReference;
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Vaibhav Chauhan
Ranch Hand
Posts: 115
| I found some pretty shells, some sea glass and this lovely tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







