开发者

Overriding or overloading?

Guys I know this question is silly but just to make sure:

Having in my class method:

bool开发者_JAVA技巧ean equals(Document d)
{
//do something
}

I'm overloading this method nor overriding right? I know that this or similiar question will be on upcoming egzam and would be stupid to not get points for such a simple mistake;


Based on the code provided, we can't tell for sure whether you're overloading or overriding it.

You are most likely overloading the equals(Object o) method.

class A {
   void method() {..}
}

class B extends A {
   // this is overriding
   void method() {..}
}

And

// this is overloading
class A {
   void method(boolean b) {..}
   void method(String arg) {..}
   void method(int arg) {..}
}

P.S. you are using a bracket convention that is not widely accepted on the java world. In Java it is more common to place opening the curly bracket on the same.


You are not even overloading, since the other method is called equals. But if you add that s, you will be overloading equals. Although, to be precise, we talk about overloading if two (or more) methods with the same name but different signature are defined in the same class. In your case, it is trickier, since your equals with its different signature partly hides the original equals. Which is usually a bad thing, because this almost always leads to hard to understand behaviour, thus subtle bugs. Whenever someone calls equals on an instance of your class, depending on the parameter type the call may go to a different implementation of the method.

class Document {
  public boolean equals(Document d)
  {
    //do something
  }
}

Document doc1 = new Document();
Document doc2 = new Document();
String string = new String();

doc1.equals(doc2); // calls Document.equals
doc1.equals(string); // calls Object.equals!

You would be overriding Object.equals if you defined your method with the exact same signature as the original, i.e.

public boolean equals(Object obj) ...

In this case, both of the above calls to equals correctly execute Document.equals.


From the code you posted it could be either. If equal is defined in a superclass with the same parameter declarations then you are overriding it. If there is already a method called equal, but with different parameter types, you are overloading it.


On a related note, if you are using Java 5 and above and your intent is to override then it is highly recommended to use the @Override annotation before the method definition to indicate your intention. The wrong usage of this annotation (i.e. when you want to override and are not doing so) would flag a compile error.

As of Java 6 you can use the @Override annotation while defining methods that are declared in an interface the class in implementing.


Overloading: same method name, same parameter list, different classes Overriding: same method name, different parameter list, same or different classes.

Class A {

    bool Equals(Document d) {...} 

    bool Equals(A a) {...} // overloaded method
}

Class B extends A {
    bool Equals(Document d) {...} // overridden method

    bool Equals(B b) {...} // overloaded method
}

One thing to note, the return type does not matter, it's the name of the method and the parameter list that make all the difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜