Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The limitation of interfaces is that they do not actually store any implementation details themselves. To share implementation details between classes, you can harness the power of inheritance through abstract classes. This video will look the similarities and differences between regular classes, abstract classes and interfaces.
[MUSIC]
0:00
Interfaces and abstract classes can
accomplish many of the same goals.
0:05
They also have their
own unique objectives.
0:10
If you want to guarantee that specific
classes provide specific functionality,
0:12
then an interface is a great tool.
0:17
An interface defines
what should happen but
0:20
doesn't define how that needs to happen.
0:22
This is extremely useful when
sharing code between projects.
0:25
I don't need to worry about how
someone else, or even my past self,
0:29
implements their code, I just need to
be able to interact with that code.
0:34
One of the most challenging
parts of writing software
0:39
is the mental load of storing all the
information about how each piece of code
0:42
interacts with every other piece of code.
0:47
If I make a change in one place,
what am I going to break somewhere else?
0:50
Many programming philosophies and
0:55
tools deal directly with
minimizing this challenge.
0:57
The key to solving this challenge
lies with encapsulation.
1:01
Encapsulation allows us to give
control of specific functionalities to
1:05
individual objects, allowing us to
focus on just the information needed
1:10
to leverage the functionality
each object controls.
1:15
Because my own limited memory no longer
has to store the inner workings of
1:20
the entire application, I can more
quickly add features and bug fixes.
1:25
This also allows new developers to
become productive more quickly.
1:31
Because they do not have to
understand the inner workings
1:35
of the entire application all at once.
1:38
By implementing multiple interfaces,
I can group functionality
1:42
into smaller parts that can be
used throughout my application.
1:46
The limitation of interfaces
is that they do not
1:51
actually store any implementation
details themselves.
1:54
Each class that implements an interface
must write their own methods.
1:59
Changing an interface requires
changing every implementing class,
2:04
because there's no default implementation.
2:09
To share implementation
details between classes,
2:12
you can harness the power of inheritance,
creating a parent or
2:15
super-class from which any number
of classes can be extended.
2:20
By declaring the parent class as abstract,
2:25
we prevent that class from
being directly instantiated.
2:28
Abstract classes also give us
the ability to define abstract methods
2:32
that must be implemented
by any extending children.
2:37
The main limitation of an abstract
class is the same as any other class.
2:41
You can only inherit
from one class at a time.
2:47
I have put together a chart that
lays out the similarities and
2:50
differences between these constructs.
2:54
Can it be instantiated?
2:57
Does it allow for multiple inheritance?
3:00
Can you define contractual functions?
3:03
Does it contain implementation details?
3:07
Can you make a change that is shared
by everything that uses this construct?
3:10
All methods required to be public?
3:16
Can you define properties?
3:19
Can you define constants?
3:22
Can you override these constants?
3:25
Can you type check for this construct?
3:28
Check the notes associated with this video
for a printable version of this chart.
3:31
By combining interfaces and
3:36
inheritance, we can make use of
the best qualities of each construct.
3:38
You need to sign up for Treehouse in order to download course files.
Sign up