• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface question

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers,

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





Thanks.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh good.

Thanks,

Muthaiah
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic