Interface Inheritance
-
Hi i am inheriting two interfaces A, B which has a method with common name called codeproject. When i am trying to call that method which interface method does it call? or how can we call a patricular method. interface A { public void codeproject(); } interface B { public void codeproject(); } public class code : A, B { } code c = new code(); c.codeproject() // which method is it calling ? what do i need to do call a particular method? Santhapur
-
Hi i am inheriting two interfaces A, B which has a method with common name called codeproject. When i am trying to call that method which interface method does it call? or how can we call a patricular method. interface A { public void codeproject(); } interface B { public void codeproject(); } public class code : A, B { } code c = new code(); c.codeproject() // which method is it calling ? what do i need to do call a particular method? Santhapur
neither as the codeproject method is implemented in the class, not the interface. you can implement each interface explicitly if you wish by using
public class code : A, B
{void A.codeproject() { } void B.codeproject() { }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
neither as the codeproject method is implemented in the class, not the interface. you can implement each interface explicitly if you wish by using
public class code : A, B
{void A.codeproject() { } void B.codeproject() { }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
I think you are misunderstanding interfaces. An interface has no implementation. c.CodeProject simply calls the method
CodeProject()
in the classcode
- nothing is ever 'called' in an interface.Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
I think you are misunderstanding interfaces. An interface has no implementation. c.CodeProject simply calls the method
CodeProject()
in the classcode
- nothing is ever 'called' in an interface.Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
Try this and see if the previous answer makes any more sense:
using System;
interface IA
{
// There is no code here. Just a "requirement" that you need // to implement a method called CodeProject().
void CodeProject();
}interface IB
{
// IB also says that you are required to implement a method called CodeProject().
void CodeProject();
}public class Code : IA, IB
{
// By inheriting from IA (or IB), what you are saying is that // you are required to implement a method called CodeProject(). // IA and IB just happen to have the same requirement. // Here it is.
public void CodeProject()
{
Console.WriteLine("This is the CodeProject() method.");
}
}class Program
{
static void Main(string[] args)
{
Code c = new Code();
c.CodeProject();
}
}A couple of quick notes: Interfaces are named with a leading letter "I", by convention (for example, IDisposable). Methods defined in interfaces are automatically public. You cannot use the
public
modifier in the interface/method definition. Enjoy, Robert C. Cartaino -
Try this and see if the previous answer makes any more sense:
using System;
interface IA
{
// There is no code here. Just a "requirement" that you need // to implement a method called CodeProject().
void CodeProject();
}interface IB
{
// IB also says that you are required to implement a method called CodeProject().
void CodeProject();
}public class Code : IA, IB
{
// By inheriting from IA (or IB), what you are saying is that // you are required to implement a method called CodeProject(). // IA and IB just happen to have the same requirement. // Here it is.
public void CodeProject()
{
Console.WriteLine("This is the CodeProject() method.");
}
}class Program
{
static void Main(string[] args)
{
Code c = new Code();
c.CodeProject();
}
}A couple of quick notes: Interfaces are named with a leading letter "I", by convention (for example, IDisposable). Methods defined in interfaces are automatically public. You cannot use the
public
modifier in the interface/method definition. Enjoy, Robert C. CartainoOops, too slow. Already answered...