Difference Between Method Overloading and Overriding in Java PDF
Difference Between Method Overloading and Overriding in Java PDF
In this tutorial we will discuss the difference between overloading and overriding in Java. If you are
new to these terms then refer the following posts:
Overloading example
//A class for adding upto 5 numbers
class Sum
{
int add(int n1, int n2)
{
return n1+n2;
}
int add(int n1, int n2, int n3)
{
return n1+n2+n3;
}
int add(int n1, int n2, int n3, int n4)
{
return n1+n2+n3+n4;
}
int add(int n1, int n2, int n3, int n4, int n5)
{
return n1+n2+n3+n4+n5;
}
public static void main(String args[])
{
Sum obj = new Sum();
System.out.println("Sum of two numbers: "+obj.add(20, 21));
System.out.println("Sum of three numbers: "+obj.add(20, 21, 22));
System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23));
System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24));
}
}
Output:
Here we have 4 versions of same method add. We are overloading the method add() here.
Overriding example
package beginnersbook.com;
class CarClass
{
public int speedLimit()
{
return 100;
}
}
class Ford extends CarClass
{
public int speedLimit()
{
return 150;
}
public static void main(String args[])
{
CarClass obj = new Ford();
int num= obj.speedLimit();
System.out.println("Speed Limit is: "+num);
}
}
Output:
Here speedLimit() method of class Ford is overriding the speedLimit() method of class CarClass.
Comments
Reply
Sujitha says
NOVEMBER 15, 2014 AT 5:50 AM
Reply
Mariyappa says
NOVEMBER 7, 2014 AT 8:11 AM
Reply
Yes It is.
Reply
jemima says
NOVEMBER 26, 2014 AT 3:45 PM
hi sir,
I’m not sure whether I’m correct i studied both method overloading and overridding then
I’m studying there difference..i think the 7th point is incorrect.. it states “Return type of
overloaded method should be same as the other methods of the same name however
return type of overriding method can be different from overridden method.” and i think it
must be ” the return type of overloading methods can differ but the return type of
overriding methods should be the same..”… if im wrong plz help me to identify my
mistake .
thank u so much
Reply
Hello Jemima,
Point#7 is correct, overridden methods can have different return types. You can
refer http://stackover ow.com/questions/14694852/can-overridden-methods-
differ-in-return-type.
Reply
Kelele says
NOVEMBER 30, 2014 AT 8:04 AM
Reply
RAJ says
DECEMBER 17, 2014 AT 12:31 PM
For the 7th point `Return type of overloaded methods should be same`. Is it always
necessary for methods to be overloaded?
What about this example?
Reply
vishal says
DECEMBER 30, 2014 AT 12:09 PM
what is the use of super keyword while we are overiding a function can you please
explain..
Reply
Reply
Yogesh says
JANUARY 14, 2015 AT 8:19 AM
I like ur explanation ……
Reply
pragati says
MARCH 4, 2015 AT 1:31 PM
thanks sir
Reply
Arpitha says
APRIL 9, 2015 AT 10:26 AM
Can u please explain me 7th point ? I think it is wrong right? return type doesn’t matter
in case of overloading and return type should be same for method overriding.
Reply
Abiola says
OCTOBER 29, 2015 AT 2:09 PM
nice way of teaching cant believe it. am learning java just like that.
Reply
plz answer
y cant we write it as
carclass obj = new car ();
Reply
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
POST COMMENT
Java Tutorial
Java Index
Java Introduction
Variables
Data Types
Operators
Java Control
Statements
Java If-else
Java Switch-Case
Continue statement
break statement
OOPs Concepts
OOPs Concepts
Constructor
Static keyword
Inheritance
Types of inheritance
Aggregation
Association
Super Keyword
Method overloading
Method overriding
Overloading vs
Overriding
Polymorphism
Types of
polymorphism
Interface
Abstract class vs
interface
Encapsulation
Packages
Garbage Collection
Inner classes
Static import
Static constructor
Java Interview Q
MORE ...
Java 8 Features
Java 9 Features
Java Conversion
Java String
Exception handling
Java Multithreading
Java I/O
Java Serialization
Java Regex
Java AWT
Java Swing
Java Enum
Java Annotations