473,398 Members | 2,120 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,398 software developers and data experts.

c # interfaces limitations ???

Hey all

Here is my question Consider two classes

Class A : SomeSystemClassA

Class B : SomeSystemClassB
Now I have to add a common interface to both class A and class B. The
way A and B are derived I cannot add a common abstract base class for
them.

So I do

Class A : SomeSystemClassA, ICommonInterface

Class B : SomeSystemClassB, ICommonInterface

The problem I am facing is that C# doesn't allow interfaces to have
static functions or for that matter any function implementation (unless
I am missing something )

In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?

Have a CommonInterfaceImplClass property on ICommonInterface and in
class A and B have all methods dispatch the call to
CommonInterfaceImplClass.

Class A
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Class B
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Still code duplication but not a maintenance nightmare.

Nov 16 '05 #1
13 1833
Interfaces are not a C# concept, they're an Object Oriented principle.

Interfaces are purely abstract. That is they do not contain any
implementation or data members.

In your case I'd do the following:

IAnimal (interface)
- all the function declarations that are required for a concrete
implementation of the object go here

Mammal : IAnimal (abstract class)
- if there are shared functions between two children they are
implemented here...

Cat : Mammal (concrete class)
Dog : Mammal (concrete class)
- uses functions found in Mammal, but has its own instantiations.
Hope that helps.

Daniel.


<ra******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hey all

Here is my question Consider two classes

Class A : SomeSystemClassA

Class B : SomeSystemClassB
Now I have to add a common interface to both class A and class B. The
way A and B are derived I cannot add a common abstract base class for
them.

So I do

Class A : SomeSystemClassA, ICommonInterface

Class B : SomeSystemClassB, ICommonInterface

The problem I am facing is that C# doesn't allow interfaces to have
static functions or for that matter any function implementation (unless
I am missing something )

In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?

Have a CommonInterfaceImplClass property on ICommonInterface and in
class A and B have all methods dispatch the call to
CommonInterfaceImplClass.

Class A
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Class B
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Still code duplication but not a maintenance nightmare.

Nov 16 '05 #2
Static methods on an interface would not really make sense. An interface is a
contract, which the implementor must support.

In your scenario, where you acnnot use implementation inheritance to provide
the functionality for common methods, you could instead create a separate
class which provides this functionality. Each class that implements your
interface could then have a private instance of this class. The interface
implemented methods could then call into this class.

HTH
Dan

"ra******@hotmail.com" wrote:
Hey all

Here is my question Consider two classes

Class A : SomeSystemClassA

Class B : SomeSystemClassB
Now I have to add a common interface to both class A and class B. The
way A and B are derived I cannot add a common abstract base class for
them.

So I do

Class A : SomeSystemClassA, ICommonInterface

Class B : SomeSystemClassB, ICommonInterface

The problem I am facing is that C# doesn't allow interfaces to have
static functions or for that matter any function implementation (unless
I am missing something )

In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?

Have a CommonInterfaceImplClass property on ICommonInterface and in
class A and B have all methods dispatch the call to
CommonInterfaceImplClass.

Class A
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Class B
{
Void CommonFunc1()
{
CommonInterfaceImplClass. CommonFunc1()
}
}
Still code duplication but not a maintenance nightmare.

Nov 16 '05 #3
> In your scenario, where you acnnot use implementation inheritance to
provide
the functionality for common methods, you could instead create a separate
class which provides this functionality. Each class that implements your
interface could then have a private instance of this class. The interface
implemented methods could then call into this class.


While this is not wrong. The fact that the classes have code in common
suggests it to be something pertaining to that object. If this is the case,
then the object orientated way would be to have the code refactored out, and
placed into a class that is a sub set of these objects. Hence creating an
intermediate layer, parenting the concrete classes, but a child of the
interface.

Creating a global class dumping ground isn't a clean or clear way of showing
what you're trying to do. While a few methods used by a couple of sibling
classes doesn't donote this scenario, it's an easy habit to get into where
every time you want to reference some common code, it goes into some large
class.


Nov 16 '05 #4
I think you missed my question totally. I already have something like
this. Now I want Cat and Dog to implement IPettable interface. I
cannot add it at Mammal because not all Mammals are pettable.

so if I do

Cat : Mammal , IPettable
Dot: Mammal , IPettable

Now Cat and Dog have to implement all IPettable methods and I am
finding some methods are exactly the same in Cat and Dog class

IPettable
{
reactToPetting()
LookAtOwnerLovingly()
}

Dog
{

reactToPetting()
{
Woof()
LookAtOwnerLovingly()
}

}

Cat
{

reactToPetting()
{
Purr()
LookAtOwnerLovingly()

}

}

In this case reactToPetting() needs to be different but
LookAtOwnerLovingly() method for class Dog and Cat are doing
essentially the same thing

Raj

Nov 16 '05 #5
Unless I am missing something it looks exactly like what i proposed in
the original post

Nov 16 '05 #6
<ra******@hotmail.com> a écrit dans le message de news:
11*********************@z14g2000cwz.googlegroups.c om...
In a scenario like this if ICommonInterface had 10 funcs chances are A
and B contain 3 functions that are exactly the same resulting in code
duplication in A and B

How can this problem be solved? Is there a way better than this method?


Write a delegate class that implements the common interface methods and
simply write redirection methods in each class that requires that
functionality.

////////////////////////////////////
interface IMyInterface
{
void Method1();
void Method2();
}

public class MyIntfDelegate
{
void Method1()
{
...
}

void Method2();
{
...
}
}

public class UsesDelegate : IMyInterface
{
private MyIntfDelegate delegate = new MyIntfDelegate();

void IMyInterface.Method1()
{
delegate.Method1();
}
void IMyInterface.Method2()
{
delegate.Method2();
}
}
////////////////////////////////

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #7
Unless I am missing something it looks exactly like what i proposed in
the original post ..........Not again

Nov 16 '05 #8
Not quite. The suggestion is to create a PettableMammal class that is a
child class of Mammal and implements IPettable, and derive Dog and Cat
from that.

Of course, this works for only one interface, and only in certain
situations, so the general answer to your question is no, there is no
solution other than the one you outlined.

Multiple inheritance was omitted from C# because of the problems that
it causes in C++ (the "diamond problem" and all that), and as Anders
Hejlsberg put it, "You almost never really need it."

I find that the only significant problem that single inheritance /
interfaces cause in C# (and Java) is the pressure to make interfaces
simple--weak, if you will. The developer tends to under-design
interfaces knowing that every new bit of functionality has to be
implemented "n" times. Of course, the resulting interfaces are often
adequate, but could be better.

Nov 16 '05 #9
Well java doesn't have the same problem though...one can implement
multiple interfaces like C# but they let you have default implemenation
in the interface class.

This way when one implements an interface one only needs to override
the methods one needs.

Nov 16 '05 #10
<ra******@hotmail.com> wrote:
Well java doesn't have the same problem though...one can implement
multiple interfaces like C# but they let you have default implemenation
in the interface class.
No they don't. From the JLS:

<quote>
An interface declaration introduces a new reference type whose members
are classes, interfaces, constants and abstract methods. This type has
no implementation, but otherwise unrelated classes can implement it by
providing implementations for its abstract methods.
</quote>

If you really believe Java interfaces can provide implementations,
please give some example code showing this. You won't be able to.
This way when one implements an interface one only needs to override
the methods one needs.


Nope.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
<ra******@hotmail.com> a écrit dans le message de news:
11**********************@l41g2000cwc.googlegroups. com...
Unless I am missing something it looks exactly like what i proposed in
the original post ..........Not again


That's because there really is no other way :-)

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
Nov 16 '05 #12
Not the Java I knew... but then it may have changed in the last two
years. :)

Nov 16 '05 #13
Bruce Wood <br*******@canada.com> wrote:
Not the Java I knew... but then it may have changed in the last two
years. :)


It has, but not in that respect.

(I find it amazing how many times people on .NET newsgroups claim that
Java works in some way that it doesn't...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
8
by: Tony | last post by:
Can someone point me to an ASP application that interfaces to a Usenet newsgroup? FOR EXAMPLE, I'd like an application that would display the posts here in microsoft.public.inetserver.general,...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.