• 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

What does the keyword "final" have to do with compiling this code?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code snippet DOES NOT compile, but the code snippet below it DOES compile and run. This problem is part of a Drag-n-Drop problem from ExamLab by M Devaka Cooray.



The example below DOES compile without warnings or errors, and runs without exceptions. What is the difference between this code and the code above, and why does the keyword "final" make the difference?

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

I think they both compile fine.

What compile error are you getting?


 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that both codes do the work, but the diference with the "final" modifier is that class will can't be subclased, in that case, the final class Three will never be a parent class.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rafael Angarita wrote:I think they both compile fine.



Sorry, I made a mistake when I transposed the snippet. This is the correct non-compiling code below.



C:\Documents and Settings\Harry Henriques>javac CCC.java
CCC.java:7: inconvertible types
found : CCC[]
required: java.lang.Runnable[]
ra1 = (Runnable []) c;
^
1 error


The following code snippet compiles without any problem.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which compiler do you use? It works fine for both versions in my eclipse.

Harry Henriques wrote:The following code snippet DOES NOT compile, but the code snippet below it DOES compile and run. This problem is part of a Drag-n-Drop problem from ExamLab by M Devaka Cooray.



The example below DOES compile without warnings or errors, and runs without exceptions. What is the difference between this code and the code above, and why does the keyword "final" make the difference?

 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try the same programme in NetBeans but I also got the same error.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
It is because, when you declare a class final, then all of its methods are final implicitly. So as data members also i suppose.
Umm i believe my notion is wrong, because if we do something like

c[0] = new CCC();

It works. Additionally, both of the classes are not lying in the same inheritance hierarchy.
If you change the code somehow like this.


In this case, it is compiling fine even with the keyword Final.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Zaidi wrote:Hello,
It is because, when you declare a class final, then all of its methods are final implicitly. So as data members also i suppose.
Umm i believe my notion is wrong, because if we do something like

c[0] = new CCC();

It works. Additionally, both of the classes are not lying in the same inheritance hierarchy.
If you change the code somehow like this.


In this case, it is compiling fine even with the keyword Final.



I believe after doing a bit of RND, in case of final keywords, the compiler does the bit of optimization, means the check of casting
which mostly happens at Runtime in non-final cases are checked at compile time in case of final keywords. Correct me please if
i am wrong.
 
Sheriff
Posts: 9707
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
In case of a non-final class, a cast to/from interface is always allowed as there might be a class that is a sub-type of both. For example



So even if you don't declare a class D, but it might exists somewhere or someone else might create it, so the compiler allows a cast between a non-final class and interface. But in case of final-class, if that class doesn't implement an interface, then a cast between that interface and the class is not allowed


Since there can't be any class that is a sub-type of both C and I, so there can't be any object that IS-A C and I. This is why a cast between C and I is rejected at compile time as the compiler knows that the cast can't succeed at runtime.

HTH
 
George Coolidge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly, unless it is absolutely impossible for C to be is-a Runnable interface, the compiler will let it pass and throw ClassCastException at runtime if the referenced type (C or its subtypes) doesn't implement Runnable interface.

Ankit Garg wrote:In case of a non-final class, a cast to/from interface is always allowed as there might be a class that is a sub-type of both. For example



So even if you don't declare a class D, but it might exists somewhere or someone else might create it, so the compiler allows a cast between a non-final class and interface. But in case of final-class, if that class doesn't implement an interface, then a cast between that interface and the class is not allowed


Since there can't be any class that is a sub-type of both C and I, so there can't be any object that IS-A C and I. This is why a cast between C and I is rejected at compile time as the compiler knows that the cast can't succeed at runtime.

HTH

 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic