Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between inherits and implements in C#

Tags:

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

like image 975
Alan2 Avatar asked Aug 18 '12 16:08

Alan2


People also ask

Is implement inheritance?

Implementation inheritance is the mechanism whereby a subclass re-uses code in a base class. By default the subclass retains all of the operations of the base class, but the subclass may override some or all operations, replacing the base-class implementation with its own.

What is the difference between extends and implements?

Difference: implements means you are using the elements of a Java Interface in your class. extends means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.

Can a class both inherit and implement?

Yes of course. Have you gone through a basic Java tutorial, covering inheritance and interfaces? Classes don't inherit from interfaces, they just implement them. You can extend (inherit from) a single class, but you can implement multiple interfaces.

What is implementation and interface inheritance?

Implementation inheritance is a relationship where a child class inherits behaviour implementation from a base class. Interface inheritance is when a child class only inherits the description of behaviour from the base class and provides the implementation itself.


1 Answers

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

Therefore HitController : ICentroidHitListener is correct.

like image 127
Ry- Avatar answered Oct 05 '22 22:10

Ry-