A single implementation for a method in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghd
    New Member
    • Sep 2007
    • 22

    A single implementation for a method in C#

    Hare Krishna, Hi,

    I wanted to know if C# has a facility whereby i can define one single implementation for a method that is used by two different classes in two different class heirarchies? Now let me illustrate the problem with an example.
    I have got a 'cat' class and a 'dog' class:

    Code:
    public class Cat : Feline
    {
      void eat(food f)
      {
        //procedure for eating food
      }
    }
    
    public class Dog : Canine
    {
      void eat(food f)
      {
        //procedure for eating food
      }
    }
    The implementation for the 'eat' method is exactly the same for both classes. Hence, I would like to define the 'eat' method at any one place so that maintenance of the code will be easy. But the 'eat' method must be usable by both classes. How can i accomplish this feat in C#?
    Last edited by ghd; Sep 9 '07, 06:15 AM. Reason: I posted this in the wrong forum!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Moving to .NET forum.

    Comment

    • ghd
      New Member
      • Sep 2007
      • 22

      #3
      Originally posted by mmccarthy
      Moving to .NET forum.
      I am sorry. I also realized the mistake after posting the thread. I trying to figure out how to move it to the .Net forum. Could you please tell me how?

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by ghd
        I am sorry. I also realized the mistake after posting the thread. I trying to figure out how to move it to the .Net forum. Could you please tell me how?
        I've moved it now. In the future click on the report button and ask a moderator to move it for you.

        Comment

        • ghd
          New Member
          • Sep 2007
          • 22

          #5
          Originally posted by mmccarthy
          I've moved it now. In the future click on the report button and ask a moderator to move it for you.
          Thank you very much.

          Comment

          • ghd
            New Member
            • Sep 2007
            • 22

            #6
            The question is still open: How can i define a single implementation for a method in C# that can be used by two different classes in two different class heirarchies? (Please see the beginning of the thread for a full definition of the problem)

            Comment

            • ghd
              New Member
              • Sep 2007
              • 22

              #7
              Originally posted by ghd
              The question is still open: How can i define a single implementation for a method in C# that can be used by two different classes in two different class heirarchies? (Please see the beginning of the thread for a full definition of the problem)
              I am sorry. The 'eat' method must be modified by the keyword 'public' as:
              Code:
              public void eat(food f)
              {
                //procedure for eating food
              }
              (:-o)

              Comment

              • naivE
                New Member
                • Jun 2007
                • 12

                #8
                I think the idea would be to take that hierarchy further back. Sure cat and dog inherit from feline and canine but feline and canine can have similarities. They could inherit from a mammal class which could in-turn inherit from an animal class which could contain the eat method.

                A google search for polymorphism and inheritance in OOP will get you started.

                Comment

                • ghd
                  New Member
                  • Sep 2007
                  • 22

                  #9
                  Originally posted by naivE
                  I think the idea would be to take that hierarchy further back. Sure cat and dog inherit from feline and canine but feline and canine can have similarities. They could inherit from a mammal class which could in-turn inherit from an animal class which could contain the eat method.

                  A google search for polymorphism and inheritance in OOP will get you started.
                  But the problem is that the 'Feline' and 'Canine' class are already defined by the class provider. As a client , i have the ability to only inherit from these classes. I have no liberty to add another class or another method to the existing classes in the heirarchy of either 'Feline' or 'Canine'.

                  Actually, in my situation, the 'Feline' class is the 'UserControl' class (which is defined by the CLR) and the 'Canine' class is the 'ComboBox' class (which is again pre-defined by the CLR). I coined the class names 'Feline' and 'Canine' to generalize the problem that i am facing with.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Well does it have to be contained withen?
                    Could you not make a static class called like:

                    Code:
                    public static classLifeFunctions
                    {
                       //could add other arguments to define what it's eating or whatever
                       public static void Eat(object WhosEating)
                       {
                       }
                    }
                    Although I thought you could make some sort of interface with functions already defined. I will have to check.

                    Comment

                    • nanku71
                      New Member
                      • Sep 2007
                      • 4

                      #11
                      I second the opinion from Plater.
                      I guess even a simple class (not static method or any thing) can help solve the problem.

                      Public Class common()
                      public void eat(object whosEating , food f) {
                      //implementation goes here. .. .
                      }


                      After this, the feline and canine classes can create an instance of the common class and can use its eat method inside its own eat method. . .

                      inside your cat or dog class....
                      public void eat(food f) {
                      common c(){new}
                      c.eat(cat, f)
                      }


                      ps: pardon my C# syntex.... 'm mainly doing vb.net...

                      Nanku...

                      Comment

                      • ghd
                        New Member
                        • Sep 2007
                        • 22

                        #12
                        Originally posted by nanku71
                        I second the opinion from Plater.
                        I guess even a simple class (not static method or any thing) can help solve the problem.

                        Public Class common()
                        public void eat(object whosEating , food f) {
                        //implementation goes here. .. .
                        }


                        After this, the feline and canine classes can create an instance of the common class and can use its eat method inside its own eat method. . .

                        inside your cat or dog class....
                        public void eat(food f) {
                        common c(){new}
                        c.eat(cat, f)
                        }


                        ps: pardon my C# syntex.... 'm mainly doing vb.net...

                        Nanku...
                        Thank you very much, Nanku, Plater and Naive. If C# would have supported defining the implementation of methods in interfaces then that would have been a very elegant solution to the whole problem. Or if C# would have supported inheriting from multiple classes that would also have resulted in an elegant solution. I wonder why C# doesn't allow inheriting from two or more classes.It can allow inheriting only those methods and members that aren't common to the classes (as inheriting methods with the same signature and members with the same name and types would result in the confusion as to which implementation that would be inheritied). Here's what i mean:

                        Code:
                        public interface IEatFood
                        {
                          void Eat(food f)
                          {
                            //Some procedure for eating food
                            //[B]But the C# compiler will not allow this code to compile[/B]
                            //as methods cannot be implemented in the interface
                            //However if C# would have allowed a method to be implemented here
                            //The solution would be a very 'elegant' one
                          }
                        }
                        
                        public class Cat : Feline, IEatFood
                        {
                        }
                        
                        public class Dog : Canine, IEatFood
                        {
                        }
                        The basic reason why C# won't allow the above code to compile is because of the principle that it follows: no inheritance of more than one class. If C# wouldn't have stuck to this principle, it would have allowed implementation of methods (and also defining other members) in the interface.

                        Hare Krishna.

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          Well C# is a java clone and java doesn't support multiple inheritence either (last time I checked)

                          Comment

                          Working...