Interface vs. Virtual methods

J

Just D

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 interfaces.

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
 
G

Guest

Hi,

Using virtual methods would result in the creation of vtable internally. I don't think using interfaces would result in that, since we are not overriding anything. So woulnd't interfaces be faster?
 
M

Michael Culley

If you just want the same function names then use an interface. If you think you will every need to add common functionality then
use a base class.
 
D

Daniel O'Connell [C# MVP]

Rakesh Rajan said:
Hi,

Using virtual methods would result in the creation of vtable internally. I
don't think using interfaces would result in that, since we are not
overriding anything. So woulnd't interfaces be faster?

No, interfaces use vtables as well. I forget the exact method used to deal
with interfaces+vtables, but they are related.
 
J

Just D

HI Michael!

It's ok, but what about the execution, run-time speed?

Thanks,
Dmitri

"Michael Culley"
 
J

Just D

Hi Daniel,

Ok, easier. If I want just to redefine the public methods during writing,
not running, should I use only virtual methods from the base class to do
that? I'm sure not. And in this case the execution will be really fast,
correct? Everything should be done during compilation and I don't care of
any virtual tables etc. Look, it's easy. I created 3 files and it works. I
think it's the best approach.

------------------------------------------------------------------------
using System;

namespace ConsoleApplication___Inheritance
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
CDerived derived = new CDerived();

Console.WriteLine(derived.NonVirtualExecution()+"\n");

Console.WriteLine(derived.VirtualExecution()+"\n");

Console.ReadLine();
}
}
}
------------------------------------------------------------------------
using System;

namespace ConsoleApplication___Inheritance
{
public class CBase
{
public CBase()
{
}

public virtual string VirtualExecution()
{
return "Virtual Base Execution";
}

public virtual string NonVirtualExecution()
{
return "Non-virtual Base Execution";
}

}
}
------------------------------------------------------------------------
using System;

namespace ConsoleApplication___Inheritance
{
public class CDerived : CBase
{
public CDerived()
{
}

override public string VirtualExecution()
{
return base.VirtualExecution() + "\n" + "Virtual Derived Execution\n";
}

override public string NonVirtualExecution()
{
return base.NonVirtualExecution() + "\n" + "Non-Virtual Derived
Execution";
}

}
}
------------------------------------------------------------------------
Output:
Non-virtual Base Execution
Non-Virtual Derived Execution

Virtual Base Execution
Virtual Derived Execution
------------------------------------------------------------------------

Thanks,
Dmitri

"Daniel O'Connell [C# MVP]"
 
D

Daniel O'Connell [C# MVP]

Just D said:
Hi Daniel,

Ok, easier. If I want just to redefine the public methods during writing,
not running, should I use only virtual methods from the base class to do
that? I'm sure not. And in this case the execution will be really fast,
correct? Everything should be done during compilation and I don't care of
any virtual tables etc. Look, it's easy. I created 3 files and it works. I
think it's the best approach.

Generally thats what I would do. Use interfaces to expose a standard
interface on multiple classes.
------------------------------------------------------------------------
using System;

namespace ConsoleApplication___Inheritance
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
CDerived derived = new CDerived();

Console.WriteLine(derived.NonVirtualExecution()+"\n");

Console.WriteLine(derived.VirtualExecution()+"\n");

Console.ReadLine();
}
}
}
------------------------------------------------------------------------
using System;

namespace ConsoleApplication___Inheritance
{
public class CBase
{
public CBase()
{
}

public virtual string VirtualExecution()
{
return "Virtual Base Execution";
}

public virtual string NonVirtualExecution()
{
return "Non-virtual Base Execution";
}

}
}
------------------------------------------------------------------------
using System;

namespace ConsoleApplication___Inheritance
{
public class CDerived : CBase
{
public CDerived()
{
}

override public string VirtualExecution()
{
return base.VirtualExecution() + "\n" + "Virtual Derived Execution\n";
}

override public string NonVirtualExecution()
{
return base.NonVirtualExecution() + "\n" + "Non-Virtual Derived
Execution";
}

}
}
------------------------------------------------------------------------
Output:
Non-virtual Base Execution
Non-Virtual Derived Execution

Virtual Base Execution
Virtual Derived Execution
------------------------------------------------------------------------

Thanks,
Dmitri

"Daniel O'Connell [C# MVP]"
No, interfaces use vtables as well. I forget the exact method used to
deal with interfaces+vtables, but they are related.
 
M

Michael Culley

Just D said:
HI Michael!

It's ok, but what about the execution, run-time speed?

I'm not sure but I would suggest to make up a small test app, quite often you don't get the results you expect. Quite often what
people say is not true, I remember in vb6 everyone seemed to think that using the "with" keyword would speed things up but it
didn't.
 
J

Jeff Louie

If you are designing a contract without any implementation details and
the
contract is going to be useful to many other classes (eg. a mix in
class), then
you should use an interface. On the other hand, use a base class if you
want
to include some implementation details. Be careful though. Once you have
published an interface, it is difficult to update an interface. Updating
an
interface will break all classes that implement the interface, so you
need to
get it right the first time. If you have an evolving contract, consider
using a
base class and inheritance. If you must add new methods to the base
class,
you can provide a default implementation of the new methods. Any classes
that inherit from the base class will also inherit the default
implementations.
Updating the base class in this way does not break any class that
inherits
from the modified base class. In summary, if you have a well defined
contract
that will be implemented by many classes use an interface. If the
contract is
evolving, consider using a base class. Use a base class if you need to
include
implementation details. 

Regards,
Jeff

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
A

Angelos Petropoulos

Hey hey hey, don't worry about performance so much!

If you start avoiding something so fundamental as inheritance or
interfaces for the sake of a few CPU cycles or bytes of memory, you
have lost the plot. Even though people who have been replying are
saying that performance is similar this still stands. Performance
comes after design, unless there is a *seriously* good reason to do
otherwise.

Inheritance is used to express a "is-a" relationship. For example a
bulldog "is-a" dog and german shepard "is-a" dog too. You create all
three classes and you inherit Bulldog and GermanShepard from Dog. This
way, any common functionality can be placed inside Dog (plus you can
do other nice stuff such as implement the Template Design Pattern,
polymorphism and others).

Interfaces express a "can" relationship. For example System.Array and
System.Delegate both "can" create clones of themselves, so they
implement the ICloneable interface. They, however, have no other
similar behaviour and they don't have any common "is-a" relationships
so they should not inherit from a common base class.

Regards,
Angelos Petropoulos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top