Back
Close

C# Refresh

Nonsultant
10K views

Interfaces

To some extend interfaces reminds a lot of an abstract class. But there is some big differences.

  1. An interface can't contain any logic
  2. A class can implement several interfaces
  3. An abstract class is a class which can't be instantiated.

An interfaces should be seen as a contract which define the public properties, methods, and events, which the class or struct must implement.

An object never can be an instance of an interface, but an object can be an instance of a class implementing one or more interfaces.

Naming

Interfaces is more or less the only type in Microsoft C# naming convension where prefixes is used in the naming.

So when naming an interface should the name be prefixed with an I. Eg. an interface for a mammal would would be named IMammal.

Interface example

In this example is does the interface Mammal tell us that all mammal have two properties of number of legs and the specie of the mammal, and that the mammal have the behavior of making a sound.

The example is quite simplified, but a key difference between a dog and a human is the ability to speak, so the implementation of human and dog differs a bit, but they still implement to the same contract (interface). This contract is used by the DisplayMammal method to display the information on the mammal.

Question to think about: What could we do if the mammal does not have any legs? Like a whale

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants