Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interface Inheritance

Interface Inheritance

Scheduled Pinned Locked Moved C#
oopquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aslesh
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • A Aslesh

      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

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      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)

      A 1 Reply Last reply
      0
      • D DaveyM69

        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)

        A Offline
        A Offline
        Aslesh
        wrote on last edited by
        #3

        what if we call like code c = new code(); c.Codeproject(); which one does it call?

        D R 2 Replies Last reply
        0
        • A Aslesh

          what if we call like code c = new code(); c.Codeproject(); which one does it call?

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          I think you are misunderstanding interfaces. An interface has no implementation. c.CodeProject simply calls the method CodeProject() in the class code - 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)

          A 1 Reply Last reply
          0
          • D DaveyM69

            I think you are misunderstanding interfaces. An interface has no implementation. c.CodeProject simply calls the method CodeProject() in the class code - 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)

            A Offline
            A Offline
            Aslesh
            wrote on last edited by
            #5

            Ok Thanks I got you.. Santhapur

            1 Reply Last reply
            0
            • A Aslesh

              what if we call like code c = new code(); c.Codeproject(); which one does it call?

              R Offline
              R Offline
              Robert C Cartaino
              wrote on last edited by
              #6

              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

              R 1 Reply Last reply
              0
              • R 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. Cartaino

                R Offline
                R Offline
                Robert C Cartaino
                wrote on last edited by
                #7

                Oops, too slow. Already answered...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups