General Question About Implementing an Interface


N

needin4mation

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

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

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

needin4mation

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.
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]
- (e-mail address removed)

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

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

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.
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]
- (e-mail address removed)

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.
 

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