Alan2
Alan2

Reputation: 24592

What is the difference between inherits and implements in C#

I have the following code:

public class HitController : MonoBehaviour, ICentroidHitListener

The way I understand it. HitController inherits from MonoBehaviour and implements the methods in the ICentroidHitListener.

But how can I code this if I just want it only to implement the methods in the ICentroidListener? Do I code it like this:

public class HitController : ICentroidHitListener

In which case that looks like HitController inherits from ICentroidHitListener

Upvotes: 31

Views: 23966

Answers (5)

Daniel
Daniel

Reputation: 2974

Generally, there is no way to differentiate an interface from a class using the : syntax.

That's why it's important to add an I prefix to all your interfaces.

The code you provided, public class HitController : ICentroidHitListener, is correct, and the readers of your code should be able to easily and quickly deduce that it is an interface based on the I prefix.

Taken from here:

When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation. The derived class is said to implement the interface implicitly.

Taken from here:

Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

In C#, as opposed to languages like C++, multiple inheritance is not allowed, meaning that a class cannot inherit from more than one other class. Conversely, a class can implement more than one interface.

Upvotes: 13

Peter Ritchie
Peter Ritchie

Reputation: 35869

"inheritance" implies extending existing behaviour. An interface has no behaviour, you have to implement all the behaviour of the methods/properties defined in the interface. Hence you "implement" an interface and cannot really inherit anything from it.

If you inherit from something else, you inherit all its concrete behaviour. If some other class implements an interface you don't have to also implement that interface. Now, the class that "implements" that interface could make one or more of those interface implementations "abstract"; in which case you'd be forced to implement the abstract members of the class you're deriving from. But, you don't have to implement that interface--you inherit the interface from the base. You can include the interface in the "base class list" for the derived type, but it's not necessary.

By that token, you'd also "implement" an entirely abstract class. But, it's rare that anyone would use that terminology. For the most part "interface" is syntax sugar in .NET; but it does have IL-level support. e.g. an entirely abstract base class is semantically identical to an interface.

Interfaces let you ensure the only thing you can do is define an interface and not behaviour (an abstract class can have behaviour).

Upvotes: 7

Arman Hayots
Arman Hayots

Reputation: 2508

That's easy.
Inheriting is gaining base class features to yours.
Implementing is creating features according to specified protocol.

class:class and interface:interface is inheritance,
class:interface is implementing,
class:abstract_class are both implementing and inheritance.

Upvotes: 1

Ry-
Ry-

Reputation: 225124

If it's an interface, : implements the interface. If it's a class, : inherits from the class.

Therefore HitController : ICentroidHitListener is correct.

Upvotes: 41

Jeremy Wiggins
Jeremy Wiggins

Reputation: 7299

The answer to what you're asking is "yes", if I understand it correctly.

In the first example, you are inheriting from a base class (MonoBehavior) and implementing an interface (ICentroidHitLIstener').

You have access to all the members of MonoBehavior from your HitController class, and your HitController class must provide implementation for all members defined in the ICentroidHitListener interface.

In the second example, you are only implementing the ICentroidHitListener interface.

Upvotes: 1

Related Questions