Abstract Classes and Interfaces relationship between C# and VB.Net

R

Rich

Greetings,

I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I
just want to ask about the relationship between Abstract Classes and
Interfaces.

My first question is if C# even has Iinterfaces. I took some Java
programming classes and Interfaces are a main staple of Java. And in VB.Net
I use interfaces for setting up definitions of classes. I am guessing that
Abstract classes in C# do the same thing as Interfaces in VB.Net - define
class structures.

Am I correct in my thinking about Abstract classes in C#? Does C# have
Interfaces? If so, then a C# Interface would be different than a VB.Net
interface. If not, then I can think that Abstract Classes of C# function the
same as Interfaces in VB.Net.

Any enlightenment/clarification greatly appreciated.

Thanks,
Rich
 
G

Guest

No. Interfaces exist in C#. An interface, by definition, cannot have any
implementation, and a class can implement any number of interfaces;

class MyClass : IDisposable, IEnumberable, IWhateverIWant {}

An abstract class *can* contain implementation code. Since it is a class,
single object inheritance means that you can only derive from one class.
Period. An Abstract class, however, cannot be created/instantiated. Only a
class that derives from the abstract can be instantiated.

So an abstract class is useful as a common base for several classes that
have the same implementations for some pieces but that would not make sense
to have by themselves.

Abstract classes can also have abstract members, forcing children to
implement those members in the same way an interface enforces this behavior.
 
A

amdrit

My impression and the way I make use of interfaces, base classes, and
classes is this:

Interfaces define the contract of an objects structure

in C# interface <interface name here> {}
in VB Interface <interface name here>
End Interface

Base classes define a contract for derived class yes, but also maintain
functional control as well.

in C# abstract class <class name here>{}
in VB Mustinherit Class <class name here>
End Class

Classes then implement their base class and interfaces to fulfill any
contracts, and provide any customizations to the overriable members of the
base class, extend the base class, and implement interface members.


Take a look at the sample below, the sample was for another post, but the
ideas are still teh same.

internal interface IBaseElement
{
void Execute();
}

internal abstract class baseclass<T> where T : IBaseElement
{
protected List<T> mSelected;

protected baseclass()
{
mSelected = new List<T>();
}

protected abstract Type GetElementType();
protected abstract void ReportProgress(decimal percentComplete, int
position);
protected abstract void UpdateControls();
protected void DoSomething()
{

for (int i = 0; i < mSelected.Count; i++)
{

IBaseElement tempElement = (IBaseElement)mSelected;

tempElement.Execute();

ReportProgress(i / mSelected.Count, i);

if (mSelected.Count > 1)
{
UpdateControls();
}
}
}
}

internal class DistributionRowStatus : IBaseElement
{

#region IBaseElement Members

public void Execute()
{
//
}

#endregion
}

internal class ReportRowStatus : IBaseElement
{

#region IBaseElement Members

public void Execute()
{
//
}

#endregion
}

internal class Class1 : baseclass<DistributionRowStatus>
{
public Class1():base()
{
//
}

protected override Type GetElementType()
{
return System.Type.GetType("DistributionRowStatus");
}

protected override void ReportProgress(decimal percentComplete, int
position)
{
//Do custom work here
}

protected override void UpdateControls()
{
//Do custom work here
}
}

internal class Class2 : baseclass<ReportRowStatus>
{
public Class2(): base()
{
//
}

protected override Type GetElementType()
{
return System.Type.GetType("ReportRowStatus");
}

protected override void ReportProgress(decimal percentComplete, int
position)
{
//Do custom work here
}

protected override void UpdateControls()
{
//Do custom work here
}
}
 
P

Peter Duniho

Greetings,

I am actually a VB.Net guy, but I have worked somewhat with C++ and C#.
I
just want to ask about the relationship between Abstract Classes and
Interfaces.

My first question is if C# even has Iinterfaces.

Yes, it does. See the C# "interface" keyword.
I took some Java
programming classes and Interfaces are a main staple of Java. And in
VB.Net
I use interfaces for setting up definitions of classes. I am guessing
that
Abstract classes in C# do the same thing as Interfaces in VB.Net - define
class structures.

While there is some similarity between an abstract class and an interface
in C#, they are not the same. Most notably:

* An abstract class can include some implementation. It does not need
to be purely abstract
* A class can only inherit one class (abstract or otherwise), but it
can implement arbitrarily many interfaces
Am I correct in my thinking about Abstract classes in C#?

It doesn't look like it, though you haven't been very specific about what
your "thinking about Abstract classes in C#" is. If you think they are a
replacement for interfaces, that's not correct.
Does C# have Interfaces?
Yes.

If so, then a C# Interface would be different than a VB.Net
interface.

Why would it? What's different about VB.Net interfaces from C# interfaces?
If not, then I can think that Abstract Classes of C# function the
same as Interfaces in VB.Net.

Can a VB.Net interface provide any implementation details? Can a class
inherit only one VB.Net interface? If the answer to both of those
question is 'yes", then it seems that abstract classes in C# are the same
as interfaces in VB.Net. Otherwise, no...you're wrong, the two are not
the same.

Pete
 
J

Jeff Louie

Rich... You _can_ think of interfaces as the equivalent of pure virtual
classes in C++ or perhaps as contracts without any implementation
details.

Regards,
Jeff
 
R

Rich

Thank you all for your posts and explanations. This is very helpful for me
to understand OOP a little bit better.

Actually, I downloaded a sample project which combines C# code with VB.Net
code (very cool). The project really contains two projects - a ClassLib
project from C# and its usage in a VB.Net app. Anyway, the C# part had a lot
of abstract classes and so forth, and I was starting to get confused how it
all worked. Now I have a little bit better understanding. The whole thing
was about getting functionality to Undo/Redo stuff.
 

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