473,722 Members | 2,338 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 3015
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
5801
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, so why not just provide a complete definition for the abstract method when defining the containing...
9
7558
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 can also check for interface at run time say when dll is loaded and see if it implememts whats...
10
2977
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 have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
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
3779
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
5199
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
5384
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
4470
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
4031
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 descendant classes (see example below.) In such a case where a common block of data *always* exists,...
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3207
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

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.