This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes private and final ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "private and final ?" Watch "private and final ?" New topic
Author

private and final ?

aravind arav
Greenhorn

Joined: Jun 07, 2007
Posts: 12
Hello,
Look at the follwing code,its compiling without any error.My doubt is..In class A ive declared method f to be final & private but still am able to override it in class B. But,as far as i know final methods cannot be overidden!.Only when am using final with private am able to override it,otherwise not.Please explain !

import java.io.*;
class A{
private final void f(){
System.out.println("hello");
}
}

class B extends A{
void f(){
System.out.println(i);
}
public static void main(String s[]){
B b = new B();
b.f();
}
}


Aravind<br />SCJA
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Final methods cannot be overridden, that's correct.

But also: private methods are not inherited. The method f() in class A isn't visible in class B, because it's private in class A.

The method f() in class B does not override the method f() in class A, because class B simply doesn't see the f() in class A. The f() in class B should be regarded as a completely different method which has no connection to the f() in class A.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
aravind arav
Greenhorn

Joined: Jun 07, 2007
Posts: 12
okay .. got it! Thank you jesper young ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private and final ?
 
Similar Threads
Method Overloading and overriding
Referring a Local Inner Class
final methods
final method
private final method