• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward declaration doubt

 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?






 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this will clear most of the doubts.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hey that's my first post ever on ranch
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class can cast to an interface type as marker interface.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic