Overloading example ?

Posted by Naga116 under C# on 8/4/2011 | Points: 10 | Views : 1692 | Status : [Member] | Replies : 4
Can you please help in this below code will execute correctly.
Can we overload same signature but different return types? is it possible.

public class a
{
public int add(int a, int b){return a+b;}
}

public class b:a
{
public float add(int a, int b) { return a + b; }
}

and
public class a
{
public int add(int a, int b){return a+b;}
public float add(int x,int y){return x+y;}
}

Nagaraju


Responses

Posted by: SheoNarayan on: 8/4/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
I have changed your code like this (instead of returning float I am returning string just to avoid confusion)

public class AddMy

{
public int add(int a, int b) { return a + b; }
}

public class AadDMy : AddMy
{
public string add(int a, int b) { return "Yes: " + (a + b); }
}


and calling like below

 AddMy ad = new AddMy();

Response.Write(ad.add(5, 7).ToString());

AadDMy add = new AadDMy();
Response.Write(add.add(5, 30).ToString());


and it is working perfectly, so it is possible.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasibabu on: 8/4/2011 [Member] Starter | Points: 25

Up
0
Down


Can we overload same signature but different return types? is it possible.

yes it is possible.

here we have 2 classes a(class) and b(class)

if you create object to the a (class) ----method add(int a,int b) is invoked and returns integer value
and
if you create object to the b(class)-----method add(int a,int b) is invoked and returns float value

overloading is possible in derived classes are:

1. number of parameters in the signature of the method.
2. order of the parameters in the signature of the method.

Polymorphism : the main objective of polymorphism is giving multiple behaviors to single method.

Inheritence:code reuseablity.

overloading main objective is giving new functionality to derived class method (of parent class).

advantage is performance.

public class a
{
public int add(int a, int b){return a+b;}
}

public class b:a
{
public float add(int a, int b) { return a * b; }
}

i was given new functionality to b class.
now this is real need of the overriding a method in derived class.

conclusion : if parent class ( a class) functionality is not suitable to our requirement. write new functionality in derived class.


if it is helpful please marked as answer

kasi babu
Software Engineer.





Regards,
Kasi babu

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down
Well in case of same signature but return type is different, its a case of shadowing.

so though its working but not as a concept of Overloading but as a concept of shadowing.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down
pls refer this..
http://msmvps.com/blogs/luisabreu/archive/2007/09/06/c-overloading-methods-by-return-type-is-not-permitted.aspx

A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.
For more details refer
http://msdn.microsoft.com/en-us/library/ms173114(v=VS.90).aspx
http://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx


Nitha Deepak

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response