Overloaded and overridden in Java

10,019

Solution 1

Overloading and overriding are complementary things, overloading means the same method name but different parameters, and overriding means the same method name in a subclass with the same parameters. So its not possible for overloading and overriding to happen at the same time because overloading implies different parameters.

Examples:

class A {
    public void doSth() { /// }
}

class B extends A {
    public void doSth() { /* method overriden */ }

    public void doSth(String b) { /* method overloaded */ }

}

Cheers!

Solution 2

overloading and overloading are just abstractions. Overloading just means the compiler uses the name in conjunction with the types and number of parameters for addressing what function to call. In reality overloading a method is no different than naming it something different because the key the compiler uses to look up the function is a combination of name and parameter list.

Overriding is kind of the same principle except the compiler can address the overriden function with the super keyword.

So can you override an overloaded function? Yes, since the overloaded method is a completely different method in the eyes of the compiler.

Solution 3

It depends what you mean. A method can be an override for an overloaded method in a superclass. And you can overload a method that you are simultaneously overriding using another method.

However, you cannot have one method that is both a new overload and an override. For a method to be an override, another method with the same signature must already exist in the superclass ... and that means that this method cannot be a new override.

Share:
10,019
Weitao Wang
Author by

Weitao Wang

Updated on June 03, 2022

Comments

  • Weitao Wang
    Weitao Wang almost 2 years

    I know how to overload a method, and how to override a method. But is that possible to overload AND override a method at the same time? If yes, please give an example.

  • user845279
    user845279 almost 12 years
    If you want to, you can override an overloaded method. It's as close as it can get.
  • user207421
    user207421 almost 12 years
    Overriding isn't the same thing at all. The decision as to which method to call is deferred to runtime. The compiler doesn't decide it.
  • nsfyn55
    nsfyn55 almost 12 years
    @EJP - Ultimately the compiler makes the decision even if its abstracted through the construction of a vtables and assignment of vpointers to facilitate dynamic dispatch. The ties that bind have to be initialized somewhere.
  • nsfyn55
    nsfyn55 almost 12 years
    @EJP - secondly if you read closely you'll see all that I said is that the compiler will treat overloaded methods as completely different methods because the compiler uses the name and the parameter list to form a key. This absolutely plays a role in the dynamic dispatch initialization process at compile time. As for the last bit, go ahead and add an override annotation to an overloaded method in a subclass with a final modifier and see if the compiler decides to get involved
  • user207421
    user207421 almost 12 years
    @nsfyn55 The compiler cannot possibly be said to make the decision when the decision can involve classes that the compiler isn't seeing at the time that it is alleged to do so. What the compiler does it to generate code which in conjunction with the runtime system or runtime library ensures that the correct method is called. This is not the same thing as the compiler making the decision. It is also not the same thing as the compiler deciding among overloads, contrary to your statement above in your answer.
  • nsfyn55
    nsfyn55 almost 12 years
    @EJP-ugh. I'm not even sure what you are talking about. The call is virtual, but it still lays down a jump instruction it just happens to jump somewhere else once it gets there. It does lay down two different addresses for overloaded methods because they are completely different methods. Implementations are irrelevant. All I am saying is that overloading only exists because the compiler doesn't key off just the name, but it does make special consideration for the super keyword when overriding methods.
  • stdout
    stdout about 7 years
    When this happens, which is responsible for the binding (in terms of static or dynamic) of the method then? Overriding or overloading?