today we're going to be talking about default interface implementations in c sharp
so let's dive in so this new feature was introduced in c-sharp version 8 which you'll have
to have.net core version 3 or dotnet standard 2.1 in order to be able to use what are we doing with
default interfaces well let's take a look well why in the world would we want to do this well
mainly the purpose was to allow an api publisher to add members to an interface without breaking
existing implementations we sometimes call this evolving the interface so one of the solutions
is to use default methods now this kind of violates our concept of an interface right
because we've always been told that interfaces do not contain implementation only classes contain
implementation and if the class is incomplete then it's an abstract class but an interface is just
pure interface and no implementation an interface would not impose any implementation requirement
on a class think about this when a class inherits from another class it creates a kind
of relationship when we implement an interface we're just saying we're capable of something
so we can have things that implement an interface that are completely unrelated but implementing an
interface can be done by a lot of classes that are otherwise completely unrelated so as long
as we're adhering to those concepts an interface could have some default implementation and not
violate our understanding of what interfaces are all right let's take a look at an example let's
say that we're going to implement an interface called icompany the icompany interface defines
typical company things like a list of employees or human resources as well as the ability to hire
new human resources so this is our interface and it's a pretty typical interface what's going to
happen of course is over time we're going to get additional requirements for new methods
to be added to this interface so in the next slide let's take a look at an implementation of icompany
okay so now the developer defines a class called company that implements i company so we're using
a list to implement the list of resources we are implementing the getter for the resources property
and we've added a higher method so that we can add the items now at some point through
the evolution some applications that are using the icompany interface would need to know what
the total salary of all of the employees of the company are we want to add a property to icompany
because existing applications are using that interface so we want the method exposed there
but the project that defined the company class we just looked at is not aware of this new property
and doesn't need it however this class is being used by other projects that are aware of the new
property and would like to access it the other projects would get runtime exceptions because the
property defined in the interface allows their code to compile but fails at runtime since the
method is not in the binary the solution is to provide a default implementation of the property
in the interface the existing company class then will inherit the total salary property that you
see highlighted in yellow here now one caveat with default methods is that they are only visible when
you're using a variable of the interface type they are not visible if you're using a variable of the
class type unless that method has been overridden by the class so in the example you see that icomp
can access the total salary property whereas comp cannot comp is a variable of type company
and so the total salary property is not exposed unless you're explicitly using the icompany type
variable and you can do that with a cast all right so in conclusion you know this feature
was added to java version 8 and it caused quite a stir amongst the java community because typically
we don't think about an interface as containing implementation however for many many years in c
plus plus we've done exactly that because c plus does not have a designated syntax for interfaces
we just implemented classes and deemed them as interfaces and typically the litmus test for
an interface in c plus is a class that does not contain instance variables or fields one of the
side effects of this and we've already seen it in java is that many interfaces will start containing
what were helper methods that we found in static classes in earlier versions so look for a
migration in.net moving helper methods from static classes into interfaces hey subscribe to this
channel or join us on social media head over to train my programmers and enroll so that you
are notified anytime a new topic comes up hey i'm chuck mccullough and as always code happy