9

Since private methods are implicitly final.

private or static or final methods are early bind means they can't be overridden.

But in my code it is actually running properly.

public class B extends A {
    public static void main(String[] args) {
        new B().privateMethod(); //no error -output B-privateMethod.
    }

    private void privateMethod() {
        System.out.println("B-privateMethod.");
    }
}

class A{
    private void privateMethod() {
        System.out.println("A-privateMethod.");
    }

    private static void privateStaticMethod() {
        System.out.println("privateStaticMethod.");
    }
}

Also I want to make sure what the benefit is of making a private member static, besides the fact that you can use class-name.member, over a non-static member within a class. They are private so can't be used outside the class. For example the hugeCapacity() method in the ArrayList class.

private static final int DEFAULT_CAPACITY = 10;

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
} 
  • 1
    Private actually has nothing to do with it... I guess your question is "When should a method be static?"? – user253751 Apr 17 '15 at 12:14
  • @TheLostMind I suggest you post your answer to the duplicate question in here. – Mena Apr 17 '15 at 13:05
  • 1
    Read it http://stackoverflow.com/questions/685522/using-private-static-methods – Rajesh Apr 17 '15 at 13:12
  • 1
    You are actually asking two questions at the same time, I would suggest that you split it up. It will increase your chances of getting the right answer to the right question. – Jaap Coomans Apr 17 '15 at 13:48
  • By the way, private methods are not implicitly private :P – SSC Apr 17 '15 at 14:20

3 Answers3

6

While I understand this may look like privateMethod() in B overrides privateMethod() in A, it actually doesn't. Because the visibility of a private method is restricted to the owning class only, you actually created two separate methods that happen to have the same name and signature.

When the method would be overwritten, you would be able to do this:

public class B extends A{
    /* ...snip... */

    private void privateMethod() {
        super.privateMethod();
        System.out.println("B-privateMethod.");
    }
}

This will however not compile, because of the aforementioned restricted visibility. When the methods would not have been private, this would have worked and would have printed:

A-privateMethod.
B-privateMethod.
Jaap Coomans
  • 1,465
  • 4
  • 15
  • 24
2

Since private methods are implicitly final and private, static and final methods are early binding means they can't be overridden. But in my code it is actually running properly.

private methods are not implicitly private, they are explicitly private. static and final methods are early bindings and they can really not be over-ridden. In your case, you might think that they are overridden but actually, they are not. For example, how to know if a method is overridden? Overridden methods allow you to call the super method, using super().functionName() But, in your case, if you will try to do, it will give you a compile time error, which clearly means, that your methods are not overridden. You are just using the same signature but actually they are never overridden.

Also I want to make sure what the benefit is of making a private member static

You probably know that a static member is not part of the object but class. Plus, if you have static methods in your class and they need access to a member which is not static, how would you do that? You will have to create an object of the class and use it, right? But, if it's private and static, you can use them in the static functions or blocks of your class.

Hope, it clears things.

SSC
  • 2,956
  • 3
  • 27
  • 43
  • I'm not allowed to make a 2-character edit, so I'll comment here: the code snippet should read `super.functionName(...)`. Right now it will not compile because it calls the no-arg constructor of the super class. – Jaap Coomans Apr 21 '15 at 12:05
  • @JaapCoomans That's just a syntax, if there are arguments. Plus, var-args means there can be 0 or more arguments. – SSC Apr 26 '15 at 07:22
  • I don't think you get my point. Your answer mentions `super()`, which is a call to the no-argument constructor of the superclass. You can't call a method (`functionName()` in your example) on a constructor. It should read `super`, a reference to the superclass which can be used to access fields and methods of the superclass. – Jaap Coomans Apr 29 '15 at 06:39
1

Your code has no overriding in it. it is just two class have individual method with same name without having any kind of relationship as both do not know the existence of another.

static method are used so that for calling them we do not have to create an object of the class it resides. i will make a static method if i only want the class itself and sub classes to use it.

Rahul Thachilath
  • 363
  • 3
  • 16
  • please look the privateMethod() method of class A,this has been overriden in class B. class B is extending class A man. –  Apr 17 '15 at 12:28
  • @dubey-theHarcourtians that doesn't mean anything is being overridden. You just have two separate methods with the same name and no relationship between them. – user253751 Apr 17 '15 at 12:35
  • @dubey-theHarcourtians go through https://docs.oracle.com/javase/tutorial/java/IandI/override.html – Rahul Thachilath Apr 17 '15 at 12:36
  • this is a sample code I am not giving any specific behavior of privateMethod() in both class.please got the point method privateMethod() of class A is being overriden in class B note- public class B extends A{} –  Apr 17 '15 at 12:43
  • @dubey-theHarcourtains Rahul is right. Try adding an `@Override` over the `privateMethod` in `B` and compile again. There will be an error that `privateMethod` in `B` is not overriding any method, even though it says so with `@Override` – Soana Apr 17 '15 at 12:49
  • 1
    @dubey-theHarcourtians basically overriding means. e.g. if **B extends A** and both have public method **draw()**. it means class **B** overrides method draw(). i.e. if we run this. **A a = new B()**. and **call a.draw();** it will call **B's** draw function rather than **A's**. that means it is **overridden** – Rahul Thachilath Apr 17 '15 at 13:04
  • 1
    @dubey-theHarcourtians in the case of private method there is no case where the subclass method acts instead of parent class method. so it is not overridden – Rahul Thachilath Apr 17 '15 at 13:05