11- Generic Classes and Inheritance

Jun 20, 2026 06:59 · 4:17 · English · Whisper Turbo · 2 speakers
यो प्रतिलिपि यसमा म्याद समाप्त हुन्छ 24 दिन स्थायी भण्डारणका लागि स्तरबृद्धि गर्नुहोस् →
देखाउँदै मात्र
0:03
S… Speaker 2 (11- Generic Classes and Inheritance)
In this video we're going to talk about a common misunderstanding when it comes to programming
0:08
S… Speaker 2 (11- Generic Classes and Inheritance)
with generics.
0:08
S… Speaker 2 (11- Generic Classes and Inheritance)
So let's add a new class here.
0:11
S… Speaker 1 (11- Generic Classes and Inheritance)
We call it instructor.
0:13
S… Speaker 2 (11- Generic Classes and Inheritance)
We want the instructor class to inherit from the user class.
0:18
S… Speaker 2 (11- Generic Classes and Inheritance)
So here we type extends user.
0:20
S… Speaker 2 (11- Generic Classes and Inheritance)
Now we have a compilation error.
0:22
S… Speaker 2 (11- Generic Classes and Inheritance)
It says there is no default constructor available in user.
0:26
S… Speaker 2 (11- Generic Classes and Inheritance)
So earlier we added this constructor to
0:30
S… Speaker 2 (11- Generic Classes and Inheritance)
pass the number of points and for this reason
0:33
S… Speaker 2 (11- Generic Classes and Inheritance)
our instructor class doesn't know what value or what argument to pass
0:37
S… Speaker 2 (11- Generic Classes and Inheritance)
to the constructor of the user class.
0:39
S… Speaker 2 (11- Generic Classes and Inheritance)
To solve this problem,
0:41
S… Speaker 2 (11- Generic Classes and Inheritance)
we add a custom constructor here.
0:43
S… Speaker 2 (11- Generic Classes and Inheritance)
So public instructor,
0:45
S… Speaker 2 (11- Generic Classes and Inheritance)
we give it the points.
0:47
S… Speaker 2 (11- Generic Classes and Inheritance)
Now here we should pass this value to the constructor of the base class.
0:50
S… Speaker 1 (11- Generic Classes and Inheritance)
And we do that using the super keyword.
0:52
S… Speaker 2 (11- Generic Classes and Inheritance)
So super and pass the points.
0:55
S… Speaker 1 (11- Generic Classes and Inheritance)
The problem is gone.
0:56
S… Speaker 1 (11- Generic Classes and Inheritance)
So here's our instructor class.
0:58
S… Speaker 2 (11- Generic Classes and Inheritance)
Now back in the main class.
1:01
S… Speaker 2 (11- Generic Classes and Inheritance)
Here we can declare a variable of type user.
1:04
S… Speaker 1 (11- Generic Classes and Inheritance)
We call it user.
1:06
S… Speaker 2 (11- Generic Classes and Inheritance)
Now we can set this to a new user or a
1:10
S… Speaker 2 (11- Generic Classes and Inheritance)
new instructor.
1:11
S… Speaker 2 (11- Generic Classes and Inheritance)
Because an instructor is a type of user.
1:14
S… Speaker 1 (11- Generic Classes and Inheritance)
So let's pass 10 here.
1:15
S… Speaker 1 (11- Generic Classes and Inheritance)
This is basic object oriented programming.
1:18
S… Speaker 2 (11- Generic Classes and Inheritance)
If this concept is foreign to you,
1:20
S… Speaker 2 (11- Generic Classes and Inheritance)
you really need to go back and watch the second part of this series.
1:23
S… Speaker 2 (11- Generic Classes and Inheritance)
So if you have a variable of type user,
1:25
S… Speaker 2 (11- Generic Classes and Inheritance)
we can set it to an instance of the user class or any of its derivatives
1:30
S… Speaker 2 (11- Generic Classes and Inheritance)
like the instructor class.
1:31
S… Speaker 1 (11- Generic Classes and Inheritance)
Now,
1:32
S… Speaker 1 (11- Generic Classes and Inheritance)
why does this matter?
1:33
S… Speaker 2 (11- Generic Classes and Inheritance)
Well,
1:34
S… Speaker 2 (11- Generic Classes and Inheritance)
let's go back to our utils class and add a new method for printing
1:38
S… Speaker 1 (11- Generic Classes and Inheritance)
a user.
1:38
S… Speaker 2 (11- Generic Classes and Inheritance)
So public static void print user,
1:42
S… Speaker 2 (11- Generic Classes and Inheritance)
we give it a parameter of type user and here
1:47
S… Speaker 2 (11- Generic Classes and Inheritance)
we do a print line of user.
1:48
S… Speaker 1 (11- Generic Classes and Inheritance)
Since we get a user here
1:51
S… Speaker 2 (11- Generic Classes and Inheritance)
we can pass an instance of the user class or any of its derivatives.
1:55
S… Speaker 2 (11- Generic Classes and Inheritance)
So back in the main class,
1:57
S… Speaker 1 (11- Generic Classes and Inheritance)
we can call utils dot print user.
2:01
S… Speaker 2 (11- Generic Classes and Inheritance)
Here we can pass a user object or an instructor object.
2:05
S… Speaker 1 (11- Generic Classes and Inheritance)
Perfectly fine.
2:07
S… Speaker 1 (11- Generic Classes and Inheritance)
Now,
2:08
S… Speaker 1 (11- Generic Classes and Inheritance)
what if we expected a list of users here?
2:10
S… Speaker 2 (11- Generic Classes and Inheritance)
So I'm going to go back to the utils class and add another method
2:15
S… Speaker 1 (11- Generic Classes and Inheritance)
for printing a list of users.
2:18
S… Speaker 2 (11- Generic Classes and Inheritance)
So public static void print users.
2:22
S… Speaker 2 (11- Generic Classes and Inheritance)
the type of the parameter should be generic list of user,
2:26
S… Speaker 1 (11- Generic Classes and Inheritance)
and we call it users.
2:28
S… Speaker 2 (11- Generic Classes and Inheritance)
Now don't worry about the implementation.
2:32
S… Speaker 2 (11- Generic Classes and Inheritance)
So this method takes a generic list of users.
2:35
S… Speaker 2 (11- Generic Classes and Inheritance)
Now back to the main class,
2:37
S… Speaker 2 (11- Generic Classes and Inheritance)
we can create a generic list of users,
2:41
S… Speaker 1 (11- Generic Classes and Inheritance)
new generic list of user,
2:43
S… Speaker 2 (11- Generic Classes and Inheritance)
and pass it to this method,
2:46
S… Speaker 2 (11- Generic Classes and Inheritance)
so utils .print users,
2:48
S… Speaker 2 (11- Generic Classes and Inheritance)
and that's perfectly fine,
2:50
S… Speaker 2 (11- Generic Classes and Inheritance)
but if we change this to instructor,
2:53
S… Speaker 2 (11- Generic Classes and Inheritance)
we get a compilation error.
2:56
S… Speaker 2 (11- Generic Classes and Inheritance)
Because a generic list of instructor is not a subtype
3:00
S… Speaker 2 (11- Generic Classes and Inheritance)
of generic list of user.
3:02
S… Speaker 2 (11- Generic Classes and Inheritance)
Why is that?
3:03
S… Speaker 2 (11- Generic Classes and Inheritance)
Well, earlier we talked about how these generic classes work under
3:07
S… Speaker 1 (11- Generic Classes and Inheritance)
the hood.
3:07
S… Speaker 2 (11- Generic Classes and Inheritance)
We looked at the bytecode representation of this generic list class.
3:11
S… Speaker 1 (11- Generic Classes and Inheritance)
As I showed you earlier,
3:12
S… Speaker 2 (11- Generic Classes and Inheritance)
this generic list internally maintains a list of objects no
3:17
S… Speaker 2 (11- Generic Classes and Inheritance)
matter what we pass here.
3:18
S… Speaker 2 (11- Generic Classes and Inheritance)
whether we pass the user class or the instructor class,
3:21
S… Speaker 2 (11- Generic Classes and Inheritance)
we only have a single generic list in our project.
3:24
S… Speaker 2 (11- Generic Classes and Inheritance)
And this class is not a subtype of itself.
3:27
S… Speaker 2 (11- Generic Classes and Inheritance)
That is the reason why a generic list of instructor is not a
3:31
S… Speaker 2 (11- Generic Classes and Inheritance)
subtype of generic list of user because we're dealing with a single class.
3:35
S… Speaker 2 (11- Generic Classes and Inheritance)
Okay, so how can we solve this problem here?
3:38
S… Speaker 2 (11- Generic Classes and Inheritance)
Let's say we have a list of instructors somewhere.
3:40
S… Speaker 2 (11- Generic Classes and Inheritance)
So let's call this instructors.
3:42
S… Speaker 2 (11- Generic Classes and Inheritance)
Now you really want to pass this list to
3:47
S… Speaker 2 (11- Generic Classes and Inheritance)
our print users method.
3:48
S… Speaker 2 (11- Generic Classes and Inheritance)
Well, one way is to convert this list to a list of users.
3:52
S… Speaker 2 (11- Generic Classes and Inheritance)
So we declare a list of users.
3:55
S… Speaker 2 (11- Generic Classes and Inheritance)
Then we have to iterate over the list
3:59
S… Speaker 2 (11- Generic Classes and Inheritance)
of instructors and add each instructor object to our
4:03
S… Speaker 2 (11- Generic Classes and Inheritance)
list of users.
4:03
S… Speaker 2 (11- Generic Classes and Inheritance)
Once we do that,
4:05
S… Speaker 2 (11- Generic Classes and Inheritance)
we'll be able to pass the list of users here.
4:08
S… Speaker 1 (11- Generic Classes and Inheritance)
But this is tedious,
4:10
S… Speaker 2 (11- Generic Classes and Inheritance)
right?
4:10
S… Speaker 1 (11- Generic Classes and Inheritance)
In the next video,
4:11
S… Speaker 2 (11- Generic Classes and Inheritance)
I'm going to show you how to solve this problem using wildcards.

This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy

❤️ प्रेम STT.ai? आफ्नो साथीहरूलाई भन्नुहोस्!
सारांश
यो लिखितको AI सारांश उत्पन्न गर्न सारांश गर्नुहोस् क्लिक गर्नुहोस् ।
सारांश गर्दैछ...
यो प्रतिलिपि बारे AI सोध्नुहोस्
यो transcript बारेमा केही सोध्नुहोस् - एआई सम्बन्धित खण्डहरू र जवाफ पाउनुहुनेछ.