Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
namespace InterFaceTest
{
   abstract  class abs
   {
       public abstract  void Display();
   }

    interface ITest
    {
        void Display();
    }
    class Program : abs, ITest
    {
        public  override  void  Display()
        {
Console.WriteLine("Why only Once");
        }
        static void Main(string[] args)
        {
        }
    }
}





why only one Display Method is needed to define here.even InterFace and Abstract Class both have the Display Mehtod
Posted

1 solution

To access the method in the interface ITest, you need to do an explicit implementation otherwise both the base class and the interface will be happy to use the same method. See Explicit Interface Implementation[^]
C#
void ITest.Display()
{
    Console.WriteLine("Because.");
}
 
Share this answer
 
v2
Comments
siddharth629 20-Sep-14 7:23am    
sir,

please provide some basic idea what happen behind the seen when i am implementing the only one Display method. and code is compile and run well and good .but not asking about other Dispaly() method .as we know that there compulsory to Implement the Method of Abstract AND Interface when ever we extend or Implement in our class
George Jonsson 20-Sep-14 8:48am    
Did you read the documentation in the link I provided?
It explains it quite well there.
BillWoodruff 20-Sep-14 12:38pm    
+5 I do note that in this case only one inherited class is an Interface; that's a little different from the inherit from multiple-interfaces scenario. And, the abstract class could provide an implementation (body), although, in this case, it does not.

I know you could use either 'New or 'Override to satisfy the compiler you'd implemented the method signature defined in 'abs. If the abstract class did provide an implementation (which could be signed as 'virtual), you could invoke it in the over-ridden, or 'new signed Display method by using: base.Display();

I am not sure what the compiler does differently in the case of inheritance of one Interface, and one abstract class compared to explicit implementation of two interfaces.

But, I am sure your answer is a good one.
George Jonsson 20-Sep-14 14:14pm    
Good additional info.
Thanks Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900