467,879 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

implement interface

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
Jun 27 '08 #1
  • viewed: 2029
Share:
5 Replies
Tony Johansson wrote:
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.
--
GŲran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
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

"Tony Johansson" <jo*****************@telia.comwrote in message
news:jh*****************@newsb.telia.net...
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

Jun 27 '08 #3
On Fri, 06 Jun 2008 15:57:19 -0700, Tony Johansson
<jo*****************@telia.comwrote:
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.
It is said by whom? How do they define the term "implements"?

As Scott said, interfaces are _implemented_, not inherited. But even
ignoring that mistake in your terminology, it's perfectly reasonable to
say that a class that inherits some implementation does in fact in some
sense implement both the inherited implementation and new implementation.

Anyway, it's really hard to say one way or the other whether a specific
statement is accurate or not without you actually posting the statement
and the full context in which it was made.

Pete
Jun 27 '08 #4
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
Jun 27 '08 #5
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

"Hans Kesting" <ne*********@spamgourmet.comwrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
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


Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Peter | last post: by
2 posts views Thread by Derrick | last post: by
2 posts views Thread by Dave | last post: by
7 posts views Thread by moondaddy | last post: by
52 posts views Thread by Ben Voigt [C++ MVP] | last post: by
4 posts views Thread by =?Utf-8?B?QmFqaS4=?= | last post: by
3 posts views Thread by =?Utf-8?B?Sm9uIEU=?= | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.