Inheritance != Interface

While continuing my journey into C# I learned about interfaces. This was a new concept and my prior experience with Ruby and JavaScript had not introduced me to the idea of interfaces. However, working in ASP.NET I am now a huge fan of interfaces and how they apply proper OOP principles such as creating loosely coupled and extensible applications.

Example Code: Logger Interface

Interface

“a class implements an interface”

An interface’s main goal is to ensure that all classes implementing the interface are adhering to a contract.

They look like a class, however, interfaces only contain the declaration of the members with no implementation. We know what we want our subclasses to be able to do, but we are not concerned with how that will be done. Usually, this is because “how it is done” can vary.

Take a look at the example interface above. It is a logger interface that declares two methods, LogError and LogInfo. Now the interface doesn't care how those methods work but only that they both exist. We could create a class that logs to the console, then another that logs to a file. Both these classes could implement our logger interface and have completely different LogError and LogInfo methods. The interface simply ensures that the classes will have the methods.

Inheritance

a class inherits from a base class

I want to stress that interfaces are not inheritance.

The main reason for using inheritance is code reusability. It’s used to give subclasses the same methods as the base class, without having to re-write any code at all! The class inherits the implementation of the methods in the base class.

Interfaces are not providing code reusability since they contain no code for implementations of their methods!

Interfaces are not Multiple Inheritances

C# has a singular inheritance. This means each subclass can only inherit from one base class. This was done since multiple inheritances can be a bad practice in OOP as it can make the source more complicated.

Now even though C# does not allow for multiple inheritances, C# does allow you to implement multiple interfaces to a class. However, multiple interfaces, this is not the same as having multiple inheritances!

Interfaces guarantee that an object implements specific contracts. Multiple interfaces mean an object supports multiple contracts. This is not multiple inheritances, which means the object would inherit both the interface and the implementation.

Summary

The best way to summarize the difference between interfaces and inheritence:

  • Inheritance describes an is-a relationship
  • Implementing an interface describes a can-do relationship

References

--

--

--

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Common Open-Source Server Monitoring Tools

Open-Source Server Monitoring Tools

Navigating The Toronto Real Estate Board’s (TREB’s) Data Exchange

Java VisualVM Profiler monitoring of application running on EC2 — the easy way

PlatON Bi-Weekly Report, 3.1–3.15

You Should Write Bad Code More Often

Iguazu — The Z-Tech Data Lake is born

React.js Projects — Firebase Overview — Soft Skills

How to Implement a New Feature?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sara Khandaker

Sara Khandaker

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

More from Medium

How to make a movable control in Avalonia?

How to fix “The provided certificate file is not a valid PFX file” with dotnet dev-certs https…

ASP.NET Core MVC Fundamentals

How to Display Sparklines in a WPF Datagrid Application | ComponentOne