aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What does the keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What does the keyword "final" have to do with compiling this code?" Watch "What does the keyword "final" have to do with compiling this code?" New topic
Author

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

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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

Joined: Jan 09, 2009
Posts: 67
Hello,

I think they both compile fine.

What compile error are you getting?



Rafael Angarita.
SCJP 6.
Alejandro Liz
Greenhorn

Joined: Sep 26, 2009
Posts: 6
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.


SCJP 78%, and looking for the next certification
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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

Joined: Apr 08, 2009
Posts: 11
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

Joined: Jul 15, 2008
Posts: 629

I try the same programme in NetBeans but I also got the same error.


Life is easy because we write the source code.....
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
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,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
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
 
subject: What does the keyword "final" have to do with compiling this code?
 
Similar Threads
Doubt about casting and final classes
Casting
Array Assignments
Inheritance and Polymorphism Involving Interface Casting
Please HELP...Devaka Exam Test 2 Number 69. I totally don't understand it.