Reputation:
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;
}
Upvotes: 9
Views: 194
Reputation: 3008
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.
Upvotes: 2
Reputation: 1465
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.
Upvotes: 6
Reputation: 363
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.
Upvotes: 1