简体   繁体   中英

Overloading a private method in Java

I want to get this thing straight: does overloading apply to methods in sub/super classes, or only to methods of one class can be overloaded?

public class Super{

     private void method(){
     }
}

class Sub extends Super{

     private void method(){ 
     }
     private void method(int x){    
     }  
}

Are both methods of Sub legally overloaded? Is the method of Super overloaded as well?

I don't know why you made all methods private. If you didn't, your question would actually make perfect sense. Consider this code:

class Super{
  void method() {}
}

class Sub extends Super {
  void method(int x) {}  
}

Now, even though it just declares one, the class Sub actually has two methods named method , therefore that method is overloaded for Sub . The class Super is unaffected because it still has just one method .

As an aside, the most notorious example where the above gets in the way of program correctness involves the standard method equals . Beginners are tempted to implement it just for the specific type:

public class Thing {
  public boolean equals(Thing that) { ...compare by object contents... }
}

but this doesn't override Object.equals , so now the class has two equals methods. The worst thing comes when some code accidentally uses the specific overload, whereas other code uses the general one:

Thing t1 =  new Thing(), t2 = new Thing();
System.out.println(t1.equals(t2)); // true, great
Object o1 = t1, o2 = t2;
System.out.println(o1.equals(o2)); // now suddenly false
System.out.println(t1.equals(o2)); // false again
System.out.println(o1.equals(t2)); // still false

You can't override a private method, because outside of Super , you can't even call the method. Even in subclasses. You can define another method with the same name, but then the superclass still has its method, and the subclass has its own separate method.

You need to understand basic overriding Rules in java :

 0).private, static and final method can  not be overridden 

A overriden method cannot :

 1)   reduces access of overriden method i.e.if overridden method declared in parent class is defined with access modifier public than overriding method can not be  package private or protected 
 2).  throw broder checked Exception For example if overridden method throws FileNotFoundException then overriding method can not throw java.lang.IOException

As your question revolves around private method, i'll try to explain it.

  1. private method with in a class

     You are allowed to use same private method name with different signature with in a class. 

Please find the below example

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

private void method(int x) {
    System.out.println("method with param x");
}

2.private method in sup/sub class

    There is no question of using private method outside the class as it is not visible.  Means you can't overload a super class private method in sub class. 

Let's see what method overloading is by JLS :

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

What do we mean by override-equivalent ?

Let's see what JLS says :

Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.

The signature of a method m1 is a subsignature of the signature of a method m2 if either:

m2 has the same signature as m1, or the signature of m1 is the same as the erasure (§4.6) of the signature of m2.

Now, lets see the example above.

2 private methods in Sub are overloaded.

The method() of Super is not inherited by Sub as it is private. Hence, there is no overloading between these method() of Super and method(int x) of Sub.

Let's see an simple example of overloading in a class in inheritance chain. In Eagle class, fly() is overloaded.

public class Bird {
    public void fly() {
        System.out.println("Bird is flying");
    }
    public void eat(int food) {
        System.out.println("Bird is eating "+food+" units of food");
    }
}
public class Eagle extends Bird {
    public int fly(int height) {
        System.out.println("Bird is flying at "+height+" meters");
        return height;
    }
}

public class Super{

 private void method(){
 }

} class Sub extends Super{

 private void method(){ 
 }
 private void method(int x){    
 }  

}

Are both methods of Sub legally overloaded? The Answer is Yes.

Is the method of Super overloaded as well? The Answer is No. Since Method is private in Super class it is not visible in subclass.

does overloading apply to methods in sub/super classes, or only to methods of one class can be overloaded? The Answer is Yes. All the public and protected methods of the super class are derived in child class. You can overload derived methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM