WEBVTT

00:00.450 --> 00:08.400
High in this session, we will discuss about polymorphism, so we have known that Wii functions can

00:08.400 --> 00:11.010
take different arguments.

00:11.340 --> 00:16.190
Methods belong to the object they act on, right.

00:16.470 --> 00:23.690
So we have methods which act on top of different object.

00:23.880 --> 00:28.830
So when we create a class, the class will contain different functions.

00:28.830 --> 00:32.610
And only these functions are called methods.

00:32.850 --> 00:36.910
And these methods will be working on the objects.

00:37.410 --> 00:37.750
Right.

00:37.980 --> 00:44.430
Whatever task I will be doing, whatever task my method will be performing, that task will be performed

00:44.430 --> 00:46.530
on top of my object.

00:46.920 --> 00:56.610
OK, now in Python polymorphism refers to the way in which different object classes can share the same

00:56.610 --> 01:06.930
method name and those methods can be called from the same place, even though a variety of different

01:06.930 --> 01:09.150
objects might be Boston.

01:09.780 --> 01:17.760
OK, so I can barson different types of object and my method can be called from the same place so I

01:17.760 --> 01:24.330
can have the same method acting differently for different types of object.

01:24.810 --> 01:27.290
OK, so how do we explain that.

01:27.300 --> 01:36.570
So basically polymorphism is a property of an object which allows it to take multiple form.

01:36.870 --> 01:44.130
OK, so when an object can take multiple forms and when we can work on different forms of an object

01:44.130 --> 01:52.200
that is called polymorphism, so it allows us to define same method in different classes so I can have

01:52.260 --> 01:53.470
the same method.

01:53.520 --> 02:03.920
Let's say I can have the same method as speak, OK, and this week can be present in case of dog also

02:03.930 --> 02:05.510
and in case of Catalyser.

02:05.940 --> 02:09.940
But how the output comes out to be would be different.

02:10.170 --> 02:14.150
So a dog would say, Wolf, while a dog would say meow.

02:14.340 --> 02:16.710
So those are the differences.

02:16.740 --> 02:24.830
So I have the same method speak, but we acted differently in case depending on if I'm calling in dog

02:24.840 --> 02:27.270
object or I'm calling the cat object.

02:28.300 --> 02:28.680
Right.

02:29.010 --> 02:34.860
So this is also known as method overriding the veto override, a particular method.

02:34.860 --> 02:36.900
We change how a method works.

02:37.450 --> 02:43.640
OK, now an example of this would be so we use GPS for navigating around.

02:43.890 --> 02:50.460
So when we come across many different routes for same destination, depending on the traffic from programming

02:50.460 --> 02:56.630
point of view, this is called polymorphism basically suggests us different route.

02:56.650 --> 02:58.500
So that is called polymorphism.

02:58.500 --> 03:02.790
So when same thing and have different solutions, which is called polymorphism.

03:03.160 --> 03:08.680
OK, so here we have, let's see, ADR method, which will have different implementations for specific

03:08.770 --> 03:09.710
rectangular cube.

03:09.960 --> 03:17.640
So for all of these, the method area will remain the same, but how it acts would change.

03:17.820 --> 03:22.740
So for Sfeir it will calculate the area differently for so-called beguilingly the.

03:23.340 --> 03:29.700
So that is what polymorphism is and it is one such methodology and Opsware one task and be performed

03:29.700 --> 03:31.810
in several different ways.

03:32.070 --> 03:35.460
So that is exactly what polymorphism is.

03:35.640 --> 03:42.660
Then we can use the same name for a method, but it will act differently based on what my object is,

03:42.930 --> 03:44.590
the nature of my object.

03:44.590 --> 03:47.340
So based on the nature of our flag differently.

03:48.210 --> 03:53.490
OK, so let's go ahead with the coding part of it.

03:57.660 --> 03:57.990
OK.

04:06.100 --> 04:11.230
So here I'm creating a class, this class is called your dog.

04:11.290 --> 04:14.580
And inside this, I will define the method.

04:14.920 --> 04:24.180
So first of all, I define in it and it will take in self, which is by default and it will take in

04:24.190 --> 04:26.530
the next.

04:26.950 --> 04:33.130
It will say self, not name is equal to a name.

04:33.920 --> 04:38.560
And let me define another method.

04:38.560 --> 04:44.140
Say this and I will speak so in speak.

04:44.140 --> 04:47.110
I will take says I don't need anything else.

04:47.320 --> 04:48.400
I'm inside this.

04:48.400 --> 04:55.720
I will say that Don and I will say self dog name plus

04:58.300 --> 05:07.720
says if I put this in quotes.

05:11.200 --> 05:13.770
So this is one of my string.

05:14.050 --> 05:18.130
Similarly, I create a similar class for CAT.

05:20.320 --> 05:28.590
So when I create this class for CAT and change its name for From Dog to God and everything else remains

05:28.600 --> 05:29.020
the same.

05:29.020 --> 05:32.020
But here the cat will instead see me.

05:35.850 --> 05:45.960
OK, now you can see here the speaker is present in case of class dog, and this method is saying,

05:45.960 --> 05:54.450
Wolf, as a result and in case of Gatica saying now and we have the same method, the name, OK, so

05:54.450 --> 05:56.510
we are using the same method names.

05:56.510 --> 06:06.150
So when I create, let's say, some examples, I'm creating a dog Dominico and I see a dog and I give

06:06.150 --> 06:08.790
it a name, Nikou.

06:09.960 --> 06:19.890
And similarly, I create a cat Feliks and I'll give it the name, the same as that.

06:21.640 --> 06:31.950
And after that, when we will simply print, we print Nicole don't speak.

06:32.670 --> 06:34.620
And similarly, we print.

06:37.060 --> 06:48.790
Felix, don't speak, and we will have to run this method, so we'll run this method and when we will

06:48.790 --> 06:50.650
run this, what will happen?

06:51.130 --> 06:59.220
So it gives me Niko says, well, Felix is like in outer space here so that it looks a little better.

07:00.550 --> 07:01.230
So you can see.

07:01.540 --> 07:07.570
So what we have achieved here is we have achieved the same method.

07:07.900 --> 07:15.640
We have implemented the same method of name, and we have achieved a different behavior out of those.

07:15.910 --> 07:19.060
So I can have any number of animals.

07:19.060 --> 07:27.520
I can have double got a cow and horses and pigs and whatever, and all of those can have their different

07:27.520 --> 07:28.030
sound.

07:28.030 --> 07:35.680
And for all I can use the same method and it will be so very convenient to use this.

07:35.890 --> 07:42.770
And I will not be confused by finding out while remembering what me might have used.

07:42.940 --> 07:46.450
So this is the convenience which will provide you.

07:47.470 --> 07:57.400
So here we have a dog class and a class and each has a dog speak method when called the object as we

07:57.750 --> 07:58.420
speak.

07:58.420 --> 08:06.730
So we are calling these objects as dog speak and they will retain redounded unique object.

08:06.740 --> 08:11.920
So the result will be unique based on what object they have.

08:12.470 --> 08:20.410
So similarly we can have different things so we can implement these differently.

08:20.410 --> 08:25.390
I'll send them out during the code so you will find those in code.

08:25.960 --> 08:38.890
Now, in case of classes, it is a more common practice where we can use abstract classes and inheritance.

08:39.190 --> 08:45.400
So we have seen inheritance or you're right, so here we have seen an inheritance.

08:45.410 --> 08:51.640
So here we have a class animal and we have created some particular method here.

08:51.640 --> 08:56.860
And this method is being overridden here in the US.

08:57.250 --> 09:05.170
But there could be a scenario where I want to create some method in case of the best in class and I

09:05.170 --> 09:09.720
want to implement those only in the class or something like that.

09:09.940 --> 09:18.520
So that is the place where we can use abstract class or an abstract class is one that never expected

09:18.520 --> 09:19.930
to be instantiated.

09:19.930 --> 09:23.980
So it will not expect any instantiation of.

09:23.980 --> 09:26.860
So we will never have an animal object.

09:27.100 --> 09:33.410
Only dog and cat object will be present, although dog and cat are derived from the animals.

09:33.610 --> 09:36.680
So we will be creating something like that.

09:36.760 --> 09:41.350
We will be creating dog and cat classes which will be derived from the animal flies.

09:41.620 --> 09:45.220
But in case of animal class, we won't instantiate that ever.

09:46.690 --> 09:50.080
So let's do that without losing any time.

09:50.350 --> 09:52.330
So I will say class

09:54.700 --> 09:58.840
animal and I see the.

10:12.830 --> 10:20.570
So what I have done here is I have created this class animal inside this class, I have given the definition

10:20.570 --> 10:24.980
for in it and I have given the attribute name inside this.

10:25.850 --> 10:33.980
After that, I have given the function, the method to speak and for this method speak.

10:33.980 --> 10:40.280
I am saying that the reason is not implemented error, saying that subclause must be implementing this

10:40.280 --> 10:41.060
abstract method.

10:41.480 --> 10:45.460
OK, so abstract method is defined by conventional.

10:45.670 --> 10:47.480
So this is defined like this only.

10:48.150 --> 10:50.480
OK, this will be defined like this.

10:50.900 --> 10:57.290
And after that I'm creating this class and this guy does just how we have implemented it.

10:57.290 --> 11:05.540
Olio in the same way we have simply said to the class dog and we have said def speak, self written,

11:05.540 --> 11:09.100
self taught name Seawolf said don't mean meal.

11:09.440 --> 11:17.360
And then I see pre the class create an object dog and create an object for God.

11:17.570 --> 11:21.560
And then I bring to the door and I start to speak.

11:22.730 --> 11:24.070
So let me run this.

11:24.350 --> 11:28.240
So it gives me Vidor's is Wolf ISIS ISIS's.

11:29.210 --> 11:33.440
So here what we have done is we have created this.

11:34.040 --> 11:41.590
Now let me do something like let me not define this here.

11:41.750 --> 11:45.600
So if I would not define these definitions.

11:45.600 --> 11:48.270
So let me not define it for just one class.

11:49.640 --> 11:55.700
So here it is seeing that it is expecting some intended block.

11:55.710 --> 12:02.600
So let me put parse here, first of all, so you can see it is giving out the not implemented error

12:02.600 --> 12:05.630
in same subclass must be implemented abstract method.

12:05.900 --> 12:08.390
So it is saying that it needs to implement this.

12:08.830 --> 12:11.670
OK, so that is what this means.

12:11.690 --> 12:12.860
Or an abstract class.

12:12.860 --> 12:23.540
We can create an abstract class by defining this is not implemented error and that we make it mandatory

12:23.840 --> 12:28.400
for us to implement it under the child classes.

12:29.960 --> 12:38.660
OK, so in real life, examples of what polymorphism we can have, something like when we are opening

12:38.660 --> 12:45.420
different file types so different tools are needed to display so would be difficult.

12:45.950 --> 12:46.480
Exactly.

12:46.520 --> 12:47.300
Exactly right.

12:47.780 --> 12:52.760
So in those case we can have a different kind of file.

12:52.760 --> 12:57.620
So different kind of handling for different files and they can have different methods for that.

12:57.840 --> 13:04.340
We can use the same method, open file, but each file will be opened differently based on the life

13:04.340 --> 13:04.910
from it.

13:04.910 --> 13:09.390
Just in another example would be when we are adding different object.

13:10.490 --> 13:18.530
So in that case, the class operator will perform an automatic operation by when we are adding the strings,

13:18.530 --> 13:21.460
it will this act as a concatenation.

13:21.770 --> 13:22.110
Right.

13:22.280 --> 13:27.950
So these are different examples that we have now.

13:27.950 --> 13:32.690
Apart from that, the next topic which we have is encapsulation.

13:32.690 --> 13:36.040
So we will be discussing that in the next session.
