473,394 Members | 1,721 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,394 software developers and data experts.

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 1484
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.