none
Same method in interface and abstract class RRS feed

  • Question

  • Hello,

    Suppose one method is the same in interface and abstract and one class derived from these(interface and abstract).

    Which one should be implemented? from interface or abstract class?

    Thanks

        interface Point
        {
            void upPoint();
        }
        abstract class P
        {
            public abstract void upPoint();
        }
        class Program : P , Point
        {
            
            static void Main(string[] args)
            {
    
                Console.ReadLine();
            }

    Wednesday, November 29, 2017 3:29 PM

Answers

  • Overriding the method upPoint in Program would only need to be done once, and this would satisfy both requirements to implement the abstract ancestor method and the interface method.

    Although this kind of situation sounds like a highly confusing and unnecessary one that should be avoided if at all possible.


    • Edited by RJP1973 Wednesday, November 29, 2017 3:36 PM
    • Marked as answer by Arash_89 Thursday, November 30, 2017 4:03 PM
    Wednesday, November 29, 2017 3:36 PM
  • Hello Arash_89,

    >>Which one should be implemented? from interface or abstract class?

    The both should be implemented. If either of them is not implemented, the compiler will prompt the unrealized message and of course the program can't pass to build.

    If you use potential fixes feature. you will see the VS just generate only one method. As RJP1973 mentioned. The overriding method will satisfy both conditions of them. If you run the below code, you will get the same result.

         static void Main(string[] args)
            {
               P p= new Program();
                p.upPoint();
    
                Point p1 = new Program();
                p1.upPoint();
            }

    And if you want to implement difference method between them. You should use interface name to specify belonging. just like as below.

            public override void upPoint()
            {
                Console.WriteLine("abstract class");
            }
    
            void Point.upPoint()
            {
                Console.WriteLine("interface");
            }

    Note. Because the class just has only one parent class. the "override" keywords will specific the method is implemented parent class.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, November 30, 2017 7:50 AM
    • Marked as answer by Arash_89 Thursday, November 30, 2017 4:03 PM
    Thursday, November 30, 2017 7:50 AM
    Moderator
  • Sounds to me like you have a design flaw.

    Anyways, if you implement the interface in the abstract class, you are all good.

    If that doesn't work for you, you can implement interface methods explicitly by preceeding the method name with the interface name and ".".

    You can choose to either implement both (suing the explicit option) or implement it on your abstract class and override the methods not to be shared through the abstract class in derived classes. You will be able to assign objects from those derived classes to variables of type of the abstract class or interface if you just implement the interface on your abstract class.

        interface IFace
        {
            void Method();
        }
    
        abstract class Face : IFace
        {
            public virtual void Method()
            {
                // general implementation
            }
        }
    
        class DerivedFace : Face
        {
            public override void Method()
            {
                base.Method();
            }
        }

    OR

        interface IFace
        {
            void Method();
        }
    
        abstract class Face
        {
            public virtual void Method()
            {
                // general implementation
            }
        }
    
        class DerivedFace : Face, IFace
        {
            public override void Method()
            {
                base.Method();
            }
    
            void IFace.Method()
            {
                // explicit
            }
        }


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.

    • Marked as answer by Arash_89 Thursday, November 30, 2017 4:03 PM
    Thursday, November 30, 2017 9:52 AM

All replies

  • Overriding the method upPoint in Program would only need to be done once, and this would satisfy both requirements to implement the abstract ancestor method and the interface method.

    Although this kind of situation sounds like a highly confusing and unnecessary one that should be avoided if at all possible.


    • Edited by RJP1973 Wednesday, November 29, 2017 3:36 PM
    • Marked as answer by Arash_89 Thursday, November 30, 2017 4:03 PM
    Wednesday, November 29, 2017 3:36 PM
  • Hello Arash_89,

    >>Which one should be implemented? from interface or abstract class?

    The both should be implemented. If either of them is not implemented, the compiler will prompt the unrealized message and of course the program can't pass to build.

    If you use potential fixes feature. you will see the VS just generate only one method. As RJP1973 mentioned. The overriding method will satisfy both conditions of them. If you run the below code, you will get the same result.

         static void Main(string[] args)
            {
               P p= new Program();
                p.upPoint();
    
                Point p1 = new Program();
                p1.upPoint();
            }

    And if you want to implement difference method between them. You should use interface name to specify belonging. just like as below.

            public override void upPoint()
            {
                Console.WriteLine("abstract class");
            }
    
            void Point.upPoint()
            {
                Console.WriteLine("interface");
            }

    Note. Because the class just has only one parent class. the "override" keywords will specific the method is implemented parent class.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, November 30, 2017 7:50 AM
    • Marked as answer by Arash_89 Thursday, November 30, 2017 4:03 PM
    Thursday, November 30, 2017 7:50 AM
    Moderator
  • Sounds to me like you have a design flaw.

    Anyways, if you implement the interface in the abstract class, you are all good.

    If that doesn't work for you, you can implement interface methods explicitly by preceeding the method name with the interface name and ".".

    You can choose to either implement both (suing the explicit option) or implement it on your abstract class and override the methods not to be shared through the abstract class in derived classes. You will be able to assign objects from those derived classes to variables of type of the abstract class or interface if you just implement the interface on your abstract class.

        interface IFace
        {
            void Method();
        }
    
        abstract class Face : IFace
        {
            public virtual void Method()
            {
                // general implementation
            }
        }
    
        class DerivedFace : Face
        {
            public override void Method()
            {
                base.Method();
            }
        }

    OR

        interface IFace
        {
            void Method();
        }
    
        abstract class Face
        {
            public virtual void Method()
            {
                // general implementation
            }
        }
    
        class DerivedFace : Face, IFace
        {
            public override void Method()
            {
                base.Method();
            }
    
            void IFace.Method()
            {
                // explicit
            }
        }


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.

    • Marked as answer by Arash_89 Thursday, November 30, 2017 4:03 PM
    Thursday, November 30, 2017 9:52 AM