1 00:00:00,720 --> 00:00:01,560 ‫Instructor: Hi. 2 00:00:01,560 --> 00:00:04,500 ‫Within this lecture we're going to continue deep diving 3 00:00:04,500 --> 00:00:08,700 ‫into classes because they're very important as you can see. 4 00:00:08,700 --> 00:00:11,400 ‫And we're going to learn about access levels, 5 00:00:11,400 --> 00:00:13,470 ‫accessibility levels. 6 00:00:13,470 --> 00:00:14,430 ‫Okay. 7 00:00:14,430 --> 00:00:19,430 ‫So we have these keywords over here like protected, public. 8 00:00:19,620 --> 00:00:24,000 ‫So we didn't pay attention to them very much before, 9 00:00:24,000 --> 00:00:28,050 ‫but this time we are gonna learn how they work. 10 00:00:28,050 --> 00:00:29,700 ‫So in order to do that, 11 00:00:29,700 --> 00:00:33,780 ‫I'm not gonna mess the previous things over here. 12 00:00:33,780 --> 00:00:36,360 ‫So I'm gonna create a new Java class. 13 00:00:36,360 --> 00:00:40,350 ‫And this time, let's call this something like Simpsons, 14 00:00:40,350 --> 00:00:42,780 ‫and we're gonna create some Simpson objects 15 00:00:42,780 --> 00:00:44,670 ‫out of this class. 16 00:00:44,670 --> 00:00:48,180 ‫So inside of Simpson's class, 17 00:00:48,180 --> 00:00:50,070 ‫we're gonna have some similar things 18 00:00:50,070 --> 00:00:52,320 ‫like we did in the musician's class. 19 00:00:52,320 --> 00:00:55,800 ‫Like we can have a name, we can have an age, 20 00:00:55,800 --> 00:00:58,590 ‫we can have a job, right. 21 00:00:58,590 --> 00:01:01,920 ‫So let me call this age, job, 22 00:01:01,920 --> 00:01:05,550 ‫and maybe you can try with other attributes as well, 23 00:01:05,550 --> 00:01:07,110 ‫but I'm gonna keep it simple. 24 00:01:07,110 --> 00:01:10,530 ‫I'm gonna say generate constructor, choose everything, 25 00:01:10,530 --> 00:01:12,180 ‫and hit okay. 26 00:01:12,180 --> 00:01:14,850 ‫So this will be my constructor. 27 00:01:14,850 --> 00:01:18,900 ‫As you can see, I have the same setup over here 28 00:01:18,900 --> 00:01:21,540 ‫like we did in the musicians. 29 00:01:21,540 --> 00:01:24,813 ‫Now let's create an another method called makeSimpsons. 30 00:01:26,280 --> 00:01:27,540 ‫Okay. 31 00:01:27,540 --> 00:01:30,750 ‫And inside of these methods we are going to make Simpsons. 32 00:01:30,750 --> 00:01:32,880 ‫Of course we don't have to do that. 33 00:01:32,880 --> 00:01:35,083 ‫I'm just doing this in order to be tidy, 34 00:01:35,083 --> 00:01:37,650 ‫in order to be structural. 35 00:01:37,650 --> 00:01:40,350 ‫So let me call the first one Homer 36 00:01:40,350 --> 00:01:42,000 ‫and then I say new Simpsons. 37 00:01:42,000 --> 00:01:47,000 ‫It will ask for a name, ask for an age, and ask for a job. 38 00:01:47,250 --> 00:01:50,850 ‫And as much as remember the Homer Simpson 39 00:01:50,850 --> 00:01:54,060 ‫was a nuclear safety guy. 40 00:01:54,060 --> 00:01:56,763 ‫So I'm going to say nuclear. 41 00:01:57,780 --> 00:02:02,142 ‫And over here it really doesn't have any differences 42 00:02:02,142 --> 00:02:05,100 ‫when we compare it to the musicians right now. 43 00:02:05,100 --> 00:02:08,610 ‫So if you say Homer.age, you can reach the age. 44 00:02:08,610 --> 00:02:10,230 ‫And you can even change it. 45 00:02:10,230 --> 00:02:12,570 ‫Like you can make it 51. 46 00:02:12,570 --> 00:02:17,160 ‫And let me print the age so that we can be certain 47 00:02:17,160 --> 00:02:20,580 ‫that we got to change it once we initialize it. 48 00:02:20,580 --> 00:02:21,413 ‫Okay. 49 00:02:21,413 --> 00:02:24,420 ‫Let me call, makeSimpsons over here 50 00:02:24,420 --> 00:02:27,540 ‫so that it will get executed. 51 00:02:27,540 --> 00:02:29,940 ‫So let me run the emulator, 52 00:02:29,940 --> 00:02:34,940 ‫and of course we will have to track this in our logcat. 53 00:02:35,220 --> 00:02:36,053 ‫So here you go. 54 00:02:36,053 --> 00:02:37,980 ‫We see that 51. 55 00:02:37,980 --> 00:02:40,278 ‫And it means that everything is working 56 00:02:40,278 --> 00:02:42,414 ‫as it's supposed to be. 57 00:02:42,414 --> 00:02:47,220 ‫But let me add public keyword over here. 58 00:02:47,220 --> 00:02:49,233 ‫So what does public mean? 59 00:02:50,550 --> 00:02:53,040 ‫It's actually detectable over here 60 00:02:53,040 --> 00:02:55,464 ‫because it's public by default. 61 00:02:55,464 --> 00:02:59,100 ‫Public means that it can be reached 62 00:02:59,100 --> 00:03:01,410 ‫within other classes as well. 63 00:03:01,410 --> 00:03:05,430 ‫So we are writing string name in Simpsons, 64 00:03:05,430 --> 00:03:07,590 ‫but you can actually change it 65 00:03:07,590 --> 00:03:12,590 ‫or read it from other classes like main activity as well. 66 00:03:12,660 --> 00:03:17,660 ‫But if I do something like this private, okay, 67 00:03:17,700 --> 00:03:19,440 ‫then you will- 68 00:03:19,440 --> 00:03:21,270 ‫I would get some errors 69 00:03:21,270 --> 00:03:25,200 ‫because age has private property right now 70 00:03:25,200 --> 00:03:29,970 ‫so you can initialize it like in Simpson's Homer case, 71 00:03:29,970 --> 00:03:34,770 ‫but you cannot reach it and change it afterwards. 72 00:03:34,770 --> 00:03:38,430 ‫As you can see, it doesn't even compile it. 73 00:03:38,430 --> 00:03:43,430 ‫It says that age has private access in Simpsons. 74 00:03:43,740 --> 00:03:47,310 ‫So if you make your attributes private 75 00:03:47,310 --> 00:03:49,260 ‫then you cannot change it afterwards. 76 00:03:49,260 --> 00:03:50,610 ‫You cannot even read it. 77 00:03:50,610 --> 00:03:52,590 ‫Of course, there are other methods 78 00:03:52,590 --> 00:03:54,510 ‫like getters and setters in order 79 00:03:54,510 --> 00:03:58,020 ‫to have some reading options, 80 00:03:58,020 --> 00:03:59,820 ‫but not writeable options. 81 00:03:59,820 --> 00:04:01,803 ‫We're gonna look into that later on. 82 00:04:02,850 --> 00:04:06,510 ‫So other than public and private, 83 00:04:06,510 --> 00:04:10,770 ‫there are some other key words like protected as well. 84 00:04:10,770 --> 00:04:12,930 ‫So protected means it's protected 85 00:04:12,930 --> 00:04:16,800 ‫and you can reach it from the same class, 86 00:04:16,800 --> 00:04:20,670 ‫but when you do it private, 87 00:04:20,670 --> 00:04:23,700 ‫you can actually reach it from the same class. 88 00:04:23,700 --> 00:04:25,770 ‫But if you do protect it, 89 00:04:25,770 --> 00:04:29,040 ‫you can reach it in the same Java file. 90 00:04:29,040 --> 00:04:31,239 ‫Okay, so if you have class inside of a class 91 00:04:31,239 --> 00:04:35,220 ‫or something like that, then you can reach it 92 00:04:35,220 --> 00:04:38,190 ‫once you define it as protected. 93 00:04:38,190 --> 00:04:40,020 ‫Once you define it as public, 94 00:04:40,020 --> 00:04:41,940 ‫you can reach it from any class. 95 00:04:41,940 --> 00:04:44,850 ‫Once you do it as private, 96 00:04:44,850 --> 00:04:47,580 ‫then you cannot reach it from anywhere else, 97 00:04:47,580 --> 00:04:50,370 ‫but within your same class. 98 00:04:50,370 --> 00:04:54,600 ‫So when you first deal with Android development, 99 00:04:54,600 --> 00:04:58,435 ‫you don't really actually involve with this decision at all. 100 00:04:58,435 --> 00:05:00,878 ‫So it really doesn't matter for you 101 00:05:00,878 --> 00:05:04,920 ‫if you make it protected or private or public. 102 00:05:04,920 --> 00:05:08,460 ‫But once you get into bigger projects, 103 00:05:08,460 --> 00:05:12,630 ‫once you start working, collaborating with other developers 104 00:05:12,630 --> 00:05:15,300 ‫then you should really pay attention to those. 105 00:05:15,300 --> 00:05:18,480 ‫Because if you don't want them to be public, 106 00:05:18,480 --> 00:05:20,460 ‫then don't make it public. 107 00:05:20,460 --> 00:05:23,970 ‫But if you want this to work like this, 108 00:05:23,970 --> 00:05:25,920 ‫like we have seen before, 109 00:05:25,920 --> 00:05:28,140 ‫then you should change it back to the public 110 00:05:28,140 --> 00:05:31,140 ‫and make sure you can reach Homer.age 111 00:05:31,140 --> 00:05:36,140 ‫or Homer.instrument or Homer.name within another class. 112 00:05:36,755 --> 00:05:39,750 ‫Then you should declare it as public. 113 00:05:39,750 --> 00:05:44,040 ‫So this is how you work with accessibility levels 114 00:05:44,040 --> 00:05:46,650 ‫inside of Android development. 115 00:05:46,650 --> 00:05:50,940 ‫So right now we only have one issue to cover 116 00:05:50,940 --> 00:05:53,850 ‫inside of classes subject, 117 00:05:53,850 --> 00:05:56,490 ‫and that is getters and setters. 118 00:05:56,490 --> 00:05:59,733 ‫We're gonna look into that in the next lecture.