21

Possible Duplicate:
Polymorphism vs Overriding vs Overloading

I'm struggling to know why method overloading and overriding needed in java?

I have read some articles regarding this but not able to catch why it's needed practically?

I also visited the below url in stackoverflow but i'm not clear in this topic yet.

Java overloading and overriding

Any practical example will be appreciated.

Thanks in advance.

Community
  • 1
  • 1
macOsX
  • 447
  • 1
  • 9
  • 19
  • 1
    http://en.wikipedia.org/wiki/Function_overloading http://en.wikipedia.org/wiki/Method_overriding both have practical examples. – Karthik T Jan 09 '13 at 05:02
  • What don't you understand from the link you cited? – fge Jan 09 '13 at 05:02
  • 1
    @fge: I can able to understand the difference between both but it's not clear for me when and how to choose this polymorphic types. – macOsX Jan 09 '13 at 05:05
  • 1
    You might choose to override a method when you're using inheritance. You choose to overload a method when you want to use the same method name with different kinds of arguments. There really is no concept of choosing between the two... they're completely different. – jahroy Jan 09 '13 at 05:08
  • 1
    @jahroy: Thanks for your comments. But still there are people like me interested in knowing the things which we don't know. Kindly help me if you can. – macOsX Jan 09 '13 at 05:08
  • dear simple overloading occurs with in a same class while overriding occurs in the parent child classes. – Zeeshan Akhter Feb 27 '14 at 08:19

2 Answers2

23

Overriding

Java Docs says:

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed.

Overriding is a feature that is available while using Inheritance.

It is used when a class that extends from another class wants to use most of the feature of the parent class and wants to implement specific functionality in certain cases.

In such cases we can create methods with the same name and signature as in the parent class. This way the new method masks the parent method and would get invoked by default.

class Thought {
    public void message() {
        System.out.println("I feel like I am diagonally parked in a parallel universe.");
    }
}

public class Advice extends Thought {
    @Override  // @Override annotation in Java 5 is optional but helpful.
    public void message() {
        System.out.println("Warning: Dates in calendar are closer than they appear.");
    }
}

Overloading

Overloading in Java is the ability to create multiple methods of the same name, but with different parameters.

The main advantage of this is cleanliness of code.

Let's take the String.valueOf method. The overloaded versions of this method are defined as:

static String valueOf(boolean b) 
static String valueOf(char c) 
static String valueOf(char[] data) 
static String valueOf(char[] data, int offset, int count) 
static String valueOf(double d) 
static String valueOf(float f) 
static String valueOf(int i) 
static String valueOf(long l) 
static String valueOf(Object obj) 

This means that if we have any type of variable, we can get a String representation of it by using String.valueOf(variable).

If overloading was not allowed we'd have methods that look like this...

static String valueOfBoolean(boolean b) 
static String valueOfChar(char c) 
static String valueOfCharArray(char[] data) 
static String valueOfCharArrayWithOffset(char[] data, int offset, int count) 
static String valueOfDouble(double d) 
static String valueOfFloat(float f) 
static String valueOfInt(int i) 
static String valueOfLong(long l) 
static String valueOfObject(Object obj) 

...which is very ugly and harder to read than the overloaded solution.

Parth Soni
  • 11,158
  • 4
  • 32
  • 54
2

The answer primarily centers around the use of interfaces and abstract classes.

Let's say you have a block of code:

abstract class testAbstract {
    int a; int b; int c;
    public String testFunction() {
        System.out.println("Override me!");
        return "default";
    }

    public void setValuesByFormula(double a, double b) {
        System.out.println("No formula set!");
    }
}

This isn't the bet example, but let's say this is intended to allow the user to set a, b, and c from a formula. The user would like to create a new instance of this:

class testClass extends testAbstract {
}

The class testClass currently contains... nothing. But it extends testAbstract and thus has a function setValuesByFormula. When the user tries to call this, however, the system outputs the message: No formula set!

What the user must do at this point is @Override the original function. Thus, the new class becomes:

class testClass extends testInterface {
    public void setValuesByFormula(double a, double b) {
        //A not really relevant formula goes here.
    }
}

This stops the message No formula set! as the user has created their own formula. This is needed for a couple main reasons:

  1. If a function is inherited from a superclass, often the child will want to override that function. However, the superclass may have default code for its class. Thus, the user must override this function if they wish to use their own code.
  2. Often, superclasses want to ensure with default code that a certain function will return without error. There are also other instances where default code is helpful. Thus, this provides an alternative: if the user does not need the default code, they can override it.
Community
  • 1
  • 1