*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final & private together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final & private together" Watch "final & private together" New topic
Author

final & private together

Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
In khalid mughal java certification book he has given

"private methods are implicitly final".What sense does it make.......

Don't you ppl think declaring private methods as final is of no use???

bcoz private methods are any how not visible in the subclass.Then where is the question of restricting to override.

class Base {

// What diff. does it make if I don't add final.
final private void method() {

System.out.println("Base:method()");
}
}

public class Test24 extends Base {

public static void main(String[] args) {

Test24 instance = new Test24();

instance.call();
}

void call() {

method();
}

void method() {

System.out.println("Extended:method()");
}
}
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
What you said is correct, what the book means is that, there is no use of declaring a private method as final as it is not visible in subclass,so it said like that.


Kiran Kumar.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Hi kiran,

What you said is correct, what the book means is that, there is no use of declaring a private method as final as it is not visible in subclass,so it said like that.

I don't think he has meant wat U are saying.

bcoz at one place he has given declaring static variable as transient is redundant and it should be avoided.

But he din't say the same thing here............
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Hi kiran,

What you said is correct, what the book means is that, there is no use of declaring a private method as final as it is not visible in subclass,so it said like that.

I don't think he has meant wat U are saying.

bcoz at one place he has given declaring static variable as transient is redundant and it should be avoided.

But he din't say the same thing here............
Kristian Perkins
Ranch Hand

Joined: Mar 27, 2006
Posts: 32
From JLS
8.4.3.3 final Methods:

A private method and all methods declared immediately within a final class (�8.1.1.2) behave as if they are final, since it is impossible to override them.
[ April 24, 2006: Message edited by: Kristian Perkins ]
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
You know anonymous classes are final by default, we can say it because we are not going to extend this classes, your question is similar to this.

it is true that static variables cannot be declared transient because they are not part of object, he asked us not to use it because the combination transient static give you compile time error but final private doesnot give you compile time error.
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
Sorry i checked it ,combination transient static is not giving compile time error but that might be the reason which i explained in my previous post
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Still not cleared ppl.

could any one clear my doubt???

Thanks in advance...
Kristian Perkins
Ranch Hand

Joined: Mar 27, 2006
Posts: 32
Compile this code:


If method() was inherited in InnerFinal we would get a compile time error because of an incompatible return type on the inner class method(). But we don't, this code compiles fine because of the (pseudo) implicit final applied to the method() method.

Notice also if you change the access modifier of FinalTest.method() to anything else (protected, public or default) the code will not compile due to the error mentioned in the previous paragraph.

[ April 24, 2006: Message edited by: Kristian Perkins ]
[ April 24, 2006: Message edited by: Kristian Perkins ]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847

"private methods are implicitly final".What sense does it make.......

Don't you ppl think declaring private methods as final is of no use???


That's exactly what it says right there...
There's no use declaring them as final since they're final already because they're private.
After all, you declare a method as final to prevent it from being overridden, which is also a sideeffect of declaring it as private.


42
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Still din't get........

My Question is "When private methods are not visible in the subclass

where is the question of preventing overriding(by making it implicitly final)".

What I mean is even if we donnot make private methods implicitly final it doesnot make any difference.

perkins
--------
class FinalTest {

private void method() {}

static class InnerFinal extends FinalTest {
public String method() {
return null;
}
}
}

Here assume that private method method() is not implicitly final.Even Then
what you said holds good(I mean it's possible to override bcoz method() any how not visible in InnerFinal).
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
The method in the inner class doesn't override anything, it HIDES the method in the outer class.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
I got the answer for this...

By declaring private methods as final we will get improvement in performance.

The binding of a method call to a method implementation is done at compile time if the method is static or final
(private methods are implicitly final).

Methods declared as final can sometimes provide a performance enhancement: The compiler is free to inline calls to them
because it �knows� they will not be overridden by a subclass. When a small final method is called, often the Java
compiler can copy the bytecode for the subroutine directly inline with the compiled code of the calling method,
thus eliminating the costly overhead associated with a method call. Inlining is only an option with final methods.
Normally, Java resolves calls to methods dynamically, at run time. This is called late binding. However, since final
methods cannot be overridden, a call to one can be resolved at compile time. This is called early binding.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Am I right???
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
that might be true in C++ but not Java.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Hi Jeroen,

No I copied above lines from java books(Complete ref and Khalid mughal).

I think I am right???wat U say???
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Hi Jeroen,

No I copied above lines from java books(Complete ref and Khalid mughal).

I think I am right.wat U say???
 
 
subject: final & private together
 
Similar Threads
SCJP Query
overridding
when can you override
Overriding Static Methods, Huh?
Which Access Modifier when override toString()?