Implementing Interfaces

  • Thread starter Thread starter Chris Leffer
  • Start date Start date
C

Chris Leffer

Hi.

I wrote an Interface with 6 methods, that I will implement in two
classes. But on one of these classes, I don't need to implement one of
the Interface methods. As the compiler keeps warning me that I need to
implement the method, what is the correct way to code that? Is there any
way to say that I don't want to implement an specific method?

TIA,
Chris Leffer
 
Chris Leffer said:
Hi.

I wrote an Interface with 6 methods, that I will implement in two
classes. But on one of these classes, I don't need to implement one
of the Interface methods. As the compiler keeps warning me that I
need to implement the method, what is the correct way to code that?
Is there any way to say that I don't want to implement an specific
method?

First, make sure that it really makes sense to implement the interface,
because it's a contract that should be fulfilled. However, you can
implement the method and, inside, only throw a NotSupportedException at
runtime.


Armin
 
I wrote an Interface with 6 methods, that I will implement in two
classes. But on one of these classes, I don't need to implement one of
the Interface methods. As the compiler keeps warning me that I need to
implement the method, what is the correct way to code that? Is there any
way to say that I don't want to implement an specific method?

An interface defines a contract - thus you MUST implement it.

If you have optional methods, either create 2 interfaces or use object
inheritence.
 
Hi Armin.

I see your point. I defined an Interface because the classes will have
the same features but with some differences in implementation. The only
exception is this method that really makes nosense on one of them.

If you can help me a bit more, my Interface defines a method called
'Add'. On one of the classes I implemented the method and created a new
one with a different signature. I was expecting that when calling the
class, the two methods would be overloaded, but just the one coming from
the Interface was shown. Is this normal behaviour?

That's the call:

Dim sRecord As IParameters = Record.GetSettings
sRecord.Add() 'At this point, just one of the signatures is shown

TIA,
Chris Leffer
 
If you can help me a bit more, my Interface defines a method called
'Add'. On one of the classes I implemented the method and created a new
one with a different signature. I was expecting that when calling the
class, the two methods would be overloaded, but just the one coming from
the Interface was shown. Is this normal behaviour?

That's the call:

Dim sRecord As IParameters = Record.GetSettings
sRecord.Add()

You casted the object an IParameters, so you'll only see IParameter's
method signatures.
 
Chris Leffer said:
Hi Armin.

I see your point. I defined an Interface because the classes will
have the same features but with some differences in implementation.

If one method was missing it would not have the same features.
The only exception is this method that really makes nosense on one
of them.

Sign the whole contract or none. :-)
If you can help me a bit more, my Interface defines a method called
'Add'. On one of the classes I implemented the method and created a
new one with a different signature. I was expecting that when
calling the class, the two methods would be overloaded, but just the
one coming from the Interface was shown. Is this normal behaviour?

That's the call:

Dim sRecord As IParameters = Record.GetSettings
sRecord.Add() 'At this point, just one of the signatures is shown

Yes, because IParameters does not have the other method. The compiler
can not know which object is assigned to sRecord; it just knows that it
fulfills the IParameters contract.

FYI, the association between the member to implement and the
implementing member is done by the "Implements" keyword in the
procdure's signature, not by the procedure name.


Armin
 
Back
Top