Geeks Talk

Prepare for your Next Interview




Difference between method overriding and overloading

This is a discussion on Difference between method overriding and overloading within the OOPS forums, part of the Software Development category; Is the concept of method overriding different from the concept of overloading. If so how they both differ....


Go Back   Geeks Talk > Software Development > OOPS

Register FAQ Members List Calendar Mark Forums Read

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2006
Contributing Member
 
Join Date: Jul 2006
Posts: 76
Thanks: 0
Thanked 4 Times in 1 Post
nancyphilips is on a distinguished road
Difference between method overriding and overloading

Is the concept of method overriding different from the concept of overloading. If so how they both differ.
Reply With Quote
The Following 4 Users Say Thank You to nancyphilips For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 07-17-2006
Junior Member
 
Join Date: Jun 2006
Location: India
Posts: 5
Thanks: 0
Thanked 4 Times in 1 Post
ruppal is on a distinguished road
Re: Difference between method overriding and overloading

Hi nancy,there is big difference between both:
see following example

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)
{
}
}
Reply With Quote
The Following 4 Users Say Thank You to ruppal For This Useful Post:
  #3 (permalink)  
Old 07-17-2006
Contributing Member
 
Join Date: May 2006
Posts: 88
Thanks: 0
Thanked 10 Times in 9 Posts
norman is on a distinguished road
Re: Difference between method overriding and overloading

Method overriding is a feature in Object Oriented programming language. This is used to implement a method for subclass which overrides in other words replaces the implementation of the super class. Overloading the concept of providing different meaning to a object based on the context of its presence. Overloading is one type of polymorphism and this is also a feature in Object Oriented programming language.
Reply With Quote
The Following User Says Thank You to norman For This Useful Post:
  #4 (permalink)  
Old 12-13-2006
Expert Member
 
Join Date: Nov 2006
Location: Hyd-IND
Posts: 523
Thanks: 1
Thanked 55 Times in 46 Posts
sutnarcha is on a distinguished road
Re: Difference between method overriding and overloading

In overriding,
The method of a sub class takes priority over its counterpart in the super-class.

where as in Overloading,
2 or more methods with same name are available (have no priority over each other) but differ in their declaration and/or definition. Either of them may be executed depending on the number and/or type of arguments passed.

Any further clarifications? Feel free to ask
Reply With Quote
  #5 (permalink)  
Old 12-13-2006
Expert Member
 
Join Date: Dec 2006
Location: Chennai
Posts: 197
Thanks: 2
Thanked 16 Times in 14 Posts
Barbie is on a distinguished road
Re: Difference between method overriding and overloading

Overriding is the concept of having functions of same name and signature in different classes. one in the super class can be made virtual and other can override the functionality of virtual one.

Overloading is the concept of having functions of same name, but different signature in same class. They are differentiated by the compiler by their signatures.
Reply With Quote
  #6 (permalink)  
Old 12-13-2006
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 76 Times in 64 Posts
psuresh1982 will become famous soon enough
Smile Re: Difference between method overriding and overloading

Using overloading and overridding, you can acheive the concept of polymorphism.

Polymorphism means "one name, multiple forms". Using one name u can do multiple of actions...

Method overloading is a compile time polymorphism and Method Overridding is a runtime polymorphism...

Compile time polymorphism means compiler knows which object assigned to which class at the compiling time....Runtime polymorphism means compiler didn't know at the compile time, it only knows at a run time...

---------------------
suresh
Reply With Quote
  #7 (permalink)  
Old 03-20-2007
Contributing Member
 
Join Date: Mar 2007
Location: renukoot(sonbhadra)
Posts: 37
Thanks: 5
Thanked 3 Times in 3 Posts
rohit dwivedi9450 is on a distinguished road
Re: Difference between method overriding and overloading

methjod overloading means one method can callto diffrent sub class of base class and in method overriding method in subclass takes priority over it's counterpart.
Reply With Quote
  #8 (permalink)  
Old 05-17-2007
Junior Member
 
Join Date: Apr 2007
Location: Bangalore , India
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
d_kanad is on a distinguished road
Re: Difference between method overriding and overloading

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
Reply With Quote
The Following User Says Thank You to d_kanad For This Useful Post:
  #9 (permalink)  
Old 05-17-2007
Junior Member
 
Join Date: Apr 2007
Location: Bangalore , India
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
d_kanad is on a distinguished road
Re: Difference between method overriding and overloading

What is overloading in java ?
In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case,the methods are said to be overloaded, and the process is referred to as method
overloading. Method overloading is one of the ways that Java implements polymorphism.

What is Overriding in java ?
when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.
Reply With Quote
  #10 (permalink)  
Old 05-24-2007
Junior Member
 
Join Date: May 2007
Location: india
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
sudhansulenka is on a distinguished road
Thumbs up Re: Difference between method overriding and overloading

overlaoding:functions having same name and same signature.It is a run time polymorphism.Eg: virtual function
overlaoding:function having same name but different signature.It is a compile time polymorphism.Eg: operator overloading and function overloading.
Reply With Quote
The Following User Says Thank You to sudhansulenka For This Useful Post:
  #11 (permalink)  
Old 07-14-2007
Junior Member
 
Join Date: Jul 2007
Posts: 20
Thanks: 7
Thanked 3 Times in 3 Posts
amaravadi.krishna81 is on a distinguished road
Re: Difference between method overriding and overloading

method overloading is designtime polymorphism whereas method overriding is runtime polymorphism..
In overloading the parameters should be uniqe i.e the no of parameters shud differ or if same no of parameters r there the signature shud differ
why because the compiler should know the methods in complintime only.
whereas in overriding we reimplement or change the functionality of the base class method in derived class .the no of parameters & return type shud be same ..
Reply With Quote
  #12 (permalink)  
Old 07-14-2007
Junior Member
 
Join Date: Jul 2007
Location: chennai
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
gnomeom is on a distinguished road
Re: Difference between method overriding and overloading

Let me give one example.

METHOD OVERLOADING

class human(
{
void Hand(two paremeters){
Eating}
void Hand(three parameters) {
Writing }
void Hand(Five parameters) {
Fighting }

METHOD OVERIDING

class father {

void Hand(5 parameters){
Smoking;
}
}
class child extends father{
void Hand(5 parameters){
Drinking}
Thanks
Reply With Quote
  #13 (permalink)  
Old 07-15-2007
Junior Member
 
Join Date: Jul 2007
Location: Banglore
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ShivaB is on a distinguished road
Thumbs up Re: Difference between method overriding and overloading

Quote:
Originally Posted by nancyphilips View Post
Is the concept of method overriding different from the concept of overloading. If so how they both differ.
OverLoading : when u want extend ur feature then u will use OverLoading.
OverRiding : When u dont want to use the existing feature then u will use OverRiding
Reply With Quote
  #14 (permalink)  
Old 07-20-2007
Expert Member
 
Join Date: Jul 2007
Location: Kolkata
Posts: 110
Thanks: 6
Thanked 8 Times in 8 Posts
animesh.chatterjee is on a distinguished road
method overriding and ovverloading

Overriding method definitions
In a derived class, if you include a method definition that has the same name and exactly the same number and types of parameters as a method already defined in the base class, this new definition replaces the old definition of the method.

Explanation
A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the methods defined in the superclass. This is referred to as method overriding. The following example demonstrates method overriding.

Step 1
In this example we will define a base class called Circle

class Circle {

//declaring the instance variable
protected double radius;

public Circle(double radius) {
this.radius = radius;
}

// other method definitions here

public double getArea() {
return Math.PI*radius*radius;
}//this method returns the area of the circle

}// end of class circle

When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.

Step 2
The next step is to define a subclass to override the getArea() method in the Circle class. The derived class will be the Cylinder class. The getArea() method in the Circle class computes the area of a circle, while the getArea method in the Cylinder class computes the surface area of a cylinder.

The Cylinder class is defined below.

class Cylinder extends Circle {

//declaring the instance variable
protected double length;

public Cylinder(double radius, double length) {
super(radius);
this.length = length;
}

// other method definitions here

public double getArea() { // method overriden here
return 2*super.getArea()+2*Math.PI*radius*length;
}//this method returns the cylinder surface area

}// end of class Cylinder

When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass(Circle).

Example code
This is the code to instantiate the above two classes

Circle myCircle;
myCircle = new Circle(1.20);

Cylinder myCylinder;
myCylinder = new Cylinder(1.20,2.50);

Please let me know if you need anything more
Reply With Quote
  #15 (permalink)  
Old 07-27-2007
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,432
Thanks: 8
Thanked 126 Times in 113 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: Difference between method overriding and overloading

Some of the threads are merged for better management of the site.

MODERATOR
Reply With Quote
  #16 (permalink)  
Old 12-14-2007
Junior Member
 
Join Date: Mar 2006
Location: Mumbai
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Ankit Vasavada is on a distinguished road
Re: Difference between method overriding and overloading

Overriding is the Functionality by witch a programmer can change the old functionality of any function even if he does`t know that what was the old one.
But in Overloading you have to change the parameter or the return type. this is the Conceptual difference
Reply With Quote
  #17 (permalink)  
Old 12-18-2007
Contributing Member
 
Join Date: Nov 2007
Location: bangalore
Posts: 54
Thanks: 6
Thanked 3 Times in 3 Posts
rahulvegi is on a distinguished road
Thumbs up Re: Difference between method overriding and overloading

according to overriding:

mean - overriding the previous text with new text
accessing:

if u want to access this method from the super class to sub class that must be same return type and same method name.

example

class Superclass{
void display()
{
System.out.println("this is super class display");
}
}
class Subclass extends Superclass{
void display()
{
System.out.println("this is sub class display");
}
}
class Myclass{
public static void main(String args[])
{
subclass s=new subclass();
s.display();
}
output:
this is subclass display
explanation:
the subclass method display() simply overrided the method in superclass.

problems:these steps must be follow for overriding the method

-return type must be same
-access specifier must be same
-method name is equal in both classes.

according to overloading:

one name with different arguments and the return type may be any one.
overloading the methods in the same class possible.

example:
void display();
int display(int, int);
double display(double,double);
Reply With Quote