hi I have come across this question from notes given by a friend
class A{}
class B extends A implements E{} //line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
public static void main(String[] args) {
A a = new D(); //line 2
C c = new C(); //line 3
E e = (E)a; //line 4
B b = (B)e; //line 5
}
}
I want to know why line 4 does not gets compiler error
"Compilation error on line 4 because class A does not implement interface E"
nitude gupta wrote:
I want to know why line 4 does not gets compiler error
"Compilation error on line 4 because class A does not implement interface E"
and why no fwd declaration error!!!
also why casting at line 4 and lin5
Here, at line 4, a is an object of class D, and class D extends B and implements interface E, So E is at top of the hierarchy.
SO compiler see this relation and allowed to typecast it (upcasting).
nitude gupta wrote:
I want to know why line 4 does not gets compiler error
"Compilation error on line 4 because class A does not implement interface E"
and why no fwd declaration error!!!
also why casting at line 4 and lin5
Here, at line 4, a is an object of class D, and class D extends B and implements interface E, So E is at top of the hierarchy.
SO compiler see this relation and allowed to typecast it (upcasting).
ouch.. Good catch, I didn't know that..
that's mean, we can cast to any interface type, no matter object implements that interface or do not implement..
The same program for both of class type doesn't work
ouch.. Good catch, I didn't know that..
that's mean, we can cast to any interface type, no matter object implements that interface or do not implement..
The same program for both of class type doesn't work
nitude gupta wrote:Thanks a lot everybody that was really helpfull and good discussion, I have read kathy but still having such doubts I think because I didn't read JSL
JLS is like Java Docs, you don't have to remember each and everything. And having such doubts mean you understand the concepts and start playing with it..
nitude gupta wrote:
Is there an alternative to JSL, I feel lost n .Could you guys suggest some material which covers JSL on whole,I mean an alternative to it
Don't worry, JLS is too big to get .. So better make your basic fundamentals clear..
And when some things didn't works as expected ( as in the current example), Go through JLS. Look for JSL to find your doubts.. This way you can solve your most of the queries.
anand-k jha
Greenhorn
Joined: Dec 02, 2008
Posts: 15
posted
0
A class can cast to an interface type as marker interface.