top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Difference between method overloading and overriding ?

+1 vote
740 views
Difference between method overloading and overriding ?
posted Jan 7, 2015 by Neeraj Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Function overloading and overriding has been already introduced in C++ . Java is a pure object oriented programming language and functions are considered methods. Method overloading is similar to function overloading of c++. Only same class's methods can be overloaded by using different number and types of parameters but with same function name and return type. Now comes to method overriding, method overriding is applicable in the case of base and child class. For the method overriding, the method signature should be same but return type may be different.

Example of method overloading:
class sum
{
int add (int i, int j)
{
return i+j;
}
int add (int i, int j, int k)
{
return i+j+k;
}

public static void main(String args[])
{

} 

}

Example of method overriding:
class base
{
public int speedLimit()
{
return 100;
}
}

class child extends base
{
public int speedLimit()
{
return 150;
}

public static void main(String args[])
{

}

}

answer Jan 8, 2015 by Vikram Singh
0 votes

Method overloading happens in the same class shares the same method name but each method should have different number of parameters or parameters having different types and order. But in method overriding derived class have the same method with same name and exactly the same number and type of parameters and same return type as a parent class. More about.....Difference between method overloading and method overriding

Nick

answer Mar 30, 2016 by Vaynenick
...