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

General Question About Implementing an Interface

Just learning about interfaces and had a probably simple question.

If a class inherits from an Interface, does this guarantee that the
class will have the methods defined in the Interface? I know this is
supposed to be true from what I read, but what I'm really asking is
that if a class implements an interface and it does not somewhere in
that class implement the methods, properties, etc. (all of them) in
that class, will that cause the program to not compile? Somewhere in
my class I have to have a reference to all the methods of the
Interface, right?

Thank you for any help.

Aug 24 '06 #1
3 1236
You are right. If a class says that it implements an interface, then it
must, by definition, implement ALL members of the interface. The C#
compiler will not allow the program to compile otherwise.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ne***********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
Just learning about interfaces and had a probably simple question.

If a class inherits from an Interface, does this guarantee that the
class will have the methods defined in the Interface? I know this is
supposed to be true from what I read, but what I'm really asking is
that if a class implements an interface and it does not somewhere in
that class implement the methods, properties, etc. (all of them) in
that class, will that cause the program to not compile? Somewhere in
my class I have to have a reference to all the methods of the
Interface, right?

Thank you for any help.

Aug 24 '06 #2
Thank you. I don't know if this is worthy of another thread or not,
but I got this code from

http://www.csharp-station.com/Tutorials/Lesson13.aspx

class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}

public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}

I just wanted to know why this line was there:

InterfaceImplementer iImp = new InterfaceImplementer();

Why does he have to instantiate the class if he's already in the class?
Thanks again.

Nicholas Paldino [.NET/C# MVP] wrote:
You are right. If a class says that it implements an interface, then it
must, by definition, implement ALL members of the interface. The C#
compiler will not allow the program to compile otherwise.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ne***********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
Just learning about interfaces and had a probably simple question.

If a class inherits from an Interface, does this guarantee that the
class will have the methods defined in the Interface? I know this is
supposed to be true from what I read, but what I'm really asking is
that if a class implements an interface and it does not somewhere in
that class implement the methods, properties, etc. (all of them) in
that class, will that cause the program to not compile? Somewhere in
my class I have to have a reference to all the methods of the
Interface, right?

Thank you for any help.
Aug 24 '06 #3
This doesn't have much to do with interfaces. A better example would
have been:

static void Main()
{
IMyInterface iImp = new InterfaceImplementer()
imp.MethodToImplement();
}

The reason why you have to create an instance of the class is because
you are in a static method. Because you are in a static method, you don't
have access to the instance members (and have to create an instance as a
result).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ne***********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Thank you. I don't know if this is worthy of another thread or not,
but I got this code from

http://www.csharp-station.com/Tutorials/Lesson13.aspx

class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}

public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}

I just wanted to know why this line was there:

InterfaceImplementer iImp = new InterfaceImplementer();

Why does he have to instantiate the class if he's already in the class?
Thanks again.

Nicholas Paldino [.NET/C# MVP] wrote:
>You are right. If a class says that it implements an interface, then it
must, by definition, implement ALL members of the interface. The C#
compiler will not allow the program to compile otherwise.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ne***********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegr oups.com...
Just learning about interfaces and had a probably simple question.

If a class inherits from an Interface, does this guarantee that the
class will have the methods defined in the Interface? I know this is
supposed to be true from what I read, but what I'm really asking is
that if a class implements an interface and it does not somewhere in
that class implement the methods, properties, etc. (all of them) in
that class, will that cause the program to not compile? Somewhere in
my class I have to have a reference to all the methods of the
Interface, right?

Thank you for any help.

Aug 24 '06 #4

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

Similar topics

39
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
4
by: Frank J. Reashore | last post by:
Hello Everyone, I am implementing a simple interface in C# using Visual Studio .net and was quite surprised to discover that the C# compiler does NOT complain if a method on the interface is not...
0
by: Yatin Bhuta | last post by:
Hi, I have an interface in vb 6.0. It has a property set member, which takes a parameter by reference. When I try to implement this interface in my vb.net project it gives me an error saying...
2
by: larzeb | last post by:
I receive the following error from the compiler: ScheduledInfo' must implement 'Property AddrLine1() As String' for interface 'ICASSBatch'. Implementing property must have matching...
4
by: Rulin Hong | last post by:
According to OO, interface is just a bunch of definitions, no implementation at all. It's also true when we write our own code in .NET. But I find every interface provided by .NET has specific...
6
by: S. Lorétan | last post by:
Hi guys. I am preparing the rewriting of an huge existing application in VB6. This software is a mess of 10 years of patchs and new functionalities added randomly, and didn't have any logical or...
6
by: Raj Wall | last post by:
Hi, I am trying to implement the IEqualityComparer interface for a struct so I can use it as the Key for a Dictionary. My struct declaration has: public struct Ring : IEqualityComparer {...
6
by: Bob Weiner | last post by:
I have a few dll's that return their own version of Account objects. In the asp code I am writing now I would like to define an interface, called IAccount, and convert each of these objects (...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.