Many collection types implement the list interface.
For example, Array List implements the list interface.
Linked list implemented as well, and there are other classes that we're not going to cover.
So in this lesson, you're going to see polymorphism from classes that implement list.
Open the folder for this lesson by following this path in your course resources.
Now, list is an interface that defines methods a class must implement.
The array list class implements this interface, it also implements a bunch of other interfaces, inherits
from a class, but we don't care about that.
Let's just focus on the fact that array list implements the list interface.
Here are some of the methods that list the fines, this one should be familiar to you, size array list
implements this interface.
So we know the array list class overrides this method.
You can go back here and call size from this array list.
Here's another one is empty, and it follows that we can call is empty.
Contains a list, definitely overrides, contains, because we've used it so many times before it.
Anyways, you get the points, the array list class implements this interface, so it's basically signing
a contract that it's going to override all of the methods inside this interface and it does.
For example, we know our array list is going to be forced to override array.
Let's start to Iraq, which returns an array from the array list.
Now, linked list is another collection type, it also implements the list interface.
Because linked list implements list, it also signs a contract that it's going to override all of the
methods inside list.
We can create a linked list collection in much the same way linked list that can store energy objects
list to.
Is equal to a new object of the limitless class.
And because the linked list class implements the list interface, we know it's going to be forced to
override all of these methods.
So all the same, we can call at least to add a lot, a few numbers.
We can call list to size.
List that is empty.
List contains.
Listen to not Iraq.
All right, so what's the difference between a relist and linked list, performance wise linked list
is almost always the wrong choice.
If you look at the documentation array list is usually faster.
Array list offers a faster way of accessing elements because it has direct references to every element,
whereas with a linked list, it has to go through every single element in the list until it gets to
the element that you're trying to access.
So it's a lot slower when you're trying to access values.
In this course, we're going to stick to using a realist.
Now, what's interesting is that remember that interfaces allow for polymorphism because array list
implements the list interface.
We can declare the object type list.
Same thing here, we can declare the linked list as type list.
Why is polymorphism important?
Same reason as always, flexibility, if I declare this type list, then I can easily switch between
array list and linked list.
I like to just stick to our list now, all I want you to take away from this lesson is that array list
and linked list can take the form of the interface they implement.
Let's recap array list and linked list, implement the list interface.
List is an interface that defines method's a class must implement.
In other words, each class signed a contract to override every method inside the lowest interface.
Each one of them overrides these methods in a different way.
But in general, performance was linked.
List is almost always the wrong choice.
In this course, we're going to stick to using a realist.
And because each class implements the interface, we can see polymorphism, you can declare the array
list as type list Auris type array list, and you can also declare a linked list as its own type or
type list.