Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Abstract classes appear to be more powerful than interfaces, but interfaces have a very specific and useful purpose.
-
0:00
On the surface abstract base classes appear to be more
-
0:03
flexible than interfaces.
-
0:05
They allow us to declare both public and
-
0:08
protected members, that some classes must have and they can also contain code.
-
0:14
Interfaces on the other hand can only declare the public members that subclasses
-
0:18
must have and that's it.
-
0:20
It's easy to see why we want to use abstract base classes, but why interfaces?
-
0:27
Let's explore why interfaces are so useful in C#.
-
0:31
You may think that interfaces are more restrictive versions of
-
0:34
abstract base classes, but in fact, they serve different purposes.
-
0:39
Abstract base classes are used to contain code that should be shared by the concrete
-
0:44
subclass implementations.
-
0:46
The implementations of all the non abstract members of the invader class
-
0:50
are inherited by its subclasses.
-
0:53
Each subclass doesn't have to create their own implementation.
-
0:57
This is an advantage to use in an abstract base class.
-
1:00
On the other hand C# only allows classes to inherit directly from one other class.
-
1:06
Being able to inherit directly from multiple base classes is known as multiple
-
1:10
inheritance.
-
1:12
Multiple inheritance can cause a lot of problems so it isn't allowed in C#.
-
1:17
Think of the case when two base classes both have methods that look identical.
-
1:22
Which method implementation should be inherited in the subclass?
-
1:25
Instead of multiple inheritance C# allows a class to Implement multiple interfaces.
-
1:31
A class can implement as many interfaces as needed.
-
1:35
An interface does nothing about the members' implementation.
-
1:38
So, it doesn't matter if two interfaces both have the same member with
-
1:42
the same signature.
-
1:44
The class only needs to provide one implementation of the member
-
1:47
to satisfy the interface.
-
1:49
An interface can also inherit other interfaces.
-
1:53
Let's break up the Iinvader interface into a couple different interfaces.
-
1:59
We can break the location property out Into another interface or just cut it from
-
2:02
here, Go up here and we'll create a new interface called IMappable.
-
2:10
We'll move the property here.
-
2:12
So now any class that implements the IMappable interface
-
2:16
must have a public map location property that provides at the very least a getter.
-
2:21
Just like how classes can implement multiple interfaces,
-
2:24
interfaces can also inherit from multiple interfaces.
-
2:27
Let's create an interface named IMovable.
-
2:31
So say, interface I Move able.
-
2:36
This one will require a single method named,
-
2:38
Move which will take from the IInvader interface.
-
2:45
There we go.
-
2:47
To inherit from multiple interfaces, we simply list them after the colon and
-
2:51
separate them with commas.
-
2:53
So we'll have the IInvader interface inherit the IMappable and
-
2:59
the IMovable interface.
-
3:01
This is the same way that a class can implement multiple interfaces.
-
3:05
We just list them here after the colon,
-
3:07
just like we do when we inherit from another class.
-
3:10
Now anything that implements the IInvader interface must also implement
-
3:15
the IMappable and IMovable interfaces.
-
3:18
That means in addition to having to have a HasScored, Health, IsNeutralized,
-
3:23
IsActive and DecreaseHealth members, they also have to have Move and Location.
-
3:29
The usefulness of interfaces isn't obvious right away, but they're important in good
-
3:33
object oriented design and they make extending and maintaining code easier.
-
3:37
The point of an interface is to only expose what is
-
3:40
absolutely needed by the code that is using the class.
-
3:43
This gives us the greatest flexibility when writing classes
-
3:47
the implement the interface.
-
3:48
We only need to match the interface.
-
3:50
Say for example that we had a method that only needed to call the Move method
-
3:54
on one of its parameters.
-
3:56
We could code it so that it only expected a parameter of the type IMoveable.
-
4:00
We could past that method any object of type IMovable,
-
4:04
IInvader is a type IMovable and so is IInvader.
-
4:07
And by extension so are all of the other invader subclasses.
-
4:10
In fact,
-
4:11
we could even pass the method a class that had nothing to do with being an invader.
-
4:15
Say for example, we had a class named Bird that implemented the IMovable interface,
-
4:20
we could also pass it to the method that expected in IMovable.
-
4:23
Even though bird is nothing like an invader,
-
4:26
they both have a Move method because they implement the IMovable interface.
-
4:30
Can you see how using interfaces in our code gives us the most freedom
-
4:34
in how we wire up our objects?
-
4:36
Combining new interface with an abstract base class is a common pattern.
-
4:39
It allows us to have the flexibility of an interface and
-
4:43
also provides a mechanism to share code among subclasses.
-
4:47
In the next video,
-
4:48
we'll see how we can leverage the power of the IInvader interface to create a new
-
4:53
type of invader that doesn't inherit from the invader base class at all
You need to sign up for Treehouse in order to download course files.
Sign up