Method overloading and polymorphism Revisited?

No. Your foo methods expect either Class1 or Class2 You're passing an IInterface which could be either Class1 or Class2 It won't compile because the compiler doesn't know which overloaded method to call. You would have to cast it to one of the types to get it to compile Alternatively IInterface would hold the foo method that took no arguments and returned which class it was.

Then something could take the IInterface object and call obj.foo() Like: interface IInterface { public string foo(); } class Class1 : IInterface { public string foo() { return "Class1"; } } class Class2 : IInterface { public string foo() { return "Class2"; } } public void Something(IInterface obj) { return obj.foo(); }.

No. Your foo methods expect either Class1 or Class2. You're passing an IInterface which could be either Class1 or Class2.It won't compile because the compiler doesn't know which overloaded method to call.

You would have to cast it to one of the types to get it to compile. Alternatively, IInterface would hold the foo method that took no arguments and returned which class it was. Then something could take the IInterface object and call obj.foo() Like: interface IInterface { public string foo(); } class Class1 : IInterface { public string foo() { return "Class1"; } } class Class2 : IInterface { public string foo() { return "Class2"; } } public void Something(IInterface obj) { return obj.foo(); }.

Wouldn't that be return obj.foo(); instead of return foo(obj); – Erik Noren Jul 29 at 22:31 Whoops, sure is! Thanks for pointing that out. – Christopher Currens Jul 29 at 22:32 This worked.

I was just playing around with trying to removing conditional logic on a service call, this is what I ended up doing. – John Farrell Jul 29 at 23:50.

Public string Something (IInterface obj) { return foo ((Class1)obj); OR // There's no way compiler to know with method to call. Return foo ((Class2)obj); } public string foo (Class1 bar) { return "Class1"; } public string foo (Class2 bar) { return "Class2"; }.

This just fixes the compile but doesn't accomplish the goal. – Erik Noren Jul 29 at 22:34 1 @Erik. Whats your goal?

You don't mention. – Randolf Rincón-Fadul Jul 29 at 22:35 1 I have no goal but the original poster seems confused on why the code posted will not work. I thought this answer was not particularly helpful as it is.It doesn't explain why the original poster's code won't work, how it could be re-factored to work.

– Erik Noren Jul 29 at 22:56.

No, where non-virtual calls go is determined at compile time, so you can't call different methods depending on the type at runtime. What you might be looking for is virtual methods: public void Start () { Class1 x = new Class1(); string s = Something(x); Console.ReadKey(); } public string Something (BaseClass obj) { return obj.foo(); } abstract class BaseClass { abstract string Foo(); } class Class1 : BaseClass { public override string foo () { return "Class1"; } } class Class2 : BaseClass { public override string foo () { return "Class2"; } }.

Your foo methods expect either Class1 or Class2. You're passing an IInterface which could be either Class1 or Class2. It won't compile because the compiler doesn't know which overloaded method to call.

You would have to cast it to one of the types to get it to compile. Alternatively, IInterface would hold the foo method that took no arguments and returned which class it was.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions