Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Harry Henriques
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • 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?

 
Rafael Angarita
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I think they both compile fine.

What compile error are you getting?


 
Alejandro Liz
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
George Coolidge
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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?

 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try the same programme in NetBeans but I also got the same error.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic