My question is about overloading , can anyone tell me that is overloading possible in inheritance or not ? because when we declare a method in parent class let suppose xyz is my method and its return type is "int". if we declare the same method name in
the child class but its return type is string. This is possible we can do this but my question is "can we call this is overloading ?".
The answer is No. thats is function overloading means (as per wiki) "allows creating several
methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different task".
In your case you are divided that function in 2 diff classes. So its not come under the overloading concept.
Thanks,
Happy to Help!
Click "…Mark As Answer" if my reply helpful to you..
A2Z DotNet
because when we declare a method in parent class let suppose xyz is my method and its return type is "int". if we declare the same method name in the child class but its return type is string
In my word, method overload means that two methods have the same name, but have different amount of the parameters or have different type of the parameters .
For example:
public class A
{
public int Add(int a, int b)
{
return a+b;
}
public int Add(int a, int b, int c)
{
return a+b+c;
}
// those two methods are overloading
}
public class B
{
public string Add(int a, int b)
{
return a+b;
}
public string Add(string a, int b)
{
return a+b;
}
// those two methods are overloading
}
public class C
{
public string Add(int a, int b)
{
return a+b;
}
public int Add(int a, int b)
{
return a+b;
}
// those two methods are not overloading
}
Best Regards,
Amy Peng
Please mark the replies as answers if they help or unmark if not.
Feedback to us
"allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different task".
Thanks,
Hi francissvk,
I am sorry for the late reply.
Overload methods can have the same type of the output, but the different in the type and amount of the input.
Best Regards,
Amy Peng
Please mark the replies as answers if they help or unmark if not.
Feedback to us
I just create a simple example, please try to refer to:
namespace test
{
class A
{
public int add(int a, int b)
{
return (a + b);
}
}
class B : A
{
public string add(string a)
{
return a;
}
}
class Program
{
static void Main(string[] args)
{
A a1 = new A();
B b1 = new B();
b1.add("abc");
b1.add(1, 2);
a1.add(1, 2);
A a2 = new B();
a1.add(1,2);
}
}
}
Best Regards,
Amy Peng
Please mark the replies as answers if they help or unmark if not.
Feedback to us
my question is very simple and straight forward, overloading is possible in inheritance or not and if possible than what we will say ? because i read it that overloading is possible in same class but i practically do that overloading is also possible in
inheritance .
usman.eng89
Member
34 Points
34 Posts
is overloading possible in inheritance or not ?
Jan 05, 2013 08:15 PM|LINK
Hi ,
My question is about overloading , can anyone tell me that is overloading possible in inheritance or not ? because when we declare a method in parent class let suppose xyz is my method and its return type is "int". if we declare the same method name in the child class but its return type is string. This is possible we can do this but my question is "can we call this is overloading ?".
Thanks .
francissvk
Participant
1010 Points
343 Posts
Re: is overloading possible in inheritance or not ?
Jan 06, 2013 02:36 AM|LINK
Hi,
The answer is No. thats is function overloading means (as per wiki) "allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different task".
In your case you are divided that function in 2 diff classes. So its not come under the overloading concept.
Thanks,
Click "…Mark As Answer" if my reply helpful to you..
A2Z DotNet
Amy Peng - M...
Star
10123 Points
958 Posts
Microsoft
Re: is overloading possible in inheritance or not ?
Jan 12, 2013 07:23 AM|LINK
Hi,
In my word, method overload means that two methods have the same name, but have different amount of the parameters or have different type of the parameters .
For example:
public class A { public int Add(int a, int b) { return a+b; } public int Add(int a, int b, int c) { return a+b+c; } // those two methods are overloading } public class B { public string Add(int a, int b) { return a+b; } public string Add(string a, int b) { return a+b; } // those two methods are overloading } public class C { public string Add(int a, int b) { return a+b; } public int Add(int a, int b) { return a+b; } // those two methods are not overloading }Best Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store
francissvk
Participant
1010 Points
343 Posts
Re: is overloading possible in inheritance or not ?
Jan 12, 2013 05:10 PM|LINK
Hi Amy,
Is my words are correct on this ?
Thanks,
Click "…Mark As Answer" if my reply helpful to you..
A2Z DotNet
usman.eng89
Member
34 Points
34 Posts
Re: is overloading possible in inheritance or not ?
Jan 13, 2013 08:12 PM|LINK
Fransissvk why this is not coming under overloading concept ? i practically do that and visual studio said 1+function overloaded . what is this ?
Amy Peng - M...
Star
10123 Points
958 Posts
Microsoft
Re: is overloading possible in inheritance or not ?
Jan 14, 2013 12:56 AM|LINK
Hi francissvk,
I am sorry for the late reply.
Overload methods can have the same type of the output, but the different in the type and amount of the input.
Best Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store
Amy Peng - M...
Star
10123 Points
958 Posts
Microsoft
Re: is overloading possible in inheritance or not ?
Jan 14, 2013 01:13 AM|LINK
Hi usman.eng80,
I just create a simple example, please try to refer to:
namespace test { class A { public int add(int a, int b) { return (a + b); } } class B : A { public string add(string a) { return a; } } class Program { static void Main(string[] args) { A a1 = new A(); B b1 = new B(); b1.add("abc"); b1.add(1, 2); a1.add(1, 2); A a2 = new B(); a1.add(1,2); } } }Best Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store
usman.eng89
Member
34 Points
34 Posts
Re: is overloading possible in inheritance or not ?
Jan 14, 2013 05:07 AM|LINK
Hi AMY,
my question is very simple and straight forward, overloading is possible in inheritance or not and if possible than what we will say ? because i read it that overloading is possible in same class but i practically do that overloading is also possible in inheritance .
Amy Peng - M...
Star
10123 Points
958 Posts
Microsoft
Re: is overloading possible in inheritance or not ?
Jan 14, 2013 08:29 AM|LINK
Hi,
In my mind, it is not possible.
Overloading is in the same class.
How do you do the overloading in inheritance?
Best Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store
francissvk
Participant
1010 Points
343 Posts
Re: is overloading possible in inheritance or not ?
Jan 14, 2013 12:11 PM|LINK
Hi Amy,
I have also want to convey the same in my previous post.
Usman, confuse with the overloading and inheritance.
Thanks.
Click "…Mark As Answer" if my reply helpful to you..
A2Z DotNet