1 00:00:00,890 --> 00:00:02,170 Heidi is a sharing satire. 2 00:00:02,180 --> 00:00:08,420 And welcome to the next Mordieu of object oriented programming in case of Cartland now in Godling we 3 00:00:08,420 --> 00:00:13,630 have some concept of object declaration and also companion objects. 4 00:00:13,640 --> 00:00:19,260 But before learning that let us first check out what is singleton in Scotland. 5 00:00:19,490 --> 00:00:25,720 Now in case of Codlin whenever we declare a clause that we're doing so far we create instance of that 6 00:00:25,730 --> 00:00:33,620 class and then access all the variables and methods but in case of Singleton we have just one instance 7 00:00:33,620 --> 00:00:36,110 of the class in the whole application. 8 00:00:36,170 --> 00:00:44,330 So once we define class as singleton then we just have one instance and that instance is just created 9 00:00:44,540 --> 00:00:46,300 by Cartland internally. 10 00:00:46,340 --> 00:00:52,070 That is we do not have any control over creating the instance of that singleton class. 11 00:00:52,090 --> 00:00:57,110 No it means that Suppose we have a class today which is declared as singleton. 12 00:00:57,260 --> 00:01:04,360 Then we cannot create the objects of this class that is there exists only one object for this class 13 00:01:04,410 --> 00:01:08,600 by default which is created by the Codlin internally. 14 00:01:08,630 --> 00:01:12,340 In simple words we can say we cannot create objects like student 1. 15 00:01:12,350 --> 00:01:14,320 STUDENT 2 and so on. 16 00:01:14,360 --> 00:01:16,560 I hope I am clear at this point. 17 00:01:16,760 --> 00:01:24,440 And now in Java they define singleton by using the keyword of static such as we have static variables 18 00:01:24,500 --> 00:01:27,110 static methods present inside the class. 19 00:01:27,140 --> 00:01:32,270 So in that case the static variables and metrics are not a part of the class. 20 00:01:32,300 --> 00:01:37,600 They are just part of class definition not the objects right. 21 00:01:37,600 --> 00:01:39,640 So in guess of Gorlin as well. 22 00:01:39,640 --> 00:01:43,750 We don't have any static keyword present but alternative to that. 23 00:01:43,750 --> 00:01:48,840 We have some object declaration and also the concept of companion objects. 24 00:01:48,950 --> 00:01:56,220 No proceeding forward in Gondolin we can not declare static variables and methods compared to Jawa. 25 00:01:56,470 --> 00:02:04,330 So we have an alternative to diked such as we can declare object and these objects are actually acting 26 00:02:04,360 --> 00:02:10,950 as a class that can contain the static variables and methods without using the keyword of static. 27 00:02:10,980 --> 00:02:17,770 Now when we declare something as object that Coquelin internally behind the curtain simply creates a 28 00:02:17,770 --> 00:02:20,960 singleton object for us when the program is running. 29 00:02:21,190 --> 00:02:25,140 So when we are in the program we get one instance of that object. 30 00:02:25,150 --> 00:02:29,030 So how do we declare the object in case of Codlin. 31 00:02:29,060 --> 00:02:30,230 So in Cartland. 32 00:02:30,460 --> 00:02:32,300 Let's check out the code snippet. 33 00:02:32,390 --> 00:02:34,470 Suppose we have object customer. 34 00:02:34,490 --> 00:02:39,250 Now here we don't have Klasky word we are using object keyword. 35 00:02:39,320 --> 00:02:40,920 So object space custom. 36 00:02:40,940 --> 00:02:43,240 This is syntax instead of. 37 00:02:43,340 --> 00:02:47,330 Suppose I have variable The other type of integer. 38 00:02:47,330 --> 00:02:52,330 Now this ID is actually acting as a static variable compared to Java. 39 00:02:52,640 --> 00:02:58,280 This is not exactly a static but behaving like a static variable like we call it static. 40 00:02:58,280 --> 00:03:03,630 In case of Java but I similarly suppose I have a function register customer. 41 00:03:03,860 --> 00:03:07,640 So this method as well is behaving as a static method. 42 00:03:07,640 --> 00:03:10,900 Now then we call a static variable and static method. 43 00:03:10,940 --> 00:03:16,700 If you are from Java background then you must be knowing how to access the static variables and methods 44 00:03:17,110 --> 00:03:19,830 not to access these variables and methods. 45 00:03:20,000 --> 00:03:25,910 We need to use simply use customer door Id like say 27. 46 00:03:26,010 --> 00:03:28,070 So here we are simply using customer. 47 00:03:28,170 --> 00:03:32,160 This customer is the name of this object or right. 48 00:03:32,220 --> 00:03:36,490 So class name or the object name dot ID equal to 27. 49 00:03:36,630 --> 00:03:40,500 That looks similar to the code snippet from the Java background. 50 00:03:40,650 --> 00:03:46,810 And next we can call customer d'arte register customer and our record inside the function will be executed 51 00:03:47,150 --> 00:03:47,710 non-nudist. 52 00:03:47,850 --> 00:03:50,720 Here we are calling without creating any instance. 53 00:03:50,760 --> 00:03:54,960 That is we don't need to declare it as the object explicitly. 54 00:03:55,020 --> 00:04:01,290 The Codlin by default creates an object instance that is a singleton instance for our customer class 55 00:04:01,380 --> 00:04:07,850 or the object so let us proceed and check out the cold inside their Damore application. 56 00:04:07,880 --> 00:04:13,640 So here inside the intelligent ID I will simply declare the class of customer. 57 00:04:13,690 --> 00:04:20,300 So here inside the intelligent ID I've simply created an automated class of customers data that contains 58 00:04:20,450 --> 00:04:25,580 the customer's account and also type of customer that simply returns a string value. 59 00:04:25,580 --> 00:04:33,060 Now in order to access discount variable and also this method inside the main we need to do these called. 60 00:04:33,920 --> 00:04:39,350 So here in order to access the count variable and this method we are simply calling the object name 61 00:04:39,530 --> 00:04:42,470 Darktown an object name dot method. 62 00:04:42,470 --> 00:04:43,020 Right. 63 00:04:43,160 --> 00:04:46,010 By simply creating the customer's data object. 64 00:04:46,010 --> 00:04:53,060 Now if I want to use this class as a singleton so what I would do is I would simply use sort of class. 65 00:04:53,180 --> 00:04:54,720 I would simply use object. 66 00:04:54,830 --> 00:05:00,160 So this object keyword simply creates a singleton object for this customers data. 67 00:05:00,440 --> 00:05:07,100 So we no longer can create the instance of this customer data manually because we can create the instance 68 00:05:07,160 --> 00:05:09,420 of the single Going to us. 69 00:05:09,500 --> 00:05:12,420 It is done automatically for you. 70 00:05:12,470 --> 00:05:15,630 So here I have to remove this at the top level code. 71 00:05:15,920 --> 00:05:21,740 And now in order to access the scanned variable what I need to do is we need to simply use copy this 72 00:05:21,850 --> 00:05:27,310 last name and pasted in Baystate here and now again paste it here. 73 00:05:27,530 --> 00:05:31,980 So here discussing variable is actually acting like a static variable. 74 00:05:32,240 --> 00:05:35,380 And also this method is acting like a static method. 75 00:05:35,380 --> 00:05:35,810 Right. 76 00:05:35,930 --> 00:05:40,120 So this is how we access the members of the object like this. 77 00:05:40,130 --> 00:05:46,460 And now if aproned legacy a print Ellen let's say customers data are gone. 78 00:05:46,900 --> 00:05:49,450 Let's see the output. 79 00:05:49,460 --> 00:05:52,890 So here in the output we get 98 right. 80 00:05:52,940 --> 00:05:56,900 So this Gounder value is currently ninety eight that we have defined here. 81 00:05:56,900 --> 00:05:59,620 Now suppose if I again change the value. 82 00:05:59,660 --> 00:06:00,370 Let's see. 83 00:06:00,470 --> 00:06:02,780 Two hundred and nine. 84 00:06:03,110 --> 00:06:06,280 And let's see what happens if we are we're into splendored. 85 00:06:06,590 --> 00:06:09,050 Now this time we get a hundred and nine. 86 00:06:09,080 --> 00:06:10,700 So the value has been updated. 87 00:06:10,730 --> 00:06:11,960 Two hundred and nine. 88 00:06:11,960 --> 00:06:18,260 So whenever you access this account and change the value it will be changed to that particular instance 89 00:06:18,320 --> 00:06:20,080 that was created by Cartland. 90 00:06:20,120 --> 00:06:20,640 Right. 91 00:06:20,750 --> 00:06:23,910 Because we don't have any name for that object. 92 00:06:23,940 --> 00:06:27,200 We are simply using the class name here. 93 00:06:27,200 --> 00:06:30,600 That is our object plus dog count and something like that. 94 00:06:30,650 --> 00:06:35,360 Similarly now the same thing applies for this method as well. 95 00:06:36,040 --> 00:06:40,500 Call it anywhere and it will be returning Indian string. 96 00:06:40,540 --> 00:06:41,050 Right. 97 00:06:42,030 --> 00:06:44,910 And now at the end this object customers data. 98 00:06:44,950 --> 00:06:48,520 And also in properties from the superclass. 99 00:06:48,520 --> 00:06:50,140 So it us define a class here. 100 00:06:50,320 --> 00:06:52,290 Let's call it my superclass. 101 00:06:52,390 --> 00:06:57,790 So here this customer data and also inherit the properties of my superclass. 102 00:06:57,820 --> 00:07:04,150 Now since this is my superclass So we need to treat it like object by using the primary constructor 103 00:07:04,180 --> 00:07:04,520 here. 104 00:07:04,540 --> 00:07:05,010 Right. 105 00:07:05,200 --> 00:07:10,940 And now let us make this class as open so actually so as to inherit all the properties and also if I 106 00:07:10,940 --> 00:07:17,340 want to inherit this my method or override this my method inside that customer's data. 107 00:07:17,350 --> 00:07:22,200 Then also they're disposable so far that to simply use the Bengie word here. 108 00:07:23,210 --> 00:07:27,410 And then simply override this function. 109 00:07:27,410 --> 00:07:33,530 My method simply or what I did now inserted is if you weren't going to call the super dark matter this 110 00:07:33,530 --> 00:07:35,370 year and now here. 111 00:07:35,620 --> 00:07:37,650 Let us spring just like this. 112 00:07:37,760 --> 00:07:43,640 And now you can notice this object is actually behaving like a class because it is actually inheriting 113 00:07:43,670 --> 00:07:45,940 all the properties of my superclass. 114 00:07:46,040 --> 00:07:47,840 So we can call it our derived class. 115 00:07:47,870 --> 00:07:50,350 And this is now acting as a superclass. 116 00:07:50,360 --> 00:07:52,500 Now what about this my method. 117 00:07:52,520 --> 00:08:00,070 How can we access this might make it so far that again inside the main method what we do is I would 118 00:08:00,070 --> 00:08:05,240 simply use customer's data Dorte my method and simply pass. 119 00:08:05,260 --> 00:08:05,930 Let's see. 120 00:08:06,070 --> 00:08:06,540 Hello. 121 00:08:06,640 --> 00:08:12,990 So this my method is now becoming the static member of this object. 122 00:08:13,010 --> 00:08:13,340 Right. 123 00:08:13,360 --> 00:08:15,390 So we can write the command line here. 124 00:08:15,430 --> 00:08:22,030 So this method is similar to this method is now acting as a static method inside this object of customer's 125 00:08:22,090 --> 00:08:22,710 data. 126 00:08:23,560 --> 00:08:29,870 And now if you create the instance of my superclass then you have to access this method here with the 127 00:08:29,870 --> 00:08:32,900 help of the object of this superclass. 128 00:08:32,960 --> 00:08:38,160 It is only behaving stocktake after all what I did inside the object customer's data. 129 00:08:38,510 --> 00:08:44,190 So let us know then the code and let's see what it prints. 130 00:08:44,190 --> 00:08:46,020 So here we get my superclass. 131 00:08:46,020 --> 00:08:49,270 Has God rendered just because of this statement. 132 00:08:49,320 --> 00:08:52,970 Superdog muttered an object customer data. 133 00:08:52,980 --> 00:08:56,430 Hello is getting printed which is present here. 134 00:08:56,640 --> 00:08:57,400 Right. 135 00:08:57,480 --> 00:09:03,780 So in this video we learn about how do we declare the object and also how can we find the alternative 136 00:09:03,990 --> 00:09:10,180 for the static variables and methods in case of Gorlin similar to what we do in Gears of Java. 137 00:09:10,200 --> 00:09:16,230 So at the end of this video and let us summarize the properties of the object so to later object we 138 00:09:16,230 --> 00:09:21,300 use the key word of object and Cartland internally creates a class. 139 00:09:21,450 --> 00:09:25,250 And also the instance an object of that particular class. 140 00:09:25,560 --> 00:09:33,510 But that object is only one Enchong we cannot create more object or instance again and these objects 141 00:09:33,720 --> 00:09:37,300 can have properties methods and also initializers. 142 00:09:37,530 --> 00:09:44,970 And now one thing I forgot to show you that inside the customer's data object we can also use the init 143 00:09:45,360 --> 00:09:46,440 block like this. 144 00:09:46,440 --> 00:09:52,600 And you can now initialize whatever you want to inside the init block. 145 00:09:52,650 --> 00:09:54,620 Right like this. 146 00:09:54,930 --> 00:09:57,680 So we can have initializers in case of object. 147 00:09:58,020 --> 00:10:05,070 And next we can not have constrictors inside the object as it makes sense because because we do not 148 00:10:05,070 --> 00:10:12,000 have any right to do so to create an instance manually and the object can also have a superclass like 149 00:10:12,000 --> 00:10:17,010 we saw in this video that is simply supports inheritance fine. 150 00:10:17,160 --> 00:10:21,020 So this was all about to object in case of Cartland. 151 00:10:21,120 --> 00:10:22,660 So that's all for this video. 152 00:10:22,680 --> 00:10:25,420 Have a nice day and get you guys in the next video. 153 00:10:25,440 --> 00:10:27,120 This is showing hard signing off. 154 00:10:27,120 --> 00:10:27,650 Thank you.