Flipkart Search

Search This Blog

Wednesday, March 18, 2009

Overloading Vs Overriding

overloading comes with in class for example :

Operator Overloading and Method overloading.

void Add( int i );
void Add( char c );

These two function declaration and definition in the same class is called method OVERLOADING.


overriding comes with two class ( base and drived class )

function in the base class is like

public virtual void Display();

function in the drived class is like

public override void Display();

this function overrides the base class .

I think you should know more on OOPs , Polimorphism , inheritance etc.



Author: Kamliesh Nadar 17 May 2005Member Level: GoldRating: Points: 2

Hi,

Overriding - same method names with same arguments and same return types associated in a class and its subclass.

Overloading - same method name with different arguments, may or may not be same return type written in the same class itself.

Example for overriding
Clas A
{
Virtual void hi(int a)
{
}
}

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

}
}

Example for Over loading

Class A
{
class a()

{

}
class a(int a)
{
}
}

Regards,



Author: M.Ravi Kishore 18 May 2005Member Level: SilverRating: Points: 2

OverLoading means one name many forms .its functionallty changes corresponding to the objects
Overriding means its funcationality can be overrided.these can be done by using by using two keywords
1.overrideable
2.overrides
overriding take place with in the in_heritance. it is not possiable with in the class.
have u got it if not contact me(ravikim2001@yahoo.co.in)
ok bye.



Author: Maheswara Reddy 13 Jun 2007Member Level: BronzeRating: Points: 2

To begin with, overloading has been used before in other languages such as C++. If you have any C++ experience with overloading operators, this will be a snap. If not, don't worry; once you see a couple examples you'll be able to figure out where things are headed.

Method Overriding

Overriding in C# makes use of the "override" keyword. To override a method means to replace it with a new way of handling data. Here's an example of what I mean:

Say we have an object that contains data for a rectangle. Our object has accessors and a method for returning the area. This is our Rectangle object (refer to Accessors in C# for help understanding what this object does):


public class Rectangle
{
public int Width
{
set { width = value; }
get { return width; }
}

public int Height
{
set { height = value; }
get { return height; }
}

public int Area
{
get { return (width * height); }
}

int width, height;
}



Now for the overriding… The easiest, and most common, use of overriding is with the ToSting() method. ToString is a built-in method that returns a string, generally referring to some aspect of the object, to the calling statement. Let's look at the following Main() method used to call Rectangle:


class TestRectangle
{
public static void Main()
{
Rectangle myRect1 = new Rectangle();

myRect1.Width = 3;
myRect1.Height = 5;

Console.WriteLine("The area of a {0}, width={1} and height={2} is: {3}.",
myRect, myRect.Width, myRect.Height, myRect.Area);
}
}



The exact output for this is:
"The area of a Rectangle, width=3 and height=5 is: 15."

The first thing you should be asking is how the word "Rectangle" gets in there? This is where ToString() comes in. As with JAVA, when you try to reference an object as a string, it automatically calls ToString(); it'd be like calling myRect.ToString(). When we call myRect with Console.WriteLine(), it outputs myRect as a string. An un-overridden object will return the type of object it is, "Rectangle" in this case. Perhaps instead to returning "Rectangle," though, we want it to return "cool rectangle." To do this we must override ToString() in our object. Adding the following method to the Rectangle class will do this:


public override string ToString()
{
return "cool rectangle";
}



The only change between this method and any other method is the use of the override keyword. It's as simple as that. Now when the Main() method is run, the output will be:
"The area of a cool rectangle, width=3 and height=5 is: 15."

Overriding ToSting() is very useful if your class contains a list of information and you want to return it with some formatting already done. For example, you could create a string that formats a list of names for output similar to:

Name 1: Kevin
Name 2: Brittany
Name 3: Joseph
etc…

Then when your object (i.e. myNameList) is referenced as a string, it’ll return your formatted list of names.

Operator Overloading
OK, so overriding methods can be fun, but what about other things? Well let’s say we want to add two rectangles. Now the common way would be to add their areas, but maybe we want to add the lengths and the widths and find the new area. We could write a method that'd find this special area; but, if this is the only way we plan on adding two rectangles, we can easily overload the '+' operator. Here are our two rectangles:


-- -- -- -- -- -- -- -- --
| | | |
| | | |
| | -- -- -- -- -- --
| |
-- -- --
Width: 3 Width: 6
Height: 4 Height: 2
Area: 12 Area: 12



Doing our new form of adding, the resulting rectangle should be:


-- -- -- -- -- -- -- -- --
| | |
| |_________________|
| |__|__|__| |
|________|__|__|__| |
| | |
| | |
-- -- -- -- -- -- -- -- --
Width: 9
Height: 6
Area: 54



To begin, let's create the method header. We know we want to return a new rectangle with the new dimensions. To simplify things, we're going to take in the two small rectangles and return the composite rectangle. Our header will look as follows:


public static Rectangle operator + (Rectangle nRect1, Rectangle nRect2);



You should be able to figure out what each part is doing here. The return type is a Rectangle, the operator keyword tells that we're overloading an operator, and the operator is the + symbol; the method is also taking in our two small rectangles. Here's what the entire method might look like:


public static Rectangle operator + (Rectangle nRect1, Rectangle nRect2)
{
Rectangle retRect = new Rectangle();

retRect.Width = nRect1.Width + nRect2.Width;
retRect.Height = nRect1.Height + nRect2.Height;

return retRect;
}



I could have addressed width and height directly, but it is safer to use the accessors because I may have performed some special operation on the data with them (in this case I didn't so it wouldn't matter either way).

Putting the new Rectangle class together along with a testing Main() method, we'd have:


using System;

namespace Overloading
{
class TestRectangle
{
public static void Main()
{
Rectangle myRect1 = new Rectangle();
Rectangle myRect2 = new Rectangle();
Rectangle myRect3 = new Rectangle();

myRect1.Width = 3;
myRect1.Height = 4;

myRect2.Width = 6;
myRect2.Height = 2;

myRect3 = myRect1 + myRect2;

Console.WriteLine("The area of a {0}, width={1} and height={2} is: {3}.",
myRect1, myRect1.Width, myRect1.Height, myRect1.Area);
Console.WriteLine("The area of another {0}, width={1} and height={2} is: {3}.",
myRect2, myRect2.Width, myRect2.Height, myRect2.Area);
Console.WriteLine("The result of the {0}s added is: width={1}, height={2}, area={3}.",
myRect3, myRect3.Width, myRect3.Height, myRect3.Area);
}
}

public class Rectangle
{
public int Width
{
set { width = value; }
get { return width; }
}

public int Height
{
set { height = value; }
get { return height; }
}

public int Area
{
get { return (width * height); }
}

public static Rectangle operator + (Rectangle nRect1, Rectangle nRect2)
{
Rectangle retRect = new Rectangle();

retRect.Width = nRect1.Width + nRect2.Width;
retRect.Height = nRect1.Height + nRect2.Height;

return retRect;
}

public override string ToString()
{
return "cool rectangle";
}

int width, height;
}
}

Virtual and Overridden Methods

Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.

using System;
namespace Polymorphism
{
class A
{
public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
public override void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "B::Foo()"
}
}
}

Method Hiding

Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:

using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
public new void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}

Combining Method Overriding and Hiding

Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:

class A
{
public void Foo() {}
}

class B : A
{
public virtual new void Foo() {}
}


A class C can now declare a method Foo() that either overrides or hides Foo() from class B:

class C : B
{
public override void Foo() {}
// or
public new void Foo() {}
}

Hope you can get complete idea.
Still you will get doubts on the same ,don't hesitate to send mail to Maheshreddy245@gmail.com

Cheers
Mahesh Reddy



Author: shivshankeryadav 13 Mar 2008Member Level: BronzeRating: Points: 2

Hi...
Overloading and overriding both are concerned with the oops. Overloading means same method name but different parameters, call depends upon the no. and types of parameters. While Overriding means changing the body of the method in the derived class.

Let me explain...
Suppose there is a class A having a method named alert(), and a derived class named B, now when we create an object of and call the method, the method os class A is called always. Now we want that we call the same method but behave differently, then we override it in the derived class. for this we use virtual keyword in the base class and override in the derived class to override and then change the body as per our need.

now when we create an object and call the method, the method of derived class will be caleed instead of the base class, This is the overriding.



Author: chakravarthy 17 Apr 2008Member Level: SilverRating: Points: 2

OverLoading is different from Overriding

Overloading comes when you define two methods or morethan two methods with the same name in the same class with different signature.

Overriding comes when you redefine a method that has already been defined in a parent class with ther same signature.

Overloading is resolved at compiletime
Overriding is resolved at runtime.

simply we can tell like Overloading deals with multiple methods in the same class with the same name but different signature
where as overriding deals with two methods one is in parent class and another one is in derived class that have the same signature, means when you override, you change the method behaviour for a child class.


Here is the Example
Public class A{
int a,b;
public int add(){
return a+b;
}
public float add(){
return a+b;
}
public int add(int i, int j){
int i+j;
}
}
in the above example shows add method is having the different signature first method doesn't take any parameter second methos returns the different floating value and thord one takes 2 parameters
in Overriding
public class BaseHide {
public BaseHide() {

}

public virtual string OverrideMe() {
return "Base Class Override Me";
}
}
public class DerivedHide : BaseHide {
public DerivedHide() {

}
public override string OverrideMe() {
return "Derived Class Override Me";
}
}
class Program {
static void Main(string[] args) {

DerivedHide derivedHideOverrideObject = new DerivedHide();
Console.WriteLine( derivedHideOverrideObject.OverrideMe());

BaseHide baseHideOverrideObject = new DerivedHide();
Console.WriteLine( baseHideOverrideObject.OverrideMe()); }
}



Author: asdf 12 Aug 2008Member Level: SilverRating: Points: 0

• Overloading
Having another method in the same name within the class. But arguments will be vary


• Overriding
Change the behaviour of the method of the derived class by the key word Override. But base class the same method should be used the keyword Virtual.



Author: Sridhar R 20 Feb 2009Member Level: DiamondRating: Points: 4

Overloading..
1.Same name but there are different definitions and parameters..
2.Here, the definitions are extented.
3.Seperate methods share the same name.
4.It is mainly for operators.
5.It must have different method signatures.

Overriding.
1.Here replacement of methods.
2.It is used in inheritance.
3.subclass methods replaces the superclass.
4.It is mainly for functions.
5.It must have same signature

Verify these links,

http://www.allinterview.com/showanswers/241.html
http://wiki.answers.com/Q/Difference_between_overriding_and_overloading
http://en.allexperts.com/q/C-1040/Difference-Overriding-Overloading.htm
http://answers.yahoo.com/question/index?qid=20060924201736AA1Qs5C

Nothing is illegal, Until You Get Caught

No comments: