1 00:00:02,020 --> 00:00:06,490 High this the strings of the heart and welcome to the next value of this Mordieu will all functions 2 00:00:06,850 --> 00:00:11,410 in this video will really talk about Vawter extension function in case of Cartland. 3 00:00:11,750 --> 00:00:18,830 Now the purpose of extension function is to add a new function to the existing class. 4 00:00:19,040 --> 00:00:25,830 So it simply adds a particular function to a predefined class without declaring it inside the class. 5 00:00:27,100 --> 00:00:33,160 And the new functions actually behaves just like the static functions in case of Java. 6 00:00:33,190 --> 00:00:39,970 Now practically also during the completion the Cartland compiler also converts these functions into 7 00:00:39,970 --> 00:00:44,160 the static functions internally that happens behind the curtain. 8 00:00:44,260 --> 00:00:49,580 So we do not see what happens behind the curtain but exactly this happens. 9 00:00:49,660 --> 00:00:54,220 So that does not check out the extension function inside the intelligent ID. 10 00:00:54,250 --> 00:01:01,250 So first I would simply create a class of let's see student insert this I would simply define a function. 11 00:01:01,270 --> 00:01:03,760 So here I have a method of has first. 12 00:01:03,790 --> 00:01:08,800 So if the student has passed the exam or not will be determined inside this method. 13 00:01:08,830 --> 00:01:14,410 So the Mock's is greater than 40 then going through is three and four is because the student has failed 14 00:01:14,650 --> 00:01:19,720 not to simplify this code by simply removing the redundant called. 15 00:01:19,720 --> 00:01:23,990 So here I'm simply returning if the marks is greater than 40 then returned true. 16 00:01:24,130 --> 00:01:29,590 Is that again false right now insert the main method I simply define a chord. 17 00:01:29,620 --> 00:01:35,230 So here I have simply written the code by simply defining the object of the other in class and then 18 00:01:35,230 --> 00:01:35,520 here. 19 00:01:35,530 --> 00:01:38,680 I'm simply passing the marks of the student as 57. 20 00:01:38,680 --> 00:01:44,730 So if the student has first exam or not will be determined with the help of discord right now. 21 00:01:44,840 --> 00:01:46,380 Suppose this is our. 22 00:01:46,540 --> 00:01:49,740 Now this class is actually our own class right. 23 00:01:49,870 --> 00:01:52,790 So we are the sole owner of this class of student. 24 00:01:52,960 --> 00:01:59,500 Now suppose this class is already defined and later on in your application you need one more to function 25 00:01:59,560 --> 00:02:01,520 inside the class of student. 26 00:02:01,750 --> 00:02:04,580 So so without defining the function right. 27 00:02:04,600 --> 00:02:11,490 Inside the class what you can do is you can simply create a One more function like C function has got 28 00:02:11,590 --> 00:02:12,900 scholarship or not. 29 00:02:12,960 --> 00:02:14,520 Or let's say is Scollard. 30 00:02:14,560 --> 00:02:21,290 So if the student has secured the marks greater than 95 then he is actually a scholar as he is not. 31 00:02:21,430 --> 00:02:24,520 So this function should be the part of law student. 32 00:02:24,590 --> 00:02:30,450 But for that we have to find this method inside this class of student but we don't want to do it. 33 00:02:30,460 --> 00:02:36,440 So what they really do is I would simply use the concept of extension function where the syntax is let's 34 00:02:36,450 --> 00:02:37,060 see. 35 00:02:37,120 --> 00:02:43,910 You need to use the class name of then come on and see and paste it here followed by the dot operator. 36 00:02:44,050 --> 00:02:48,790 So here Varda of then is I've simply used student dord is scholar. 37 00:02:48,910 --> 00:02:56,830 So whenever I use this syntax and this function is Scollard will behave like a function present inside 38 00:02:56,830 --> 00:02:58,580 the class of student. 39 00:02:58,600 --> 00:03:00,120 So here I can simply use. 40 00:03:00,190 --> 00:03:03,630 Let's see Parentline scholarships status. 41 00:03:03,730 --> 00:03:06,260 Student dord is scholar right. 42 00:03:06,280 --> 00:03:10,090 And simply Pazner the marks of the student as let's say 57. 43 00:03:10,180 --> 00:03:12,050 So here if I add in the call right now. 44 00:03:12,160 --> 00:03:15,100 Then in the output we will get something like that. 45 00:03:15,290 --> 00:03:18,490 Scholarships stators either true or false. 46 00:03:18,520 --> 00:03:21,180 So here we get the scholarships tutors as follows. 47 00:03:21,190 --> 00:03:26,980 And the pastor has as you know this scholarship students as far as it's coming from the extension function 48 00:03:27,160 --> 00:03:28,670 right here like this. 49 00:03:28,670 --> 00:03:34,900 Now if I tell you that this is actually not the practical usage of the extension function. 50 00:03:34,900 --> 00:03:39,330 So what he would have he saw right now was just an explanation of the extension function. 51 00:03:39,400 --> 00:03:40,510 How do we define it. 52 00:03:40,510 --> 00:03:42,580 The syntax is like this. 53 00:03:42,580 --> 00:03:49,200 Now let us come to the point where it has defined a function and check out the practical usage of the 54 00:03:49,200 --> 00:03:53,000 extension function so that it's not genes are called. 55 00:03:53,000 --> 00:03:59,860 Now suppose I define a string see SD or one other type of string equal to hello. 56 00:03:59,990 --> 00:04:02,330 And now let us define one more string. 57 00:04:02,330 --> 00:04:08,390 So here I've simply defined two string values string one and string two other type of hello and void 58 00:04:08,540 --> 00:04:09,140 value. 59 00:04:09,200 --> 00:04:15,220 And now I suppose if I define one more SDR tree of the type of string equal to. 60 00:04:15,350 --> 00:04:16,710 Let's see. 61 00:04:17,120 --> 00:04:22,280 Right now suppose later on in our program I want to combine these three strings. 62 00:04:22,280 --> 00:04:25,710 That is I'm going to append these three strings together. 63 00:04:25,730 --> 00:04:33,710 For example we can simply use geometry equal to SDR three less SDR even less SDR. 64 00:04:33,810 --> 00:04:34,850 Do like this. 65 00:04:34,850 --> 00:04:36,490 So this will work fine. 66 00:04:36,500 --> 00:04:39,110 So in this way we can simply append three strings. 67 00:04:39,110 --> 00:04:46,140 Now suppose I want to define a function that is I want to use the string plus to add these three strings. 68 00:04:46,150 --> 00:04:46,610 Right. 69 00:04:46,730 --> 00:04:51,740 So what they will do is simply try to find the solution by using SDR 3 dord. 70 00:04:51,800 --> 00:04:56,830 Let's say I have any Metford That simply adds all the strings together or not. 71 00:04:57,200 --> 00:05:03,010 So here I don't have any matter that takes two more metres and simply add all the strings together. 72 00:05:03,170 --> 00:05:04,820 So what we do is down the side. 73 00:05:04,820 --> 00:05:10,670 I would simply create the extension function that is let us create a function inside which we will add 74 00:05:10,700 --> 00:05:12,280 all the strings together right. 75 00:05:12,290 --> 00:05:17,080 So I will simply create an extension function for the class of string string dord. 76 00:05:17,120 --> 00:05:22,470 Let's see and right and now as a it I simply simply pass. 77 00:05:22,490 --> 00:05:28,290 Let's say as diehard latest string one of that type of string of their type of string again. 78 00:05:28,360 --> 00:05:28,910 Right. 79 00:05:29,000 --> 00:05:33,290 And this simply return appended string. 80 00:05:33,510 --> 00:05:40,100 And now here I will simply use a written statement and I would simply add this. 81 00:05:40,470 --> 00:05:44,060 Less is even less true. 82 00:05:44,200 --> 00:05:46,530 Now I will explain right now why it is happening. 83 00:05:46,530 --> 00:05:52,500 So I will simply use your SD card or add and simply pass SDR. 84 00:05:52,860 --> 00:06:00,310 When an SD card too right and now let us print the cord by simply using the print LNA statement SD or 85 00:06:00,390 --> 00:06:07,980 treat the statement from here and pasted here and now let us run the code and lets see what happens. 86 00:06:08,350 --> 00:06:09,990 So in the output we get. 87 00:06:10,040 --> 00:06:11,240 Hey hello world. 88 00:06:11,380 --> 00:06:13,520 So what exactly is happening here. 89 00:06:13,660 --> 00:06:16,640 So here I have simply defined the extension function of. 90 00:06:17,200 --> 00:06:22,320 Now this extension function and is now a part of this class of string. 91 00:06:22,330 --> 00:06:25,540 Now this string is actually the predefined class who scored. 92 00:06:25,540 --> 00:06:26,820 We have not written. 93 00:06:26,830 --> 00:06:32,210 It is already existing in our SDK so we don't know how to modify this class. 94 00:06:32,230 --> 00:06:34,600 But in Codlin there is a way to do it. 95 00:06:34,670 --> 00:06:42,340 We can simply add a function inside a predefined class so this function is now is a part of this class 96 00:06:42,440 --> 00:06:44,090 right of the string right. 97 00:06:44,300 --> 00:06:47,980 And now here it takes to be the meter's string and string to. 98 00:06:48,070 --> 00:06:51,670 And here I'm calling this method with the help of this SDR tree. 99 00:06:51,670 --> 00:06:56,390 Now your tree is defined here which is actually of their type of string. 100 00:06:56,400 --> 00:07:01,440 Now as already said everything in Gartley is actually an object. 101 00:07:01,510 --> 00:07:07,770 So this string tree is actually an object so very simply calling objects don't act Metford. 102 00:07:07,820 --> 00:07:14,110 So a string object don't add method right just in a way that we call any other method with the help 103 00:07:14,110 --> 00:07:14,990 of the object. 104 00:07:15,220 --> 00:07:21,760 So this is Tier 3 simply cause this method and simply bus stupid leaders and inside this method I'm 105 00:07:21,820 --> 00:07:29,980 using this list is endless as to now this keyword is actually referring to this SDR tree that actually 106 00:07:29,980 --> 00:07:32,580 holds the reference for the string tree. 107 00:07:32,680 --> 00:07:40,510 Now string tree is actually has a value of here as one has a value of hello and two has the value of 108 00:07:40,690 --> 00:07:41,300 void. 109 00:07:41,380 --> 00:07:42,250 So we are agreeing. 110 00:07:42,260 --> 00:07:47,790 Hey hello way to get that appended and returned inside the print L-N mattered and we're getting the 111 00:07:47,800 --> 00:07:50,270 output as hey hello world write. 112 00:07:50,470 --> 00:07:52,970 And now let us take one more example. 113 00:07:53,170 --> 00:08:02,290 Let's straight to find a greater number with the help of the integer class Val X like C equal to 6 and 114 00:08:02,290 --> 00:08:06,180 then I would simply use Val VEIDT equal to 10. 115 00:08:06,190 --> 00:08:09,960 Now these two are actually of their type of right. 116 00:08:09,970 --> 00:08:12,040 So don't forget this. 117 00:08:12,090 --> 00:08:13,770 It is also their type of end. 118 00:08:13,990 --> 00:08:19,990 Now here there is no predefined function where we can simply call legacy X.. 119 00:08:20,020 --> 00:08:24,190 Dog find the greater number like that or greater number. 120 00:08:24,190 --> 00:08:29,170 We don't have any Metford inside the integer class that does this in a class so for. 121 00:08:29,230 --> 00:08:34,070 Again on that side I will simply define the extension function for the class of NBG. 122 00:08:34,250 --> 00:08:37,900 Dark large see greater value. 123 00:08:38,020 --> 00:08:39,950 And here I will simply pass one. 124 00:08:39,970 --> 00:08:42,970 I mean that because we are having to NBG the values S.N.. 125 00:08:43,000 --> 00:08:49,090 So the first one will fall here and the second one will pass on with the help of all the other number 126 00:08:49,360 --> 00:08:50,630 of their type of. 127 00:08:50,890 --> 00:08:54,310 And it simply returns the integer value right. 128 00:08:54,400 --> 00:08:57,610 This is greater than the other. 129 00:08:57,760 --> 00:09:00,200 So here I've simply completed my code. 130 00:09:00,280 --> 00:09:03,360 So inside this method I have written the if condition. 131 00:09:03,370 --> 00:09:08,890 If this is better than other number then simply return the correct number it is returned the other number 132 00:09:08,990 --> 00:09:09,500 right. 133 00:09:09,670 --> 00:09:17,200 So if we are calling X Dorte greater value and simply pass the other value as by. 134 00:09:17,320 --> 00:09:17,830 Right. 135 00:09:17,980 --> 00:09:26,200 So here we will get the value as well greater value equal to extraordinary Ravelli Why not simply print 136 00:09:26,200 --> 00:09:29,830 out let's see greater value. 137 00:09:29,860 --> 00:09:37,070 So when the street is executed exhorted revalue is simply going to call this function in dark revalue 138 00:09:37,510 --> 00:09:40,370 which is not a part of this interior glass. 139 00:09:40,390 --> 00:09:41,520 Right. 140 00:09:41,550 --> 00:09:49,510 And now here this refers to the value of x the object that is calling this method the value of that 141 00:09:49,600 --> 00:09:52,900 object is actually stored inside this key word. 142 00:09:52,930 --> 00:09:53,580 Right. 143 00:09:53,590 --> 00:09:56,560 So this refers to the correct reference of the object. 144 00:09:56,680 --> 00:10:00,900 And now the other one is actually the parameter that we are passing here. 145 00:10:01,000 --> 00:10:02,170 With the help of y. 146 00:10:02,200 --> 00:10:03,220 Very well. 147 00:10:03,240 --> 00:10:09,130 So if the current value is greater than the other value then simply return this otherwise return the 148 00:10:09,130 --> 00:10:12,090 other value which is not a greater value. 149 00:10:12,220 --> 00:10:14,070 So let us note in the chord. 150 00:10:14,210 --> 00:10:20,570 So here in the output we have as a value which is a greater number among 6 and 10 right. 151 00:10:20,570 --> 00:10:25,300 So this is the way you can use the extension function in case of Cartland. 152 00:10:25,310 --> 00:10:28,780 Now what are the advantages of using the extension function. 153 00:10:28,820 --> 00:10:29,800 Let's check it out. 154 00:10:30,540 --> 00:10:37,160 So the extension function has few properties such as they can become part of your own plussed example 155 00:10:37,170 --> 00:10:39,910 student employee user and so on. 156 00:10:40,050 --> 00:10:46,110 And the other thing is that they can become part of the predefined classes such as add a string integer 157 00:10:46,110 --> 00:10:51,890 Gad or any predefined classes that comes with that Gartin is the key right. 158 00:10:52,170 --> 00:10:53,560 So all of this remember this. 159 00:10:53,700 --> 00:11:00,510 You can make the part of your custom class such as student Derocher you saw in this video and also for 160 00:11:00,510 --> 00:11:05,020 the predefined classes such as string and and so on. 161 00:11:05,080 --> 00:11:11,170 And now what are the advantages of using the extension function is that it simply reduces the code and 162 00:11:11,170 --> 00:11:18,070 it makes the code much more cleaner and easier to read Serj simply reduces scored by allowing you to 163 00:11:18,090 --> 00:11:26,200 add your own custom methods inside some class so you don't have to modify your old classes or the predefined 164 00:11:26,200 --> 00:11:29,160 classes for just one function. 165 00:11:29,200 --> 00:11:33,380 So this is all for this video about the extension function in case of Cartland. 166 00:11:33,790 --> 00:11:35,480 Thanks for watching and have a good day.