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

Interface vs. Virtual methods

All,

What are advantages and disadvantages of these two different approaches?

1 . I create a base class with a few virtual methods, then I derive my
classes from this class, override these methods and use this class.

2. I work with the interface, same for each class, just define it for every
class

What is faster in .NET? I supposed that deriving from the base classes.

The main purpose was to create a few classes of the second level having the
same method names like Initialize(), Update(), Get(), Sage(). Add() etc.

Thanks,
Dmitri

Jul 21 '05 #1
8 2407
> What are advantages and disadvantages of these two different approaches?

1 . I create a base class with a few virtual methods, then I derive my
classes from this class, override these methods and use this class.

2. I work with the interface, same for each class, just define it for every class

What is faster in .NET? I supposed that deriving from the base classes.

The main purpose was to create a few classes of the second level having the same method names like Initialize(), Update(), Get(), Sage(). Add() etc.

There should be no performance differences between interfaces and virtual
methods since interface methods are internally just abstract methods.
You should consider that in .NET classes can oly have one base-class but
multiple interfaces, also if your classes will have common functionality you
best go with base-class but using interfaces you can still use composition
or delegation to provide this functionality in each class without
copy/paste-programming.

Also bear in mind that if you later add methods to an interface you also
have to add it to all its implementing classes, with a base-class you don't
have this problem (unless the added method is abstract).

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jul 21 '05 #2
Hi Cody,
You should consider that in .NET classes can only have one base-class but
multiple interfaces, also if your classes will have common functionality
you
best go with base-class but using interfaces you can still use composition
or delegation to provide this functionality in each class without
copy/paste-programming.


Interesting, but just one question. I haven't heard that I can use a parts
of code from different files to compose some code file. Are you meaning this
way? Sometimes I want to include a part of the code repeating in different
places, the inheritance is not always acceptable and I need just to insert a
reference to some external file to use the code from this file. I couldn't
find any official way for that in C#. In C++ we can do that. I even asked in
this group or in CSHARP a few months ago. No answer.If you know anything
about these combining of the code from external files, then please tell a
little bit more, ok?

Thanks,
Dmitri

Jul 21 '05 #3
> > You should consider that in .NET classes can only have one base-class
but
multiple interfaces, also if your classes will have common functionality
you
best go with base-class but using interfaces you can still use composition or delegation to provide this functionality in each class without
copy/paste-programming.
Interesting, but just one question. I haven't heard that I can use a parts
of code from different files to compose some code file. Are you meaning

this way? Sometimes I want to include a part of the code repeating in different
places, the inheritance is not always acceptable and I need just to insert a reference to some external file to use the code from this file. I couldn't
find any official way for that in C#. In C++ we can do that. I even asked in this group or in CSHARP a few months ago. No answer.If you know anything
about these combining of the code from external files, then please tell a
little bit more, ok?

In the next version of C# you will be able to split classes in several
source files, but I didn't meant that.
Composition is someting like that:

class Car
{
Engine engine;
}

instead of

class Car : Engine
{
}

so if you need common functionality and do not want to put it in a
base-class, make it an own class and put and object of it in your class. Got
the idea?

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jul 21 '05 #4
Have a look at
http://msdn.microsoft.com/library/de...anagedcode.asp.
Performance issues of method calls are explained in VERY HIGH detail there.
In a nutshell: an interface call takes a little longer than a virtual method
call, but we're talking about nano-seconds here, so this is really only
relevant if all your methods are empty...

From a design perspective: classes describe "kinds" of things (like cat,
human or car) while interfaces describe "ways to interact" with things, or
"roles" of things (like waiter, president, transportation utility). It's not
always 100% clear how to classify things, but the methods you describe
(Init, Update, Get, Add) sound like "a way to interact with things",
although these "things" may be of unrelated kind.

Typical interfaces are IComparable or IList; You can write algorithms that
operate on these and provide e.g. sorting or searching services without
knowledge of the underlying implementation;

The basic idea is: instead of tying classes together, you loosely couple
them through well-defined interfaces, so you can at any time decide to split
a certain class into two if it gets too complex, to fuse two classes
together if they don't make sense separete from each other or to exchange a
certain class by another. Other classes won't be affected by such decisions,
as they only see the interfaces that don't change (of course this only works
if your interfaces *are* well defined...)

Hope this helps,

Niki
"Just D" <no@spam.please> wrote in news:fYOzc.28$8r5.6@fed1read03...
All,

What are advantages and disadvantages of these two different approaches?

1 . I create a base class with a few virtual methods, then I derive my
classes from this class, override these methods and use this class.

2. I work with the interface, same for each class, just define it for every class

What is faster in .NET? I supposed that deriving from the base classes.

The main purpose was to create a few classes of the second level having the same method names like Initialize(), Update(), Get(), Sage(). Add() etc.

Thanks,
Dmitri

Jul 21 '05 #5
> Have a look at
http://msdn.microsoft.com/library/de...us/dndotnet/ht
ml/fastmanagedcode.asp. Performance issues of method calls are explained in VERY HIGH detail there. In a nutshell: an interface call takes a little longer than a virtual method call, but we're talking about nano-seconds here, so this is really only
relevant if all your methods are empty...


They should perform the same speed, you cannot tell the one is faster than
the other only because of small deviations in the results. The results
highly depend on the hardware, the .NET runtime version and on the
application (does the code fit in the cache/cache misses on so on).
What you surely can tell that inlined methods are the fastest, and the speed
decreases with the count and sizes of the method parameters (including
this-pointer) and return type, which means that a static void foo() is the
fastest and a virtual method should perform much slower.

I do not believe some results of the test it says for example that a "this
itf instance call" takes 0.2ns whereas a "this itf virtual call" took 5.4ns.
Iam sure, the first one was certainly inlined.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jul 21 '05 #6
Assuming a spherical cow ...

A lot of assumptions here to derive the idea that virtual methods are
faster. I will agree, however, that the span is very small, if it is faster.
When compiled, you end up with code that is identical, or nearly so.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"Niki Estner" <ni*********@cube.net> wrote in message
news:uT*************@TK2MSFTNGP10.phx.gbl...
Have a look at
http://msdn.microsoft.com/library/de...anagedcode.asp. Performance issues of method calls are explained in VERY HIGH detail there. In a nutshell: an interface call takes a little longer than a virtual method call, but we're talking about nano-seconds here, so this is really only
relevant if all your methods are empty...

From a design perspective: classes describe "kinds" of things (like cat,
human or car) while interfaces describe "ways to interact" with things, or
"roles" of things (like waiter, president, transportation utility). It's not always 100% clear how to classify things, but the methods you describe
(Init, Update, Get, Add) sound like "a way to interact with things",
although these "things" may be of unrelated kind.

Typical interfaces are IComparable or IList; You can write algorithms that
operate on these and provide e.g. sorting or searching services without
knowledge of the underlying implementation;

The basic idea is: instead of tying classes together, you loosely couple
them through well-defined interfaces, so you can at any time decide to split a certain class into two if it gets too complex, to fuse two classes
together if they don't make sense separete from each other or to exchange a certain class by another. Other classes won't be affected by such decisions, as they only see the interfaces that don't change (of course this only works if your interfaces *are* well defined...)

Hope this helps,

Niki
"Just D" <no@spam.please> wrote in news:fYOzc.28$8r5.6@fed1read03...
All,

What are advantages and disadvantages of these two different approaches?

1 . I create a base class with a few virtual methods, then I derive my
classes from this class, override these methods and use this class.

2. I work with the interface, same for each class, just define it for

every
class

What is faster in .NET? I supposed that deriving from the base classes.

The main purpose was to create a few classes of the second level having

the
same method names like Initialize(), Update(), Get(), Sage(). Add() etc.

Thanks,
Dmitri


Jul 21 '05 #7
It really depends on the nature of your methods. Interfaces are best when
you deal with functionality that spans multiple families of classes. For
example, you want to add print capabilities to both the Employee class and
an Invoice class. Other than object, these two classes have nothing in
common, so you would unlikely create a virtual class to derive from, as this
is not good OO design.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"Just D" <no@spam.please> wrote in message news:fYOzc.28$8r5.6@fed1read03...
All,

What are advantages and disadvantages of these two different approaches?

1 . I create a base class with a few virtual methods, then I derive my
classes from this class, override these methods and use this class.

2. I work with the interface, same for each class, just define it for every class

What is faster in .NET? I supposed that deriving from the base classes.

The main purpose was to create a few classes of the second level having the same method names like Initialize(), Update(), Get(), Sage(). Add() etc.

Thanks,
Dmitri

Jul 21 '05 #8
They should perform the same speed, you cannot tell the one is faster than the other only because of small deviations in the results. The results
highly depend on the hardware, the .NET runtime version and on the
application (does the code fit in the cache/cache misses on so on).
Did you read that article? He actually shows the assembler sequency

produced JIT compiler, and they are different for virtual calls/interface calls. Why should they take the same amout of time? I'm not an assembler crack, but it seems pretty obvious that 5 instructions may take more time than 3
instructions...
OK I now read it again and it seems that for interface calls there is an
additional lookup for the interface.
It seems interface tables are using not the vtable of the class but of the
interface, so the result is now logical to me.
I do not believe some results of the test it says for example that a "this itf instance call" takes 0.2ns whereas a "this itf virtual call" took

5.4ns.
Iam sure, the first one was certainly inlined.


All method calls in the table took from 5.4-6.8ns, the two inlined ones took
0.2ns. So it is very unlikely that "this itf virtual call" takes 0.2ns.
Iam certain the method was inlined.
virtual calls need one extra memory lookup; Memory lookups are about the
most expensive operation in this kind of benchmark.


Here is the question why a "static call" took 6.1ns and a "virtual call"
took 5.4ns.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jul 21 '05 #9

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

Similar topics

10
by: The Directive | last post by:
Is there a way in C++ to create a "true" interface. I want to create an absract class that other classes can inherit from and be forced to implement the interface's abstract (pure virtual) methods....
1
by: Fuzzy | last post by:
If an object implements an interface, are interface methods always virtual or only if accessed through the interface? For example, if I have: interface IFoo { public void Method1() } ...
13
by: Just D | last post by:
All, What are advantages and disadvantages of these two different approaches? 1 . I create a base class with a few virtual methods, then I derive my classes from this class, override these...
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...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
10
by: Bit byte | last post by:
I have a set of C++ classes for which I want to provide a C API - and compile into a C DLL, for use in an application that can only work with a C interface DLL. Any suggestions/pointers on how...
7
by: Ernesto Bascón | last post by:
Opinions. I want to create an OO UI framework. I want to define a set of ABCs like: class IA { virtual int GetA() = 0; };
5
by: The Cool Giraffe | last post by:
I'm designing an ABC and in connection to that i have run into some "huh!" and "oh...". Let me put it as a list. 1. Since the class will only contain bodies of the methods, only the header file...
8
by: Lamefif | last post by:
// C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.