Deepa Natarajan
July 31st, 1999, 08:07 PM
Hi,
I have 2 questions of the classCastingExcep concept.
Source code 1:
class parent{};
class D1 extends parent{}
class D2 extends parent{}
class Test{
public static void main(){
parent p1 = new parent();
D1 d1 = new d1();
D2 d2 = new d2();
d1 = (D1)p;
}
}
Which ans is correct?
1.illegal both compile and runtime
2.Legal at compile bit fails in runtime
3.Legal at compile and runtime
Source Code 2:
class Test{
static void s(){
S.o.p("***");
}
}
class D1 extends Test{
static void s(){
S.o.p("***");
}
public static void main(){
Test t1 = new Test();
t1.s();
D1 d1 = new D1();
d1.s();
d1 = (D1)t1;
d1.s();
}
}
Both compile but give a runtime exception.Why?What is the change and what is the concept behind?
Thanks,
Deepa
I have 2 questions of the classCastingExcep concept.
Source code 1:
class parent{};
class D1 extends parent{}
class D2 extends parent{}
class Test{
public static void main(){
parent p1 = new parent();
D1 d1 = new d1();
D2 d2 = new d2();
d1 = (D1)p;
}
}
Which ans is correct?
1.illegal both compile and runtime
2.Legal at compile bit fails in runtime
3.Legal at compile and runtime
Source Code 2:
class Test{
static void s(){
S.o.p("***");
}
}
class D1 extends Test{
static void s(){
S.o.p("***");
}
public static void main(){
Test t1 = new Test();
t1.s();
D1 d1 = new D1();
d1.s();
d1 = (D1)t1;
d1.s();
}
}
Both compile but give a runtime exception.Why?What is the change and what is the concept behind?
Thanks,
Deepa