Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi

what is the difference between MethodOverriding and Overloading ?tell me the situations where we can use both with example.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Nov-12 23:44pm    
They are totally unrelated, so the question is totally incorrect. The whole approach to learning is awfully wrong. Just read on both concepts and ask if you failed to understand it.
--SA
[no name] 19-Nov-12 23:46pm    
It's correct question.
why you are saying always it's not correct question, don't do like this.
If u know answer then post correct solution otherwise leave it.
Narendrareddy07 20-Nov-12 2:23am    
kk thank u .....plz tell me some real time scenarios on those ....

1 solution

CSS
Overloading methods
1.They appear in the same class or a subclass
2.They have the same name but, have different parameter lists, and can have different return types.
An example of an overloaded method is print() in the java.io.PrintStream class

Overriding methods
It allows a subclass to re-define a method it inherits from it's superclass
overriding methods:
1. It appears in subclasses
2. They have the same name as a superclass method
3. They have the same parameter list as a superclass method
4. They have the same return type as as a superclass method
5. They have the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
If the superclass method is public, the overriding method must be public
If the superclass method is protected, the overriding method may be protected or public
If the superclass method is package, the overriding method may be packagage, protected, or public
If the superclass methods is private, it is not inherited and overriding is not an issue



When overriding, you change the method behavior for a derived class. 
e.g Clas A
{
Virtual void hi(int a)
{
}
}

Class B:A
{
public overrid void hi(int a)
{

}
}

Overloading simply involves having a method with the same name within the class. 

Example for Over loading

Class A
{
class a()

{

}
class a(int a)
{
}
}
 
Share this answer
 
Comments
Narendrareddy07 20-Nov-12 1:25am    
plz tellme the realtime scenarios where we use thes two concepts ? .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900