This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Interface question  RSS feed

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Oh good.

Thanks,

Muthaiah
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
Java Scala
  • Mark post as helpful
  • send pies
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!