473,408 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

A single implementation for a method in C#

22
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:

Expand|Select|Wrap|Line Numbers
  1. public class Cat : Feline
  2. {
  3.   void eat(food f)
  4.   {
  5.     //procedure for eating food
  6.   }
  7. }
  8.  
  9. public class Dog : Canine
  10. {
  11.   void eat(food f)
  12.   {
  13.     //procedure for eating food
  14.   }
  15. }
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#?
Sep 9 '07 #1
12 1379
MMcCarthy
14,534 Expert Mod 8TB
Moving to .NET forum.
Sep 9 '07 #2
ghd
22
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?
Sep 9 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
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.
Sep 9 '07 #4
ghd
22
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.
Sep 9 '07 #5
ghd
22
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)
Sep 9 '07 #6
ghd
22
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:
Expand|Select|Wrap|Line Numbers
  1. public void eat(food f)
  2. {
  3.   //procedure for eating food
  4. }
(:-o)
Sep 9 '07 #7
naivE
12
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.
Sep 9 '07 #8
ghd
22
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.
Sep 10 '07 #9
Plater
7,872 Expert 4TB
Well does it have to be contained withen?
Could you not make a static class called like:

Expand|Select|Wrap|Line Numbers
  1. public static classLifeFunctions
  2. {
  3.    //could add other arguments to define what it's eating or whatever
  4.    public static void Eat(object WhosEating)
  5.    {
  6.    }
  7. }
  8.  
Although I thought you could make some sort of interface with functions already defined. I will have to check.
Sep 10 '07 #10
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...
Sep 10 '07 #11
ghd
22
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:

Expand|Select|Wrap|Line Numbers
  1. public interface IEatFood
  2. {
  3.   void Eat(food f)
  4.   {
  5.     //Some procedure for eating food
  6.     //But the C# compiler will not allow this code to compile
  7.     //as methods cannot be implemented in the interface
  8.     //However if C# would have allowed a method to be implemented here
  9.     //The solution would be a very 'elegant' one
  10.   }
  11. }
  12.  
  13. public class Cat : Feline, IEatFood
  14. {
  15. }
  16.  
  17. public class Dog : Canine, IEatFood
  18. {
  19. }
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.
Sep 11 '07 #12
Plater
7,872 Expert 4TB
Well C# is a java clone and java doesn't support multiple inheritence either (last time I checked)
Sep 11 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: skn | last post by:
Hello, I have written a very simple java class file, which invokes a Python script using JEP. Code snippet:- ------------------- Jep jep = new Jep(false);...
2
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
3
by: Eric Chaves | last post by:
Hi fellows, According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal...
15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
32
by: Guoqi Zheng | last post by:
I am really do not know so much about byte/bit, etc. Question, if I defined a byte(), how can I add a single byte to it? what I want is that I have an array of bytes, I loop that array, look at...
2
by: jjouett | last post by:
We are starting to setup some Web Services to provide our customers with a way to programatically interact with our application, and some of our customers have slightly different requirements in...
5
by: Sarath | last post by:
I've to write a single instance class. there are different methods to control the single instance of a program. I've tried the following method class CSingleton { public: CSingleton&...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.