473,503 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interface implementation question

Hi everyone,

I'm a bit of a newbie to C# programming so forgive this innocent
question, but coming from a C++ background this seems very odd to me,
and I'm hoping someone can shed some light onto why things work this
way as opposed to the C++ way, which I see no real disadvantages in.

The issue arises when trying to implement an interface in C#. In C++
it is quite common to define an interface using a bunch fo pure virtual
functions and then implement it 2 or 3 levels up in the hierarchy. For
example,

class A
{
public:
virtual void f() = 0;
};

class B : public A
{

};

class C : public B
{
virtual void f() {cout << "f()" << endl;}
}

In C#, it seems this is not allowed by design (unless I'm missing
something obvious, which it certainly wouldn't be the first time). My
question is why? It seems to force you to implement all methods at the
very next level of inheritance. In the above example, C# compiler
would have forced me to implement the method f() in class B.

This method seems very inconvenient, however. For example, there are
plenty of pre-existing interfaces in the .NET base classes that provide
various services. What if you want a particular set of classes to all
provide this service? Normally, in C++, I would have made a base class
called like BaseServiceClass, which implemented this interface. Then
in the derived classes I would provide the implementation, and
elsewhere in my code I would store pointers to BaseServiceClass
objects, all of which could be used to call the method in question.
What is the paradigm for solving this kind of design problem in the C#
/ .NET world?

Thanks

Jan 16 '06 #1
6 1487
I think you might be confusing "interfaces" with "inheritance". Interfaces
will indeed force you to provide an implementation on the class for which
it is defined - otherwise what would be the point of the interface?

The example you give works just fine in C# as well if you are talking about
"inheritance". In fact, the example you give isn't "interface" at all in
the strict sense of the word - it is in fact inheritance. Also, to avoid
warnings, you would need the 'override' keyword on the f() in the class C.

public class A
{
public virtual void f() { }
}

public class B : A
{
}

public class C : B
{
public override void f()
{
Console.WriteLine("f()");
}
}

-mdb

"Zach" <di***********@gmail.com> wrote in news:1137427435.828656.194550
@g43g2000cwa.googlegroups.com:
Hi everyone,

I'm a bit of a newbie to C# programming so forgive this innocent
question, but coming from a C++ background this seems very odd to me,
and I'm hoping someone can shed some light onto why things work this
way as opposed to the C++ way, which I see no real disadvantages in.

Jan 16 '06 #2
Zach wrote:
I'm a bit of a newbie to C# programming so forgive this innocent
question, but coming from a C++ background this seems very odd to me,
and I'm hoping someone can shed some light onto why things work this
way as opposed to the C++ way, which I see no real disadvantages in.

The issue arises when trying to implement an interface in C#. In C++
it is quite common to define an interface using a bunch fo pure virtual
functions and then implement it 2 or 3 levels up in the hierarchy. For
example,

class A
{
public:
virtual void f() = 0;
};

class B : public A
{

};

class C : public B
{
virtual void f() {cout << "f()" << endl;}
}

In C#, it seems this is not allowed by design (unless I'm missing
something obvious, which it certainly wouldn't be the first time).


The above in C# would be:

public abstract class A
{
public abstract void F();
}

public abstract class B : A
{
}

public class C : B
{
public override void F()
{
Console.WriteLine ("F()");
}
}

B and A cannot be instantiated directly, as they don't provide
implementations for all the methods which could be called. Use the
modifier "abstract" to indicate that a method has to be overridden, or
to indicate that a class must be derived from.

Does that answer your question?

Jon

Jan 16 '06 #3
I understand the difference between the two, the thing is that in C++
there is no difference: Implementation -is- inheritance. So when
moving to C# it is more strict about what you can and can't do since it
distinguishes between the two.

The problem, however, is that there are interfaces that are already in
existance in the .NET base class libraries. They are defined using the
keyword "interface". A good example is ISerializable. At some point,
I may want 50 related classes to be serializable. It would seem
logical that, rather than directly implementing ISerializable on each
of the 50 classes, I would implement it on a base class, but let the
derived classes override the GetObjectData() method, rather than the
base class (since it can't possibly know how derived classes need to
serialize themselves). This would naturally lead to the base class not
being instantiatable, which is fine.

Sure if I'm writing all the code myself I can make my class abstract
instead of making it an interface, but a lot of times I'm using an
interface that already exists somewhere (be it in the .NET framework,
or in some 3rd party code), and use of the other code requires that I
implement the correct interface rather than using an abstract class
(for example, because a method specifically requests an interface as a
parameter).

For example, following doesn't compile in C#, although I would like it
to.

class A : ISerializable
{
}

class B : A
{
public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
//Serialize the class here.
}
}

Instead, I have to do something like this to achieve a similar result:

abstract class A : ISerializable
{
protected abstract void GetObjectDataImpl(SerializationInfo info,
StreamingContext context);

public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
GetObjectDataImpl(info, context);
}
}

class B : A
{
protected override void GetObjectDataImpl(SerializationInfo info,
StreamingContext context)
{
//Serialize the class here
}
}

So I guess my real question is, is there a more elegant way of doing
this, or is this really what I have to do? And what is the reasoning
behind the designers decision to force interface implementations to
happen at the very next level in the hierarchy, rather than simply
making the class uninstantiatable until such level in the hierarchy
where all the interface methods, properties, etc have been provided an
implementation?

Jan 16 '06 #4
Wait! I've seen the light. Apparently the following code works just
fine:

abstract class A : ISerializable
{
public abstract void GetObjectData(SerializationInfo info,
StreamingContext context);
}

class B : A
{
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
//Serialize the class here
}
}

It just didn't occur to me that I might be able to add the keyword
'abstract' at the base class level, and then just leave it
unimplemented.

Jan 16 '06 #5
"Zach" <di***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
It just didn't occur to me that I might be able to add the keyword
'abstract' at the base class level, and then just leave it
unimplemented.

"abstract" is just one of many keywords/syntax rules of C#, which take
concepts which, in C++ are put in comments(*), and actually puts them in the
C# code where (EGADS!), the compiler might actual verify them.
(*) It could happen....
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Jan 16 '06 #6
Yea, it's nice having the compiler check more things for you. Still
though, it seems that educational materials focus so much on drilling
into your head that "abstract classes" and "interfaces" are completely
different ("you use -either- an abstract class or an interface") that
it clouds the real issue. For me, it made me assume that doing things
the way I finally got it to work would generate a compiler error, since
the solution to the problem involved using an abstract class -and- an
interface.

Jan 16 '06 #7

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

Similar topics

6
3499
by: Paul Fame | last post by:
Hello World, This is not a flame, but a question about the fundamentals of the language. Unlike some languages, C++ requires class member functions to be declared twice: once in the class...
9
4617
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
175
8641
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
20
4222
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
13
3216
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface...
4
2836
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
8
3922
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
2
268
by: psandler70 | last post by:
All, This might be a stupid question. When defining an interface, is it possible to force the class that implements it to support a property/method with the same name, but not force a...
52
20810
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
1
3199
by: =?Utf-8?B?Sk0=?= | last post by:
In an application I have an interface with methods and properties. The interface is used on a Class (ie class MyClass : IMyClassA, IMyClassB). On a windows form I define a BindingSource...
0
7084
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
7278
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
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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
5578
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,...
1
5013
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.