473,573 Members | 2,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Dec 7 '07 #1
5 3002
On Fri, 07 Dec 2007 13:28:00 -0800, Rich <Ri**@discussio ns.microsoft.co m>
wrote:
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
Dec 7 '07 #2
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.
--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com


"Rich" <Ri**@discussio ns.microsoft.co mwrote in message
news:E9******** *************** ***********@mic rosoft.com...
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

Dec 7 '07 #3
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<Twher e T : IBaseElement
{
protected List<TmSelected ;

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)m Selected[i];

tempElement.Exe cute();

ReportProgress( i / mSelected.Count , i);

if (mSelected.Coun t 1)
{
UpdateControls( );
}
}
}
}

internal class DistributionRow Status : IBaseElement
{

#region IBaseElement Members

public void Execute()
{
//
}

#endregion
}

internal class ReportRowStatus : IBaseElement
{

#region IBaseElement Members

public void Execute()
{
//
}

#endregion
}

internal class Class1 : baseclass<Distr ibutionRowStatu s>
{
public Class1():base()
{
//
}

protected override Type GetElementType( )
{
return System.Type.Get Type("Distribut ionRowStatus");
}

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

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

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

protected override Type GetElementType( )
{
return System.Type.Get Type("ReportRow Status");
}

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

protected override void UpdateControls( )
{
//Do custom work here
}
}
"Rich" <Ri**@discussio ns.microsoft.co mwrote in message
news:E9******** *************** ***********@mic rosoft.com...
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

Dec 7 '07 #4
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

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '07 #5
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.
"Rich" wrote:
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
Dec 7 '07 #6

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

Similar topics

6
5793
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove the need to have abstract class types in C#? Derived classes from abstract base classes must overrided the abstract method defininition anyway,...
9
7543
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you...
10
2959
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods...
10
667
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what circumstacnes does one implement an interface? TIA, -- Joe VBA Automation/VB/C++/Web and DB development
18
3768
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
5187
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
3
5378
by: Valmont | last post by:
Moving from C++ to C# .Net: Why does an interface exist in C# when I can create one by making all methods in the abstract class "pure virtual"? Did I miss something conceptually or is the "interface" just here for my conveniance? If it is a convinient thing, what style is accepted by the C# community (or mcsd requirements) as the standard?
7
4460
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears to be checking that the correct data type is used DataAcess sets an abstract class and methods
6
4021
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it would be useful to have /some/ data defined in such an abstract class for reasons of forming a common block of data existing in or used by all...
0
8006
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7768
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6400
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5288
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3729
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2191
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 we have to send another system
1
1295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.