implement interface

T

Tony Johansson

Hello!

Assume you have the following interface and classes shown below.

It is said that a class must implement all the methods in the interface it
inherits.
Below we have class MyDerivedClass that inherits IMyInterface but
MyDerivedClass doesn't implement method DoSomething() it inherits it from
the base class MyBaseClass.
So the statement that a class must implement all method in an interface that
it inherits is not fully true as I see this matter.
Do you agree with me ?

public interface IMyInterface
{
void DoSomething();
void DoSomethingElse();
}

public class MyBaseClass
{
public void DoSomething() { }
}

public class MyDerivedClass : MyBaseClass, IMyInterface
{
public void DoSomethingElse() {}
}

//Tony
 
G

Göran Andersson

Tony said:
Hello!

Assume you have the following interface and classes shown below.

It is said that a class must implement all the methods in the interface it
inherits.
Below we have class MyDerivedClass that inherits IMyInterface but
MyDerivedClass doesn't implement method DoSomething() it inherits it from
the base class MyBaseClass.
So the statement that a class must implement all method in an interface that
it inherits is not fully true as I see this matter.
Do you agree with me ?

public interface IMyInterface
{
void DoSomething();
void DoSomethingElse();
}

public class MyBaseClass
{
public void DoSomething() { }
}

public class MyDerivedClass : MyBaseClass, IMyInterface
{
public void DoSomethingElse() {}
}

//Tony

The person writing that description probably didn't think of that
special case, or throught that it was more important to keep the
description simple, than to make it cover every possible aspect.
 
S

Scott M.

It is said that a class must implement all the methods in the interface it
inherits.

Well no. A class must implement all the methods of the interface it
"implements". Classes do not "inherit" interfaces. Implementation and
inheritance are not the same thing and thus, the source of your confusion.

MyDerivedClass doesn't have to implement DoSomething() because it is
inheriting the implementation of DoSomething() from the base class. It does
have to "implement" DoSomethingElse() because the class "implements" the
interface.

So, when a class "inherits" from a base class, it inherits all that the base
class has. When a class "implements" an interface it is entering into a
contract that requires that the entire interface be implemented.

-Scott
 
H

Hans Kesting

Tony Johansson expressed precisely :
Hello!

It is said that a class must implement all the methods in the interface it
inherits.

I think it depends on what you mean by "implement a method".

If it is strict "must physically define this method within this class",
then the above statement is false.

If it is the more broad "an instance of this class should have this
method, either directly or through inheritance", then the statement is
true. And your example would not violate it.

Hans Kesting
 
S

Scott M.

I think you're confusing things for the OP.

There really isn't (or shouldn't be) a dual meaning for "implements". A
method is implemented when a class provides functionality that the interface
requires and that functionality matches the name/signature that the
interface requires.

If a class inherits from a class that implements a method, the derived class
does not implement the method, but instead has an implementation of the
method.

In other words, implementation occurs at the level where the method is
provided to the class.

-Scott
 

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