• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

final & private together

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()");
}
}
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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............
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still not cleared ppl.

could any one clear my doubt???

Thanks in advance...
 
Kristian Perkins
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"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.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method in the inner class doesn't override anything, it HIDES the method in the outer class.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I right???
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that might be true in C++ but not Java.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeroen,

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

I think I am right.wat U say???
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic