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.
Yes it is possible, you can overload and override a function in the same class but you would not be able to overload a function in two different classes as it is logically not possible.
Method Overloading is used to implement Compile time or static polymorphism. Method Overriding is used to implement Runtime or dynamic polymorphism. It is used to expand the readability of the program. The number of parameters and type of each parameter must be the same in case of method overriding.
Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
Method overloading increases the readability of the program. This provides flexibility to programmers so that they can call the same method for different types of data.
From doc
Method Overloading: Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, drawString, drawInteger, drawFloat, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list.
public class DataArtist {
...
public void draw(String s) {
...
}
public void draw(int i) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...
}
}
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s)
and draw(int i)
are distinct and unique methods because they require different argument types.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With