File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "private and final ?" Watch "private and final ?" New topic

private and final ?

aravind arav

Joined: Jun 07, 2007
Posts: 12
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 !

class A{
private final void f(){

class B extends A{
void f(){
public static void main(String s[]){
B b = new B();

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

Joined: Aug 16, 2005
Posts: 15081

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 8 API documentation
aravind arav

Joined: Jun 07, 2007
Posts: 12
okay .. got it! Thank you jesper young ..
I agree. Here's the link:
subject: private and final ?
It's not a secret anymore!