aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward declaration doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward declaration doubt" Watch "Forward declaration doubt" New topic
Author

Forward declaration doubt

nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
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"

and why no fwd declaration error!!!

also why casting at line 4 and lin5
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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).

Same logic for line no 5.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Sagar Rohankar wrote:
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).

Same logic for line no 5.


How about this one?







SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Vijitha Kumara wrote:
How about this one?






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

So, Why this things with interface only ??

I looked at JLS,

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Vijitha Kumara wrote:
How about this one?






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

So, Why this things with interface only ??

I looked at JLS,
But didn't get anything useful..

[edted: Sorry, cant put that JLS part here]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I think this will clear most of the doubts.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

I think here at JLS tells the conversion.


- From any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17



Hey that's my first post ever on ranch


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Vijitha Kumara wrote:I think here at JLS tells the conversion.


- From any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.


Thanks a ton !!

Thanks James & Ankit, too.
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
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

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
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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
A class can cast to an interface type as marker interface.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward declaration doubt