aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Interface question" Watch "Interface question" New topic
Author

Interface question

Muthaiah Ramanathan
Ranch Hand

Joined: May 16, 2005
Posts: 102
Ranchers,

This is a question in Devaka's mock exam. Why is this allowed at compile time?





Thanks.


SCJP 1.6, SCJD, SCWCD, SCBCD.

Be nice to people on the way up cos, you'll need 'em on your way down - From somewhere I can't remember!
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

The cast between A and I compiles fine because A is non-final. So there can be a class which inherits from both A and I, so the compiler allows the cast. Since B is final, so there can't be a class which inherits from both B and I, so at runtime the cast between B and I can never succeed. This is why the compiler disallows the cast...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

thats really a great answer
May I know how much studying and coding does it take to reach to such a perfection level?
please let me know

SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 771
Hello,

You should go through K&B thoroughly and additionally frequently consult JLS and java API docs and with lots
of mock questions practice, you will be able to reach what you desire. Write lots of code though.

Best Regards,


Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Prasad Kharkar wrote:May I know how much studying and coding does it take to reach to such a perfection level?


IMO, apart from studying and coding , also you need to browse through the *forum* like Javaranch regularly
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Seetharaman Venkatasamy wrote:IMO, apart from studying and coding , also you need to browse through the *forum* like Javaranch regularly


+1 (from my experience )
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

well well well
I am going fine then
thanks for the posts
I'll be like you all experts in few months
Muthaiah Ramanathan
Ranch Hand

Joined: May 16, 2005
Posts: 102
Oh good.

Thanks,

Muthaiah
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Ankit Garg wrote:

The cast between A and I compiles fine because A is non-final. So there can be a class which inherits from both A and I, so the compiler allows the cast. Since B is final, so there can't be a class which inherits from both B and I, so at runtime the cast between B and I can never succeed. This is why the compiler disallows the cast...



I really don't understand this. Compiler can makeout that class A doesn't implement interface I. Why is it allowing the cast to compile?


SCJP6
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

I guess that it's because there can be a class ( C ) that extends A and implements I... Note that I'm not saying that there's.

In compile time it will work, but in runtime it will throw a ClassCastExcpetion.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

The answer would still be the same (Andre said the same thing too). There can be a class which extends A and implements I. So the compiler has to allow the cast. Suppose I have a method like this
How can the compiler not allow the cast. This cast may actually be successful at runtime. We don't know what is the actual object passed to this method. The object might be of a class C which extends A and implements I. We have not created the class C right now but that doesn't mean we can't in the future. Or see it this way, suppose you are developing a library, then others might extend your classes. Lets take the java API for example. If I create this type of code
The Thread class doesn't implement Comparable. But I might create a Thread subclass which also implements Comparable. And someone else might use my comparable-thread class. So the cast will have to be allowed...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface question
 
Similar Threads
Final Class Problem
dan chisolm exam question
why variables from super(....); must be declared static?
K&B Book not complete ???
Inner Class in the Interface