1 00:00:00,000 --> 00:00:00,074 2 00:00:00,074 --> 00:00:01,240 SPEAKER 1: Hello, everybody. 3 00:00:01,240 --> 00:00:03,960 Welcome to the CS50 Seminar. 4 00:00:03,960 --> 00:00:07,530 Today, we'll be talking about Java SE 8 programming basics. 5 00:00:07,530 --> 00:00:09,990 Java is one of the most popular and well-known coding 6 00:00:09,990 --> 00:00:11,550 languages in the world. 7 00:00:11,550 --> 00:00:13,560 And whether you've never touched Java before, 8 00:00:13,560 --> 00:00:15,396 or you've coded extensively in it, I hope 9 00:00:15,396 --> 00:00:17,520 that you'll find this lecture helpful because we'll 10 00:00:17,520 --> 00:00:20,340 be going over a review of syntax, but also some 11 00:00:20,340 --> 00:00:22,530 of the more foundational and important concepts 12 00:00:22,530 --> 00:00:25,800 that Java has to contribute to us as programmers. 13 00:00:25,800 --> 00:00:27,510 So let's get started. 14 00:00:27,510 --> 00:00:32,430 Java, as a background, was founded by Sun Microsystems in 1995. 15 00:00:32,430 --> 00:00:35,340 That means today it is 22 years old, older than I 16 00:00:35,340 --> 00:00:37,800 am, probably older than some of you. 17 00:00:37,800 --> 00:00:40,132 It is an object-oriented programming language. 18 00:00:40,132 --> 00:00:43,090 And we'll cover a little bit more in-depth what this means in a moment. 19 00:00:43,090 --> 00:00:47,400 But it suffices to say that Java is composed of objects that you create, 20 00:00:47,400 --> 00:00:49,890 classes that you define, and everything revolves 21 00:00:49,890 --> 00:00:55,380 around this idea of us making these objects and manipulating them. 22 00:00:55,380 --> 00:00:57,407 Java runs on the Java Virtual Machine, which 23 00:00:57,407 --> 00:00:59,490 is something you have to download on your computer 24 00:00:59,490 --> 00:01:01,802 if you want to run Java code. 25 00:01:01,802 --> 00:01:04,260 Unfortunately, this does mean it's a little bit of a hassle 26 00:01:04,260 --> 00:01:05,250 to get off the ground. 27 00:01:05,250 --> 00:01:09,120 But this will have some benefits to it, as well, that we'll cover in a moment. 28 00:01:09,120 --> 00:01:11,130 Today, we'll be talking about SE 8-- 29 00:01:11,130 --> 00:01:14,560 that's Standard Edition 8 of Java-- that was released in 2014. 30 00:01:14,560 --> 00:01:18,180 Java is one of those languages that's continuously updated, whenever 31 00:01:18,180 --> 00:01:21,310 you're resolving a bug or you're adding new features to the language. 32 00:01:21,310 --> 00:01:25,200 In fact, there was actually a Java SE 9 released in September of this year. 33 00:01:25,200 --> 00:01:28,680 But it's OK, because all the ideas and the concepts that we're 34 00:01:28,680 --> 00:01:31,980 going to talk about today have not changed much at all between the two 35 00:01:31,980 --> 00:01:33,750 updates. 36 00:01:33,750 --> 00:01:35,969 So moving forward, what are the benefits of Java? 37 00:01:35,969 --> 00:01:37,260 Why should we learn about Java? 38 00:01:37,260 --> 00:01:38,820 Why is it relevant today? 39 00:01:38,820 --> 00:01:41,290 Well, first and foremost, it is object-oriented, 40 00:01:41,290 --> 00:01:44,220 which is a new way of-- or at least, when it was founded-- 41 00:01:44,220 --> 00:01:45,890 a new way of looking at coding. 42 00:01:45,890 --> 00:01:48,690 Beforehand, there was procedural coding language, 43 00:01:48,690 --> 00:01:51,066 such as C, that we learned in class, and afterwards there 44 00:01:51,066 --> 00:01:53,898 were things that came along called functional programming languages, 45 00:01:53,898 --> 00:01:54,420 like Python. 46 00:01:54,420 --> 00:01:57,920 But being in the middle of these two, object-oriented programming language 47 00:01:57,920 --> 00:02:00,960 is both complex in that you have a lot of creative room 48 00:02:00,960 --> 00:02:06,175 to manipulate your objects and to add your own creativity into your program, 49 00:02:06,175 --> 00:02:08,550 but at the same time, it has some built-in functionality, 50 00:02:08,550 --> 00:02:12,700 like in Python, how we have certain functions we can call that simplify 51 00:02:12,700 --> 00:02:14,940 our task, that's the same thing with Java, 52 00:02:14,940 --> 00:02:19,230 because there is some built-in methods, built-in classes, that help us. 53 00:02:19,230 --> 00:02:22,680 Another very important benefit of Java is the theory of encapsulation. 54 00:02:22,680 --> 00:02:25,800 Encapsulation is this idea that you create data 55 00:02:25,800 --> 00:02:29,340 and you format it in a way such that only certain people and certain users 56 00:02:29,340 --> 00:02:30,420 can access it. 57 00:02:30,420 --> 00:02:32,580 That way, if you're trading sensitive information, 58 00:02:32,580 --> 00:02:35,250 or you're building programs for important causes, 59 00:02:35,250 --> 00:02:40,440 there will not be any access to it by people who are not authorized. 60 00:02:40,440 --> 00:02:44,490 This is very important because it is the key to secure coding, 61 00:02:44,490 --> 00:02:47,160 and it is very important for programmers if we 62 00:02:47,160 --> 00:02:51,340 want to make our code have a real world impact. 63 00:02:51,340 --> 00:02:55,070 Java's also platform independent, because when you compile Java code, 64 00:02:55,070 --> 00:02:57,050 it compiles into bytecode, which you can run 65 00:02:57,050 --> 00:03:01,460 on any operating system, whether it's iOS, or if it's Windows, 66 00:03:01,460 --> 00:03:02,330 or if it's Linux. 67 00:03:02,330 --> 00:03:06,230 And that means that it reduces the hassle of converting, or basically 68 00:03:06,230 --> 00:03:08,346 porting, between these different platforms, 69 00:03:08,346 --> 00:03:10,220 because you don't have to recompile your code 70 00:03:10,220 --> 00:03:12,500 and you don't have to change your code in any way. 71 00:03:12,500 --> 00:03:17,810 Because it stays inside of its virtual machine, it will run anywhere. 72 00:03:17,810 --> 00:03:19,220 Also, Java is very robust. 73 00:03:19,220 --> 00:03:23,180 This is an upgrade that it got from C. Java has no memory leaks, because Java 74 00:03:23,180 --> 00:03:26,392 handles its memory, and it handles garbage collection on its own. 75 00:03:26,392 --> 00:03:27,600 We don't have to free memory. 76 00:03:27,600 --> 00:03:28,974 We don't have to allocate memory. 77 00:03:28,974 --> 00:03:31,970 And that reduces the hassle on the coder and makes it a lot easier 78 00:03:31,970 --> 00:03:35,870 to do what is relevant for our projects. 79 00:03:35,870 --> 00:03:37,580 Java is also simple. 80 00:03:37,580 --> 00:03:39,590 One of the other major upgrades it got from C 81 00:03:39,590 --> 00:03:41,720 is the fact that it no longer has pointers. 82 00:03:41,720 --> 00:03:44,630 You no longer have to worry about where your addresses and memory are 83 00:03:44,630 --> 00:03:47,390 because Java allocates them for you, and therefore, it 84 00:03:47,390 --> 00:03:51,300 is easier, as I said before, to focus on the relevant tasks at hand. 85 00:03:51,300 --> 00:03:53,270 And finally, Java is very secure. 86 00:03:53,270 --> 00:03:56,360 I mentioned the Java Virtual Machine a moment ago. 87 00:03:56,360 --> 00:03:58,670 The Java Virtual Machine is basically a sandbox. 88 00:03:58,670 --> 00:04:01,370 And all Java programs run only inside of this sandbox. 89 00:04:01,370 --> 00:04:04,590 And they cannot affect your computer in any other way outside of it. 90 00:04:04,590 --> 00:04:07,840 This is very important because that way, you cannot have any malicious attacks 91 00:04:07,840 --> 00:04:11,900 on your hardware or on your memory or anything like that from a Java program, 92 00:04:11,900 --> 00:04:16,230 because it can only stay inside a certain area of your computer. 93 00:04:16,230 --> 00:04:19,140 And finally, but most importantly, Java is everywhere. 94 00:04:19,140 --> 00:04:22,490 It is almost impossible for you to pursue a career in software engineering 95 00:04:22,490 --> 00:04:26,104 or programming and not come across some sort of Java files or projects. 96 00:04:26,104 --> 00:04:28,520 In addition, if you think about getting one of those jobs, 97 00:04:28,520 --> 00:04:31,429 you most likely will have to learn Java at some point in the future. 98 00:04:31,429 --> 00:04:33,470 So I hope that today's lecture will help give you 99 00:04:33,470 --> 00:04:37,220 an introduction, a refresher, or some motivation to go and explore 100 00:04:37,220 --> 00:04:38,904 this fascinating language. 101 00:04:38,904 --> 00:04:40,820 But before we dive into the specifics, we also 102 00:04:40,820 --> 00:04:44,450 have to talk a little bit about the downsides of Java. 103 00:04:44,450 --> 00:04:46,400 Yes, we have to reintroduce the semi-colon. 104 00:04:46,400 --> 00:04:48,560 I know in Python we are able to eliminate this, 105 00:04:48,560 --> 00:04:52,880 but in C, as in Java, we have semi-colons as well as more complicated 106 00:04:52,880 --> 00:04:56,490 syntax, because we are dealing with a lower level programming language 107 00:04:56,490 --> 00:04:57,980 than in Python, unfortunately. 108 00:04:57,980 --> 00:05:00,717 So for those of you who didn't exactly like C, 109 00:05:00,717 --> 00:05:03,050 or thought that it was too much hassle, potentially Java 110 00:05:03,050 --> 00:05:04,300 could also be this way to you. 111 00:05:04,300 --> 00:05:07,860 But I hope that I will be able to convince you otherwise today. 112 00:05:07,860 --> 00:05:10,679 So let's go with a roadmap of what we're going to talk about. 113 00:05:10,679 --> 00:05:13,220 As I said before, we'll start off with some basic Java syntax 114 00:05:13,220 --> 00:05:15,261 so that those of you who are new to the language, 115 00:05:15,261 --> 00:05:19,110 or who have not coded in it for a while, can come up 116 00:05:19,110 --> 00:05:20,880 to speed with the rest of us. 117 00:05:20,880 --> 00:05:24,020 And then, we'll go into good class design, because like David Malan talks 118 00:05:24,020 --> 00:05:26,300 about in CS50, there are good ways and bad ways 119 00:05:26,300 --> 00:05:28,320 to implement the same project in code. 120 00:05:28,320 --> 00:05:30,500 So how do we get the most optimal, most efficient, 121 00:05:30,500 --> 00:05:32,780 most elegant solution to our problem? 122 00:05:32,780 --> 00:05:34,449 That comes through good class design. 123 00:05:34,449 --> 00:05:37,490 And finally, we'll talk about the very important concept of encapsulation 124 00:05:37,490 --> 00:05:39,740 and how we can implement that into our programs, 125 00:05:39,740 --> 00:05:41,660 and why it's relevant in real life. 126 00:05:41,660 --> 00:05:44,810 And hopefully, coming out of this presentation, whenever 127 00:05:44,810 --> 00:05:48,680 you look at a Java program or you work with others on a Java project, 128 00:05:48,680 --> 00:05:51,150 you'll be able to relate a little bit less to this meme 129 00:05:51,150 --> 00:05:52,191 that I'll share with you. 130 00:05:52,191 --> 00:05:55,070 131 00:05:55,070 --> 00:05:59,880 All right, now, moving forward, let's start with object-oriented programming. 132 00:05:59,880 --> 00:06:04,226 So I talked about how Java relates to objects and classes that we define. 133 00:06:04,226 --> 00:06:05,350 Well, what exactly is that? 134 00:06:05,350 --> 00:06:08,880 What is the most basic structure of a Java program? 135 00:06:08,880 --> 00:06:10,440 Well, it's simply this. 136 00:06:10,440 --> 00:06:13,050 Public class Animal-- notice here that we 137 00:06:13,050 --> 00:06:17,540 have a class, which is what we have defined, 138 00:06:17,540 --> 00:06:19,340 and a name for our class, which is animal. 139 00:06:19,340 --> 00:06:23,024 By convention, the first letter of our name is capitalized. 140 00:06:23,024 --> 00:06:25,190 And notice here that we have two curly braces, which 141 00:06:25,190 --> 00:06:27,690 are what will encase our code. 142 00:06:27,690 --> 00:06:30,750 Notice that there is no code inside, but this program will still run, 143 00:06:30,750 --> 00:06:34,280 and because this is the most basic structure that you can have in Java. 144 00:06:34,280 --> 00:06:37,010 So now, let's put something inside of our program. 145 00:06:37,010 --> 00:06:39,507 Namely, we have now created an object. 146 00:06:39,507 --> 00:06:40,340 What is this object? 147 00:06:40,340 --> 00:06:41,930 It is called name. 148 00:06:41,930 --> 00:06:43,550 And what type is it? 149 00:06:43,550 --> 00:06:44,970 It is a string object. 150 00:06:44,970 --> 00:06:48,140 So just like we had strings in Python and C, we now have strings in Java, 151 00:06:48,140 --> 00:06:48,770 as well. 152 00:06:48,770 --> 00:06:51,194 Not a big surprise, but you'll see later that there 153 00:06:51,194 --> 00:06:53,360 will be different functionalities with this program. 154 00:06:53,360 --> 00:06:56,290 Also notice the semi-colon, because we'll be using that a lot. 155 00:06:56,290 --> 00:07:00,310 156 00:07:00,310 --> 00:07:03,490 OK, so in Java, there's also a distinction 157 00:07:03,490 --> 00:07:05,900 that we make between fields and methods. 158 00:07:05,900 --> 00:07:08,440 So here, we have a class. 159 00:07:08,440 --> 00:07:11,100 Class is Animal, and the difference between fields and methods 160 00:07:11,100 --> 00:07:13,300 is fields is basically all of our objects, 161 00:07:13,300 --> 00:07:15,730 and methods are all the things that our objects can do. 162 00:07:15,730 --> 00:07:21,140 So for instance, in this class called Animal, we have an object called name. 163 00:07:21,140 --> 00:07:23,800 And then we have a method over here called getName. 164 00:07:23,800 --> 00:07:27,710 And we distinguish methods by the parentheses that we put afterwards. 165 00:07:27,710 --> 00:07:32,530 And notice here in our getName we have a word called public, which is a keyword 166 00:07:32,530 --> 00:07:34,480 that we'll introduce in a moment. 167 00:07:34,480 --> 00:07:37,930 We have a return type that we've talked about in C and Python before. 168 00:07:37,930 --> 00:07:39,470 It's called string. 169 00:07:39,470 --> 00:07:42,640 We have the name of the method, and then we have the body of the method, 170 00:07:42,640 --> 00:07:44,830 too, which is return name. 171 00:07:44,830 --> 00:07:48,070 So return name tells us exactly what this method is going to do. 172 00:07:48,070 --> 00:07:52,930 In this case, it just gives us back this object that we have defined above. 173 00:07:52,930 --> 00:07:55,300 So now, let's look at a little bit more over here. 174 00:07:55,300 --> 00:08:01,820 As another example of a method, we have public void, setName, string, newName. 175 00:08:01,820 --> 00:08:04,552 So in this case, we have a different return type, namely void. 176 00:08:04,552 --> 00:08:06,260 And we might have touched on this before, 177 00:08:06,260 --> 00:08:08,920 but void just means that there is no return to the user. 178 00:08:08,920 --> 00:08:12,610 When the user calls this method, they don't expect anything back. 179 00:08:12,610 --> 00:08:15,404 And then, we also have the setName, name of the method. 180 00:08:15,404 --> 00:08:16,820 But now, we have something inside. 181 00:08:16,820 --> 00:08:18,310 This is called a parameter. 182 00:08:18,310 --> 00:08:21,250 And the parameter has a type specified-- in this case, it's a string-- 183 00:08:21,250 --> 00:08:23,380 as well as a placeholder, which is newName. 184 00:08:23,380 --> 00:08:26,290 newName is not going to be an actual variable or anything. 185 00:08:26,290 --> 00:08:29,920 But whenever we call this method, we'll provide an object 186 00:08:29,920 --> 00:08:31,480 that newName will take on. 187 00:08:31,480 --> 00:08:33,669 And newName is what is accessed inside our code. 188 00:08:33,669 --> 00:08:39,209 And we set our name variable equal to this newName. 189 00:08:39,209 --> 00:08:41,480 All right, so now, let's move up a little bit 190 00:08:41,480 --> 00:08:43,370 and talk about classes and files. 191 00:08:43,370 --> 00:08:47,210 So every single Java file starts with this header, public class 192 00:08:47,210 --> 00:08:49,310 Animal, every single one of them. 193 00:08:49,310 --> 00:08:52,850 And specifically, this word right here, this title, 194 00:08:52,850 --> 00:08:55,590 has to match the name of the Java file. 195 00:08:55,590 --> 00:08:58,550 So if we were to look into our computer, we 196 00:08:58,550 --> 00:09:03,720 would see a file called animal dot Java when we save this. 197 00:09:03,720 --> 00:09:06,900 As you can see, if we open up terminal or command prompt, 198 00:09:06,900 --> 00:09:09,840 and we want to compile and run this program, 199 00:09:09,840 --> 00:09:12,450 we have to use a command called javac. 200 00:09:12,450 --> 00:09:14,950 And then, we type in the name of our program. 201 00:09:14,950 --> 00:09:16,830 So javac is the compiler of Java. 202 00:09:16,830 --> 00:09:20,840 And notice that, unlike C, where we can have, like, c leng or make, 203 00:09:20,840 --> 00:09:23,550 and we can customize our compilers, Java has only one. 204 00:09:23,550 --> 00:09:26,160 And this also reduces the hassle that programmers 205 00:09:26,160 --> 00:09:29,860 have to go through when you're trying to construct and then execute code. 206 00:09:29,860 --> 00:09:33,120 After we have compiled Java, Animal dot Java, 207 00:09:33,120 --> 00:09:36,600 we can now run it by using this command, java Animal. 208 00:09:36,600 --> 00:09:38,880 Notice that we have taken off the Java at the end. 209 00:09:38,880 --> 00:09:39,890 Why is that so? 210 00:09:39,890 --> 00:09:42,159 It's because actually, when we compile our program, 211 00:09:42,159 --> 00:09:44,450 we have converted it into bytecode, like I said before. 212 00:09:44,450 --> 00:09:49,990 But it becomes a new file inside of our folder that ends with dot class. 213 00:09:49,990 --> 00:09:52,380 So in fact, we're not calling Animal Java anymore. 214 00:09:52,380 --> 00:09:56,040 We're calling Animal dot class when we're executing it. 215 00:09:56,040 --> 00:10:00,570 And what do you think this will print after I've executed these two program 216 00:10:00,570 --> 00:10:02,040 statements inside my computer? 217 00:10:02,040 --> 00:10:04,780 218 00:10:04,780 --> 00:10:06,920 If you thought nothing, well, you're correct, 219 00:10:06,920 --> 00:10:08,935 because this program has nothing inside of it. 220 00:10:08,935 --> 00:10:10,810 But in a while, we'll have some programs that 221 00:10:10,810 --> 00:10:13,351 actually have something that will print out on your computer, 222 00:10:13,351 --> 00:10:16,234 or that will make something happen. 223 00:10:16,234 --> 00:10:18,400 All right, so now, let's talk about the main method. 224 00:10:18,400 --> 00:10:22,060 This is something that we had in Python that was a little bit syntactically 225 00:10:22,060 --> 00:10:24,430 challenging, mainly because we had certain things we 226 00:10:24,430 --> 00:10:27,130 had to type out that we weren't exactly sure what it meant. 227 00:10:27,130 --> 00:10:29,050 But the same thing kind of happens in Java. 228 00:10:29,050 --> 00:10:31,750 And this is how we get a lot of our functionality with Java, 229 00:10:31,750 --> 00:10:33,050 is this main method. 230 00:10:33,050 --> 00:10:34,370 So let's take a look. 231 00:10:34,370 --> 00:10:37,082 We have a program here called public class Zoo. 232 00:10:37,082 --> 00:10:39,040 And inside of it, there is this very fancy line 233 00:10:39,040 --> 00:10:43,325 that says public static void main, string, bracket, args. 234 00:10:43,325 --> 00:10:45,200 Some of these words might be familiar to you. 235 00:10:45,200 --> 00:10:46,240 Some of them might be very foreign. 236 00:10:46,240 --> 00:10:47,590 But let's go over them. 237 00:10:47,590 --> 00:10:49,240 So first is a static keyword. 238 00:10:49,240 --> 00:10:51,850 Static is very important inside of Java because it basically 239 00:10:51,850 --> 00:10:55,432 means that you can call this method without creating an object. 240 00:10:55,432 --> 00:10:58,390 Usually you think, oh, if I have a method or some action that I can do, 241 00:10:58,390 --> 00:11:00,252 I need an object to do that action. 242 00:11:00,252 --> 00:11:02,710 But in this case, we want to make sure that we can run main 243 00:11:02,710 --> 00:11:06,010 without having to create instances of the class Zoo 244 00:11:06,010 --> 00:11:08,060 because, when we run this in our computer, 245 00:11:08,060 --> 00:11:10,180 are we going to be able to make a Zoo object when 246 00:11:10,180 --> 00:11:11,745 we're executing a main method? 247 00:11:11,745 --> 00:11:13,870 The answer is no, and therefore, we have to specify 248 00:11:13,870 --> 00:11:16,240 static so that it will run anyways. 249 00:11:16,240 --> 00:11:19,240 Void is obviously the return type because when you call the main method, 250 00:11:19,240 --> 00:11:21,250 you don't expect to get anything back, right? 251 00:11:21,250 --> 00:11:24,547 You only get the print methods that happen inside of the main method, 252 00:11:24,547 --> 00:11:26,380 and therefore, the return type would be void 253 00:11:26,380 --> 00:11:30,010 because you're not getting any objects or any output. 254 00:11:30,010 --> 00:11:31,744 Finally, inside, we have our parameter. 255 00:11:31,744 --> 00:11:33,160 And this parameter is very unique. 256 00:11:33,160 --> 00:11:35,430 This is an array inside of Java. 257 00:11:35,430 --> 00:11:38,200 And although we've talked about arrays before in Python and in C, 258 00:11:38,200 --> 00:11:40,450 obviously in Java, the array syntax and the array structure 259 00:11:40,450 --> 00:11:42,340 will be a little bit different than before. 260 00:11:42,340 --> 00:11:44,020 Notice that this is called args. 261 00:11:44,020 --> 00:11:45,250 And why is it called args? 262 00:11:45,250 --> 00:11:47,208 Well, it's because we're accepting command line 263 00:11:47,208 --> 00:11:49,300 arguments with this parameter. 264 00:11:49,300 --> 00:11:52,690 And we've touched on those before with C and Python. 265 00:11:52,690 --> 00:11:56,085 But let's take a look at Java's specific command line argument structure. 266 00:11:56,085 --> 00:11:59,210 So two things that you'll notice with this expanded program-- first of all, 267 00:11:59,210 --> 00:12:02,170 we have something called system dot out dot println. 268 00:12:02,170 --> 00:12:05,560 So this is kind of what printf is in C and what print is in Java. 269 00:12:05,560 --> 00:12:10,330 This is the way you type out a specification for the program 270 00:12:10,330 --> 00:12:12,154 to print something on your computer screen. 271 00:12:12,154 --> 00:12:13,570 In this case, what is it printing? 272 00:12:13,570 --> 00:12:17,830 It is printing the first element inside of this array of arguments. 273 00:12:17,830 --> 00:12:20,500 In Java, unlike in other programming languages, 274 00:12:20,500 --> 00:12:23,096 the first element of your arguments is not 275 00:12:23,096 --> 00:12:24,970 going to actually be the file you're running. 276 00:12:24,970 --> 00:12:27,460 It's going to be what comes immediately afterwards. 277 00:12:27,460 --> 00:12:32,020 So if I were to just execute, say, Java Zoo with nothing afterwards, 278 00:12:32,020 --> 00:12:34,630 no command line arguments afterwards, this first element 279 00:12:34,630 --> 00:12:36,070 would actually be null. 280 00:12:36,070 --> 00:12:38,230 There would be nothing there. 281 00:12:38,230 --> 00:12:41,200 And then, we have our second element inside 282 00:12:41,200 --> 00:12:44,260 of the string array of arguments. 283 00:12:44,260 --> 00:12:47,530 And this is also going to be the second thing 284 00:12:47,530 --> 00:12:49,710 that we put after our program name. 285 00:12:49,710 --> 00:12:53,530 So if I were to say Java Zoo, and then I say, like, city, 286 00:12:53,530 --> 00:12:56,499 well, this is a second element of the array. 287 00:12:56,499 --> 00:12:58,540 And in this case, we have not set anything there. 288 00:12:58,540 --> 00:13:00,970 So it would be null, as well. 289 00:13:00,970 --> 00:13:05,220 Well, let's take a look at us trying to implement this program in our terminal 290 00:13:05,220 --> 00:13:06,610 or in our command prompt. 291 00:13:06,610 --> 00:13:10,800 So first, obviously, we would have to compile it, javac Zoo dot Java. 292 00:13:10,800 --> 00:13:13,750 And then afterwards, we assume that it compiles without error. 293 00:13:13,750 --> 00:13:15,300 Let us try to run it. 294 00:13:15,300 --> 00:13:17,980 And in this case, we run it with the Java keyword Zoo 295 00:13:17,980 --> 00:13:21,880 And then we put in two command line arguments, Bronx Zoo specifically. 296 00:13:21,880 --> 00:13:24,480 So what will be printed out afterwards? 297 00:13:24,480 --> 00:13:28,647 Exactly what we expected it to, Bronx, new line, Zoo. 298 00:13:28,647 --> 00:13:30,480 Notice here that our command line arguments, 299 00:13:30,480 --> 00:13:33,900 like we did in Python and in C, were separated by spaces. 300 00:13:33,900 --> 00:13:38,910 So you might ask yourself, well, what if I wanted to give it a parameter, 301 00:13:38,910 --> 00:13:40,380 that is, two words? 302 00:13:40,380 --> 00:13:43,890 How do I separate those two words without using a space? 303 00:13:43,890 --> 00:13:47,100 How do I make it look like one command line argument instead of two? 304 00:13:47,100 --> 00:13:51,190 Well, the answer is you simply have to put quotation marks around. 305 00:13:51,190 --> 00:13:54,000 And in this case, we would treat San Diego as one command line 306 00:13:54,000 --> 00:13:55,770 argument instead of two. 307 00:13:55,770 --> 00:13:59,640 And therefore, when we print it out, we will get San Diego on one line 308 00:13:59,640 --> 00:14:00,780 and then Zoo on the next. 309 00:14:00,780 --> 00:14:03,115 310 00:14:03,115 --> 00:14:04,990 All right, now let's take a look at something 311 00:14:04,990 --> 00:14:07,340 that we might try to do that is not going to work. 312 00:14:07,340 --> 00:14:13,670 So if I compile Zoo, and then I try to run Zoo with one command line argument, 313 00:14:13,670 --> 00:14:16,090 what do you think will happen? 314 00:14:16,090 --> 00:14:17,480 Probably an error, right? 315 00:14:17,480 --> 00:14:19,400 And that is actually exactly what happens. 316 00:14:19,400 --> 00:14:23,450 You'll get this exception called ArrayIndexOutOfBounds. 317 00:14:23,450 --> 00:14:24,770 And why is that the case? 318 00:14:24,770 --> 00:14:28,660 It's because Java recognizes that we created a code, 319 00:14:28,660 --> 00:14:31,450 we created a program, that has a reference 320 00:14:31,450 --> 00:14:33,760 to the second element of an array. 321 00:14:33,760 --> 00:14:36,800 But in this case, we have only added one element to that array. 322 00:14:36,800 --> 00:14:40,190 And if we're trying to access an index of the array that does not exist, 323 00:14:40,190 --> 00:14:42,670 it will create this exception and throw it back at us. 324 00:14:42,670 --> 00:14:45,970 Exceptions are a fancy word that basically means an error. 325 00:14:45,970 --> 00:14:49,087 But in Java, errors and exceptions are a little bit different. 326 00:14:49,087 --> 00:14:50,920 And this is something that you'll definitely 327 00:14:50,920 --> 00:14:56,150 learn about as you move on in Java, but we will not be covering today in class. 328 00:14:56,150 --> 00:14:58,430 All right, so moving forward, let's make a distinction 329 00:14:58,430 --> 00:15:00,052 between primitives and objects. 330 00:15:00,052 --> 00:15:01,760 So before, we already talked about things 331 00:15:01,760 --> 00:15:05,270 like a name, which is a string object. 332 00:15:05,270 --> 00:15:06,900 String is a class inside of Java. 333 00:15:06,900 --> 00:15:10,280 It's granted a class that Java has already created for us, 334 00:15:10,280 --> 00:15:12,440 and therefore, it is a special class. 335 00:15:12,440 --> 00:15:15,390 But nonetheless, it is an object instead of a primitive. 336 00:15:15,390 --> 00:15:18,110 And notice here that when we are creating this object, 337 00:15:18,110 --> 00:15:21,680 we are telling it exactly what goes inside of it, namely, 338 00:15:21,680 --> 00:15:24,500 in this case, my name. 339 00:15:24,500 --> 00:15:28,362 In this case, there is also what is called a new parameter, a new keyword, 340 00:15:28,362 --> 00:15:30,320 which is what you will use when you're creating 341 00:15:30,320 --> 00:15:32,300 an object for the first time, when you're referencing it 342 00:15:32,300 --> 00:15:33,290 for the first time. 343 00:15:33,290 --> 00:15:38,325 Afterwards, you will have to repeat the variable type of the object, 344 00:15:38,325 --> 00:15:39,200 in this case, string. 345 00:15:39,200 --> 00:15:42,320 It should match what is on the left hand side of the equal sign. 346 00:15:42,320 --> 00:15:45,390 And then, you have what is inside, which is my name. 347 00:15:45,390 --> 00:15:47,280 Now let's take a look at a primitive. 348 00:15:47,280 --> 00:15:52,220 So in this case, integer is specified by Java as a primitive class, 349 00:15:52,220 --> 00:15:56,000 meaning that Java sees that people use this class so often that it should 350 00:15:56,000 --> 00:15:59,360 build in some functionalities and make it easier for people to create 351 00:15:59,360 --> 00:16:02,750 and manipulate this variable type, so that they don't 352 00:16:02,750 --> 00:16:04,290 have to go through as much hassle. 353 00:16:04,290 --> 00:16:09,320 So notice here that with this, we have int as our specifier at the beginning, 354 00:16:09,320 --> 00:16:13,010 but then, we do not have any of this fancy new-- 355 00:16:13,010 --> 00:16:15,890 or we don't have to type out int again. 356 00:16:15,890 --> 00:16:19,280 We just simply specify the name, and then specify the value. 357 00:16:19,280 --> 00:16:21,560 And that cuts down on a lot of the hassle 358 00:16:21,560 --> 00:16:26,220 that programmers have to go through when they're making these objects. 359 00:16:26,220 --> 00:16:30,500 So here is just, very briefly, a rundown of all the primitives 360 00:16:30,500 --> 00:16:31,790 that Java supports. 361 00:16:31,790 --> 00:16:35,060 We have boolean, which we've seen before, obviously, true or false. 362 00:16:35,060 --> 00:16:39,690 We have, then, four of these variables, byte, short, int, long, 363 00:16:39,690 --> 00:16:41,690 that serve kind of the same purpose as what we 364 00:16:41,690 --> 00:16:44,482 saw in C with different sized integers. 365 00:16:44,482 --> 00:16:47,690 They basically store integers, but they have different spaces allocated them, 366 00:16:47,690 --> 00:16:51,560 8 bits, 16 bits, 32 bits, and 64 bits. 367 00:16:51,560 --> 00:16:55,970 Afterwards, we have two more primitive types, float and double, 368 00:16:55,970 --> 00:16:57,590 which both store decimals. 369 00:16:57,590 --> 00:17:02,120 And again, double has twice the space that float has to store decimals. 370 00:17:02,120 --> 00:17:05,240 And finally, we have char, which is a 16-bit unicode value 371 00:17:05,240 --> 00:17:09,589 to store letters of the alphabet, apostrophes, punctuation, 372 00:17:09,589 --> 00:17:12,589 et cetera, et cetera. 373 00:17:12,589 --> 00:17:15,380 All right, so going forward, we're going to focus a little bit more 374 00:17:15,380 --> 00:17:16,880 on constructors. 375 00:17:16,880 --> 00:17:18,950 Just to give you a brief rundown of these, 376 00:17:18,950 --> 00:17:21,920 we have a program here to look at. 377 00:17:21,920 --> 00:17:23,380 This is public class Chick. 378 00:17:23,380 --> 00:17:24,980 These are baby chickens. 379 00:17:24,980 --> 00:17:28,410 And obviously, we want to create our own chick object. 380 00:17:28,410 --> 00:17:30,180 So how do we go about doing that? 381 00:17:30,180 --> 00:17:33,650 Well, first thing to notice is that we, up here, have 382 00:17:33,650 --> 00:17:36,097 created an object with the name, name. 383 00:17:36,097 --> 00:17:37,430 And what do we put inside of it? 384 00:17:37,430 --> 00:17:38,660 We put "Fluffy." 385 00:17:38,660 --> 00:17:41,360 Notice here that this is a little different from the argument 386 00:17:41,360 --> 00:17:46,705 that we saw on the previous slide, which had this new string inside of the line. 387 00:17:46,705 --> 00:17:48,830 Why don't we have a new string inside of this line? 388 00:17:48,830 --> 00:17:52,390 Well, that's because Java recognizes that string as a special class. 389 00:17:52,390 --> 00:17:54,140 People are going to be using string a lot. 390 00:17:54,140 --> 00:17:56,452 And even though Java doesn't make it a primitive type, 391 00:17:56,452 --> 00:17:58,160 it does simplify it so that we don't have 392 00:17:58,160 --> 00:17:59,993 to put the new operator every single time we 393 00:17:59,993 --> 00:18:02,690 create an instance of this object. 394 00:18:02,690 --> 00:18:06,000 That's just to cut down on the hassle that programmers have to go through. 395 00:18:06,000 --> 00:18:07,572 Next, notice this block of code. 396 00:18:07,572 --> 00:18:09,530 This block of code is very important because it 397 00:18:09,530 --> 00:18:11,240 is what is called a constructor. 398 00:18:11,240 --> 00:18:13,820 When we create this class and we're typing all this out, 399 00:18:13,820 --> 00:18:17,120 we need to have a constructor in order to create instances of this class, 400 00:18:17,120 --> 00:18:18,770 to create chick objects, right? 401 00:18:18,770 --> 00:18:22,786 Because this fundamentally is like a blueprint for a chick. 402 00:18:22,786 --> 00:18:25,910 But in order to create the actual chick itself, we need this block of code. 403 00:18:25,910 --> 00:18:29,034 It starts out with the word public, which is, again, a specifier that we'll 404 00:18:29,034 --> 00:18:30,450 talk about in a moment. 405 00:18:30,450 --> 00:18:33,400 And then it has a name, Chick, and then parentheses. 406 00:18:33,400 --> 00:18:35,900 So this looks similar to the format that we had for methods, 407 00:18:35,900 --> 00:18:37,760 except in this case, the name of the method 408 00:18:37,760 --> 00:18:39,770 is the same as the name of the class. 409 00:18:39,770 --> 00:18:41,780 And this is very important because we have 410 00:18:41,780 --> 00:18:45,230 to have this specified here in order for Java to know that it is a constructor, 411 00:18:45,230 --> 00:18:46,760 instead of a regular method. 412 00:18:46,760 --> 00:18:50,480 The names have to match, and they have to be identical, case and everything. 413 00:18:50,480 --> 00:18:54,410 Inside notice that we can run arguments that change variables 414 00:18:54,410 --> 00:18:55,530 or create new variables. 415 00:18:55,530 --> 00:18:57,821 In this case, what we're doing is we're taking the name 416 00:18:57,821 --> 00:19:02,300 object that we created before and giving it a new value, in this case, the word 417 00:19:02,300 --> 00:19:03,530 "tiny." 418 00:19:03,530 --> 00:19:07,740 And then, below, when we use our main method that we talked about earlier, 419 00:19:07,740 --> 00:19:10,400 we can create an object that is a chick object. 420 00:19:10,400 --> 00:19:12,530 And in this case, we have to use a new parameter. 421 00:19:12,530 --> 00:19:14,700 We have to say, Chick, over here. 422 00:19:14,700 --> 00:19:18,342 And notice that this way, we have a new object successfully created. 423 00:19:18,342 --> 00:19:20,300 And then, if we go on to the very bottom and we 424 00:19:20,300 --> 00:19:23,630 try to print out the chick's name, what do you think we would get? 425 00:19:23,630 --> 00:19:26,510 426 00:19:26,510 --> 00:19:30,584 If we were to go over here and compile Chick, and then run Chick, 427 00:19:30,584 --> 00:19:32,000 what do you think will be printed? 428 00:19:32,000 --> 00:19:35,540 429 00:19:35,540 --> 00:19:37,760 If you said "tiny," you are correct. 430 00:19:37,760 --> 00:19:40,730 And notice that this is because, even though we started out 431 00:19:40,730 --> 00:19:43,670 with "Fluffy" at the top for the value of our parameter, 432 00:19:43,670 --> 00:19:47,600 inside of our constructor, we changed its value to "tiny." 433 00:19:47,600 --> 00:19:50,810 And therefore, when we create every object of the Chick class, 434 00:19:50,810 --> 00:19:56,140 it will always have "tiny" stored inside of it instead of "Fluffy." 435 00:19:56,140 --> 00:20:00,115 All right, so going forward, let's look at a slightly more complicated program. 436 00:20:00,115 --> 00:20:01,990 The first thing you'll notice is that we have 437 00:20:01,990 --> 00:20:05,370 a lot of the same features, Fluffy, tiny, constructor. 438 00:20:05,370 --> 00:20:07,840 But there is another constructor here. 439 00:20:07,840 --> 00:20:11,170 Why do we have two of these, and why are they called the same thing? 440 00:20:11,170 --> 00:20:15,430 This is a very important concept in Java called overloading. 441 00:20:15,430 --> 00:20:19,780 Overloading in Java is where you have constructors or methods that 442 00:20:19,780 --> 00:20:21,264 have the same name. 443 00:20:21,264 --> 00:20:23,680 They could potentially have the same return type, as well. 444 00:20:23,680 --> 00:20:26,650 But the difference is in what parameters they accept. 445 00:20:26,650 --> 00:20:28,874 The first constructor accepts no parameters. 446 00:20:28,874 --> 00:20:30,790 But the second constructor accepts a parameter 447 00:20:30,790 --> 00:20:33,470 called name, which is a string object. 448 00:20:33,470 --> 00:20:36,440 And what specifically does it do with this string object? 449 00:20:36,440 --> 00:20:38,620 Well, it calls itself-- 450 00:20:38,620 --> 00:20:41,920 this dot name is referencing the name that is inside of the file, 451 00:20:41,920 --> 00:20:45,200 and it's setting its value equal to the name that the user inputs. 452 00:20:45,200 --> 00:20:48,790 Notice here that because we have two objects with the title name, 453 00:20:48,790 --> 00:20:52,130 we have to distinguish between them by using the this operator. 454 00:20:52,130 --> 00:20:54,510 The this operator is optional, obviously, 455 00:20:54,510 --> 00:20:57,580 when we're just talking about one variable of that name. 456 00:20:57,580 --> 00:21:00,160 But when we have two, it's useful to have this 457 00:21:00,160 --> 00:21:03,520 here so we know which one is which and we don't get confused. 458 00:21:03,520 --> 00:21:08,410 And notice here that because we have this additional constructor, when 459 00:21:08,410 --> 00:21:12,610 we create the object below, we can choose to specify a name inside of it. 460 00:21:12,610 --> 00:21:16,270 And with the specified name, when we print out the chick name, 461 00:21:16,270 --> 00:21:19,550 we will get something of our choosing. 462 00:21:19,550 --> 00:21:23,680 So what will this print, in this case, given that before, we 463 00:21:23,680 --> 00:21:30,990 had the chick "Cutie" as our constructor instead of just a blank constructor? 464 00:21:30,990 --> 00:21:31,800 That is correct. 465 00:21:31,800 --> 00:21:34,136 We will print "Cutie" out instead of "Fluffy" 466 00:21:34,136 --> 00:21:36,010 and instead of "tiny," because, in this case, 467 00:21:36,010 --> 00:21:38,134 we've accessed a different constructor than before. 468 00:21:38,134 --> 00:21:41,130 469 00:21:41,130 --> 00:21:45,410 All right, now let's go ahead and talk about declarations and initializations. 470 00:21:45,410 --> 00:21:49,640 And here's where I want to make a specific reference to all the objects 471 00:21:49,640 --> 00:21:53,060 and all the changes in values that we've made in this program, 472 00:21:53,060 --> 00:21:54,720 highlighted in yellow. 473 00:21:54,720 --> 00:21:58,580 So at the very top, we have our first declaration of this object. 474 00:21:58,580 --> 00:22:01,010 The distinction between declaration and initialization 475 00:22:01,010 --> 00:22:05,060 is the declaration is the left hand side of this equal sign. 476 00:22:05,060 --> 00:22:07,970 That is where we just say that this object exists. 477 00:22:07,970 --> 00:22:09,740 That is our declaration of the object. 478 00:22:09,740 --> 00:22:11,930 But do we know what is inside of it yet? 479 00:22:11,930 --> 00:22:13,310 Not exactly. 480 00:22:13,310 --> 00:22:15,860 After the equal sign is where we specify the data 481 00:22:15,860 --> 00:22:17,280 that goes inside of this object. 482 00:22:17,280 --> 00:22:19,410 And that is when we have initialized it. 483 00:22:19,410 --> 00:22:22,940 Is a very important distinction because you can create the object 484 00:22:22,940 --> 00:22:24,710 without putting anything inside. 485 00:22:24,710 --> 00:22:27,200 Therefore, you are just declaring but not instantiating it. 486 00:22:27,200 --> 00:22:30,740 Afterwards, when we run name equals tiny, 487 00:22:30,740 --> 00:22:33,090 we're not declaring or initializing any objects, 488 00:22:33,090 --> 00:22:35,240 but we're just rather changing the value inside. 489 00:22:35,240 --> 00:22:37,840 And if we look below at our final example in this program, 490 00:22:37,840 --> 00:22:39,590 when we create our new chick object, we're 491 00:22:39,590 --> 00:22:42,260 again declaring that a chick exists, and then we're 492 00:22:42,260 --> 00:22:45,866 telling it what exactly it is that this chick object has inside. 493 00:22:45,866 --> 00:22:48,740 So you have to make sure you understand the distinction between this, 494 00:22:48,740 --> 00:22:51,800 because sometimes you will see only the left side 495 00:22:51,800 --> 00:22:53,259 of the equal sign in your programs. 496 00:22:53,259 --> 00:22:56,091 And you have to make sure you understand that the program will still 497 00:22:56,091 --> 00:22:59,060 run and still work, because you are declaring the object, 498 00:22:59,060 --> 00:23:00,130 not yet initializing it. 499 00:23:00,130 --> 00:23:02,910 500 00:23:02,910 --> 00:23:06,907 All right, now, I will go through very quickly some Java statements. 501 00:23:06,907 --> 00:23:08,740 These statements are just logical statements 502 00:23:08,740 --> 00:23:10,325 that we have also in C and in Python. 503 00:23:10,325 --> 00:23:13,170 504 00:23:13,170 --> 00:23:15,459 Namely, we have the if-then statement-- 505 00:23:15,459 --> 00:23:17,250 we've definitely talked about this before-- 506 00:23:17,250 --> 00:23:21,150 if-then-else statements, also that we've referenced before, while statements, 507 00:23:21,150 --> 00:23:24,150 do-while statements, and for statements. 508 00:23:24,150 --> 00:23:29,220 For the sake of time, and so we can go into more complex and cooler 509 00:23:29,220 --> 00:23:32,520 concepts in Java, I'm going to skip over these because they follow basically 510 00:23:32,520 --> 00:23:35,130 the same logic as in other languages. 511 00:23:35,130 --> 00:23:36,930 The only difference might be the syntax. 512 00:23:36,930 --> 00:23:39,550 And so I'll cover that very quickly here with two examples. 513 00:23:39,550 --> 00:23:44,620 Notice that in our if statement, we have, once again, parentheses. 514 00:23:44,620 --> 00:23:45,880 Sorry. 515 00:23:45,880 --> 00:23:48,505 In Python, you might have gotten used to not using parentheses, 516 00:23:48,505 --> 00:23:50,150 but we're back to using parentheses. 517 00:23:50,150 --> 00:23:53,140 Notice that we have our logical operator, equals equals. 518 00:23:53,140 --> 00:23:56,560 And notice that NULL, when we specify it, NULL is a keyword 519 00:23:56,560 --> 00:23:58,720 and it's in all caps. 520 00:23:58,720 --> 00:24:00,575 Down below, we have a for statement. 521 00:24:00,575 --> 00:24:05,440 And notice that this looks very similar to what we have in C. We have the three 522 00:24:05,440 --> 00:24:09,520 parts of our parentheses separated by semi-colons, starts the int at 0, 523 00:24:09,520 --> 00:24:12,400 increases it each time using the plus plus operator, 524 00:24:12,400 --> 00:24:16,330 and also uses the less-than operator to specify when this for loop will end. 525 00:24:16,330 --> 00:24:21,090 And every time, it just prints out i, the integer. 526 00:24:21,090 --> 00:24:21,640 Cool. 527 00:24:21,640 --> 00:24:26,290 So now we're ready to get into the fun part of the lecture, which 528 00:24:26,290 --> 00:24:27,830 talks about class design. 529 00:24:27,830 --> 00:24:30,370 Like I mentioned before, there are good and bad ways 530 00:24:30,370 --> 00:24:33,517 to implement the same project to code the same thing in Java. 531 00:24:33,517 --> 00:24:35,350 But how do we make sure that we're coding it 532 00:24:35,350 --> 00:24:38,150 in the most elegant, most efficient way possible? 533 00:24:38,150 --> 00:24:42,160 So let's take a look at two files in Java. 534 00:24:42,160 --> 00:24:44,170 The first one is public class Animal. 535 00:24:44,170 --> 00:24:46,150 This is an animal that we're creating. 536 00:24:46,150 --> 00:24:48,670 Inside of it there is an age, as well as a method for us 537 00:24:48,670 --> 00:24:51,214 to get the age of this animal. 538 00:24:51,214 --> 00:24:52,880 On the right hand side, we have a tiger. 539 00:24:52,880 --> 00:24:53,838 We're creating a tiger. 540 00:24:53,838 --> 00:24:56,230 And it also has an age, obviously, because tigers 541 00:24:56,230 --> 00:24:58,240 are animals and tigers have ages. 542 00:24:58,240 --> 00:25:01,760 And there is a way to get the age, too, using our age method. 543 00:25:01,760 --> 00:25:04,240 Beneath, we also have another method called roar, 544 00:25:04,240 --> 00:25:05,780 because this tiger can roar. 545 00:25:05,780 --> 00:25:07,790 Not all animals can roar, but a tiger can. 546 00:25:07,790 --> 00:25:09,790 But do you notice something about this code that 547 00:25:09,790 --> 00:25:13,660 makes it not exactly the most efficient, not actually the cleanest solution? 548 00:25:13,660 --> 00:25:16,250 549 00:25:16,250 --> 00:25:17,360 Yes, exactly. 550 00:25:17,360 --> 00:25:21,350 You have two repeated redundant sections up here. 551 00:25:21,350 --> 00:25:23,010 These say the same exact thing. 552 00:25:23,010 --> 00:25:25,910 So if you're a programmer looking to cut down 553 00:25:25,910 --> 00:25:28,070 on the number of lines of code you're using, 554 00:25:28,070 --> 00:25:30,410 or just to make your solution cleaner, how 555 00:25:30,410 --> 00:25:34,760 would you think is a good way to combat this, combat this repetitiveness, 556 00:25:34,760 --> 00:25:38,325 and to make this more compressed, more precise, more concise? 557 00:25:38,325 --> 00:25:40,890 558 00:25:40,890 --> 00:25:45,490 This is how we came about with the idea of class inheritance. 559 00:25:45,490 --> 00:25:48,480 And this is a concept that's been around in Java for a long time. 560 00:25:48,480 --> 00:25:52,320 It basically says that if you have a certain class, 561 00:25:52,320 --> 00:25:55,500 and you create child classes of it, we should 562 00:25:55,500 --> 00:25:59,820 let the child classes share all the features inside of the parent class, 563 00:25:59,820 --> 00:26:00,600 so to speak. 564 00:26:00,600 --> 00:26:02,790 That way, we can cut down on the length of our code 565 00:26:02,790 --> 00:26:05,123 and also cut down on how many of the repetitive features 566 00:26:05,123 --> 00:26:06,150 we have to implement. 567 00:26:06,150 --> 00:26:09,600 Inside of Java, we have only what's called single inheritance, 568 00:26:09,600 --> 00:26:13,699 meaning that each child class can only have one parent class. 569 00:26:13,699 --> 00:26:15,990 The reason why it can't have more than one parent class 570 00:26:15,990 --> 00:26:18,817 is because of the complexities that arise when you have, like, 571 00:26:18,817 --> 00:26:21,150 a certain value inside of one parent class that does not 572 00:26:21,150 --> 00:26:23,580 match the value inside a different one. 573 00:26:23,580 --> 00:26:26,610 And if you get the child class to inherit both of them, which one should 574 00:26:26,610 --> 00:26:27,219 it use? 575 00:26:27,219 --> 00:26:29,010 That creates an issue, and that's something 576 00:26:29,010 --> 00:26:31,470 that Java doesn't want to be a part of. 577 00:26:31,470 --> 00:26:34,070 And therefore, it says you can only have one parent. 578 00:26:34,070 --> 00:26:37,310 So now, let's take a look logically at what this would entail. 579 00:26:37,310 --> 00:26:39,510 I know, animals, beautiful. 580 00:26:39,510 --> 00:26:41,640 Animal is our parent class. 581 00:26:41,640 --> 00:26:45,460 It is the one that is not referencing anything higher than it. 582 00:26:45,460 --> 00:26:49,530 But notice that all these other classes are children of it. 583 00:26:49,530 --> 00:26:53,021 Mammals are children of Animal, Bird are children of Animals. 584 00:26:53,021 --> 00:26:55,770 And then below it, we also have bats that the children of mammals, 585 00:26:55,770 --> 00:26:57,960 and tigers that are children and mammals, et cetera, 586 00:26:57,960 --> 00:26:58,720 et cetera, et cetera. 587 00:26:58,720 --> 00:27:00,450 And this is kind of the logical structure 588 00:27:00,450 --> 00:27:02,340 that you want to have in your mind as you 589 00:27:02,340 --> 00:27:04,530 are designing these classes because that way, 590 00:27:04,530 --> 00:27:08,250 if Animal has something like a name, we can just specify it 591 00:27:08,250 --> 00:27:10,680 and then say that the children classes are inheriting 592 00:27:10,680 --> 00:27:15,890 from it so that we don't have to specify a name inside of all of our children. 593 00:27:15,890 --> 00:27:19,140 So let's take a look again at our two classes. 594 00:27:19,140 --> 00:27:21,790 But now, notice that we have done something different. 595 00:27:21,790 --> 00:27:24,160 Over here on the right, we have Tiger, and then we 596 00:27:24,160 --> 00:27:27,730 have a very important keyword, extends Animal. 597 00:27:27,730 --> 00:27:30,570 This is how we specify that Tiger is a child class, 598 00:27:30,570 --> 00:27:32,410 and Animal is the parent class of child. 599 00:27:32,410 --> 00:27:34,810 We use the extends keyword, and then we specify 600 00:27:34,810 --> 00:27:37,210 the name of that class afterwards. 601 00:27:37,210 --> 00:27:39,370 And because we are extending it that means 602 00:27:39,370 --> 00:27:43,720 that we have all of the information inside of it at our disposal, namely, 603 00:27:43,720 --> 00:27:47,800 the age and the getAge method that are inside of Animal before. 604 00:27:47,800 --> 00:27:51,850 We are now able to use inside of Tiger without typing it a second time. 605 00:27:51,850 --> 00:27:54,130 And this definitely cuts down on all of the hassle 606 00:27:54,130 --> 00:27:57,040 that a programmer has to go through when they're coding Tiger, 607 00:27:57,040 --> 00:28:00,610 or when they're coding other potential classes like Bird, 608 00:28:00,610 --> 00:28:02,392 or Crocodile, or something like that. 609 00:28:02,392 --> 00:28:05,350 They don't have that keep specifying age and typing the same code over, 610 00:28:05,350 --> 00:28:06,250 and over, and over. 611 00:28:06,250 --> 00:28:08,583 They can just say it inherits it, and therefore, they're 612 00:28:08,583 --> 00:28:11,270 done with this part of the code. 613 00:28:11,270 --> 00:28:16,475 Now, I have gone ahead and added a second method to the Tiger class. 614 00:28:16,475 --> 00:28:19,600 As a child class, obviously, we want to do something with this information. 615 00:28:19,600 --> 00:28:21,260 Are we are able to reference it? 616 00:28:21,260 --> 00:28:22,610 The answer is yes. 617 00:28:22,610 --> 00:28:26,530 Notice here that I have created an age method that uses the getAge method. 618 00:28:26,530 --> 00:28:28,850 The getAge method is not inside of the Tiger class, 619 00:28:28,850 --> 00:28:30,800 but rather inside the Animal class. 620 00:28:30,800 --> 00:28:34,070 But this is still valid because Tiger is a child of Animal. 621 00:28:34,070 --> 00:28:36,980 And then afterwards, we're able to print out the age of the tiger 622 00:28:36,980 --> 00:28:38,960 by simply referencing it from above. 623 00:28:38,960 --> 00:28:41,990 624 00:28:41,990 --> 00:28:49,600 So now, let's take a look at a more complicated pair of classes. 625 00:28:49,600 --> 00:28:52,690 Notice here on the left, we have a class called Animal. 626 00:28:52,690 --> 00:28:54,790 Animal, as we have before, has an age. 627 00:28:54,790 --> 00:28:57,280 But now, we have also given it a name. 628 00:28:57,280 --> 00:28:59,760 So notice that we have two constructors again. 629 00:28:59,760 --> 00:29:02,260 We're overloading our constructors because the first one has 630 00:29:02,260 --> 00:29:05,380 two parameters, an age and a name, and our second one only 631 00:29:05,380 --> 00:29:07,930 has one parameter, just an age. 632 00:29:07,930 --> 00:29:09,820 But notice that we're now going to highlight 633 00:29:09,820 --> 00:29:12,310 a very important key word, super. 634 00:29:12,310 --> 00:29:13,450 What does super mean? 635 00:29:13,450 --> 00:29:17,912 Super is a keyword introducing Java to reference a parent class. 636 00:29:17,912 --> 00:29:19,870 So if you're inside of your child class and you 637 00:29:19,870 --> 00:29:22,244 want to reference something that isn't your parent class, 638 00:29:22,244 --> 00:29:24,870 you can use a super operator in order to do so. 639 00:29:24,870 --> 00:29:28,450 In this case, the super parentheses references 640 00:29:28,450 --> 00:29:30,007 the constructor of the parent class. 641 00:29:30,007 --> 00:29:31,840 This is very useful because that way, we can 642 00:29:31,840 --> 00:29:35,110 cut down on the implementation inside of our constructors, as well. 643 00:29:35,110 --> 00:29:36,985 We can make it easier by just saying, hey, 644 00:29:36,985 --> 00:29:38,860 our parent class did this in its constructor. 645 00:29:38,860 --> 00:29:41,910 Let's just do that again here, and specify it with the super keyword 646 00:29:41,910 --> 00:29:43,600 so we don't have to do it again. 647 00:29:43,600 --> 00:29:46,054 Now, the question may arise in your minds, 648 00:29:46,054 --> 00:29:47,470 what's the parent class of Animal? 649 00:29:47,470 --> 00:29:50,080 We haven't specified anything there. 650 00:29:50,080 --> 00:29:52,360 Well Java has hidden this fact from you. 651 00:29:52,360 --> 00:29:54,910 And it is because usually it's not very useful at all. 652 00:29:54,910 --> 00:29:58,330 But the reality is, in Java, every single class 653 00:29:58,330 --> 00:30:01,720 that we create is inherently a child class of a class 654 00:30:01,720 --> 00:30:04,570 that Java has created beforehand called object. 655 00:30:04,570 --> 00:30:08,160 Object dot Java is built inside of Java, and it 656 00:30:08,160 --> 00:30:11,245 acts as the parent class for everything else inside of Java. 657 00:30:11,245 --> 00:30:14,530 It is also very important because it builds all the functionality of, like, 658 00:30:14,530 --> 00:30:17,650 string classes and primitives and stuff like that so we 659 00:30:17,650 --> 00:30:18,950 don't have to worry about it. 660 00:30:18,950 --> 00:30:21,310 But this means that we can also reference it 661 00:30:21,310 --> 00:30:22,900 by using the super keyword. 662 00:30:22,900 --> 00:30:25,300 So what this means is, inside of this Animal constructor, 663 00:30:25,300 --> 00:30:27,730 we are referencing an object constructor. 664 00:30:27,730 --> 00:30:30,520 Well, in this case, object constructors don't really do anything. 665 00:30:30,520 --> 00:30:32,560 So this line of code is pretty much useless. 666 00:30:32,560 --> 00:30:34,420 But it's good for demonstration purposes. 667 00:30:34,420 --> 00:30:37,810 And afterwards, we are creating and manipulating 668 00:30:37,810 --> 00:30:42,440 the variables inside of this constructor using age, name, et cetera, et cetera. 669 00:30:42,440 --> 00:30:46,480 Notice how this super operator is also optional 670 00:30:46,480 --> 00:30:48,910 whenever we're talking about the Animal class, 671 00:30:48,910 --> 00:30:50,980 because we do not include it here. 672 00:30:50,980 --> 00:30:53,650 Like I said before, this was pretty much useless and therefore, 673 00:30:53,650 --> 00:30:55,947 why would you have to put it in your code? 674 00:30:55,947 --> 00:30:58,780 But now, we're going to talk about something more interesting, which 675 00:30:58,780 --> 00:31:02,110 is over there in the Gorilla subclass. 676 00:31:02,110 --> 00:31:05,240 The Gorilla subclass extends the Animal class. 677 00:31:05,240 --> 00:31:09,200 And in this case, what happens is we have two constructors inside 678 00:31:09,200 --> 00:31:10,191 of the Gorilla class. 679 00:31:10,191 --> 00:31:12,940 Notice that we're overloading them once again, because one of them 680 00:31:12,940 --> 00:31:14,950 has one parameter, the other has none. 681 00:31:14,950 --> 00:31:19,360 But notice that, in both of them now, we are calling the super keyword in order 682 00:31:19,360 --> 00:31:23,350 to reference all the features inside of our Animal constructors. 683 00:31:23,350 --> 00:31:27,340 So over here in our first Gorilla constructor, we input an age. 684 00:31:27,340 --> 00:31:28,870 What does a Gorilla constructor do? 685 00:31:28,870 --> 00:31:31,090 It goes in and says, hey, Animal class, can I 686 00:31:31,090 --> 00:31:34,140 borrow your constructor that takes two inputs? 687 00:31:34,140 --> 00:31:37,390 And then Animal says, all right, this is my constructor that takes two inputs. 688 00:31:37,390 --> 00:31:39,660 And Gorilla says, all right, put in an age for me, 689 00:31:39,660 --> 00:31:41,320 and put in the word gorilla. 690 00:31:41,320 --> 00:31:44,090 And what is going to come back is these actions. 691 00:31:44,090 --> 00:31:47,680 So by going over to Animal and referencing its constructor, 692 00:31:47,680 --> 00:31:51,070 we now take the age and store it inside of the age variable, We take Gorilla, 693 00:31:51,070 --> 00:31:54,670 and we store it inside the name variable, and voila, it's done. 694 00:31:54,670 --> 00:31:57,780 We could have specified this in a ton of lines of code by repeating it. 695 00:31:57,780 --> 00:31:59,680 But instead, we just used the super keyword 696 00:31:59,680 --> 00:32:01,690 to get rid of all the redundancies. 697 00:32:01,690 --> 00:32:04,090 And notice below that we also have a second reference 698 00:32:04,090 --> 00:32:05,950 of super in our second constructor. 699 00:32:05,950 --> 00:32:08,350 In this case, we just say, hey, Animal, can you 700 00:32:08,350 --> 00:32:11,080 give me a constructor that has only one parameter? 701 00:32:11,080 --> 00:32:12,250 And Animal says, sure. 702 00:32:12,250 --> 00:32:13,240 Give me an integer. 703 00:32:13,240 --> 00:32:15,430 And then Gorilla gives it the integer 5. 704 00:32:15,430 --> 00:32:16,880 It stores it inside of the age. 705 00:32:16,880 --> 00:32:19,090 It gives a name as NULL, and it returns it. 706 00:32:19,090 --> 00:32:22,440 And therefore, you're done constructing your second gorilla. 707 00:32:22,440 --> 00:32:26,850 So notice how this entire program takes basically no lines at all. 708 00:32:26,850 --> 00:32:28,850 And it could have taken a lot more if we did not 709 00:32:28,850 --> 00:32:32,030 have this concept of inheritance and the super keyword to help us out. 710 00:32:32,030 --> 00:32:34,570 711 00:32:34,570 --> 00:32:38,220 So now let's move on to something a little bit more fun, specifically what 712 00:32:38,220 --> 00:32:40,150 are called abstract classes. 713 00:32:40,150 --> 00:32:42,270 Abstract classes are very, very important 714 00:32:42,270 --> 00:32:46,200 because these are classes that maybe you do not 715 00:32:46,200 --> 00:32:49,827 know exactly what you want to be inside of them yet, 716 00:32:49,827 --> 00:32:51,660 but you know that this should exist anyways. 717 00:32:51,660 --> 00:32:54,420 It's kind of like a blueprint for a house. 718 00:32:54,420 --> 00:32:55,920 You're not building a house yet. 719 00:32:55,920 --> 00:32:58,753 But you kind of have an idea of what you want inside of it, like you 720 00:32:58,753 --> 00:33:01,320 want a bathroom, a kitchen, a bedroom. 721 00:33:01,320 --> 00:33:04,980 And based off of this, you can create actual house classes or actual house 722 00:33:04,980 --> 00:33:05,730 objects. 723 00:33:05,730 --> 00:33:07,950 You're just not ready at this point yet in your code. 724 00:33:07,950 --> 00:33:10,600 And that's why abstract classes are important. 725 00:33:10,600 --> 00:33:12,500 Let's take a look. 726 00:33:12,500 --> 00:33:15,620 Here, we have an abstract class called Animal. 727 00:33:15,620 --> 00:33:20,540 Notice here that we have a child class that extends Animal. 728 00:33:20,540 --> 00:33:22,260 So Animal is the parent class. 729 00:33:22,260 --> 00:33:24,050 Swan is the child class. 730 00:33:24,050 --> 00:33:27,530 And up here, very important, we have an abstract keyword 731 00:33:27,530 --> 00:33:28,850 that we have not introduced. 732 00:33:28,850 --> 00:33:33,350 And specifically, inside of Animal, we have an abstract method. 733 00:33:33,350 --> 00:33:36,200 Public abstract string getName has nothing inside of it. 734 00:33:36,200 --> 00:33:37,910 It doesn't even have the braces anymore. 735 00:33:37,910 --> 00:33:39,826 It just has a semi-colon to signify that there 736 00:33:39,826 --> 00:33:42,176 is nothing to be implemented here. 737 00:33:42,176 --> 00:33:44,300 The reason being, when you have an abstract method, 738 00:33:44,300 --> 00:33:48,290 it means that you're leaving it blank for children classes to take care of. 739 00:33:48,290 --> 00:33:50,480 You're not going to implement it in your class 740 00:33:50,480 --> 00:33:52,954 because you don't know what it's going to do yet. 741 00:33:52,954 --> 00:33:54,620 Why don't we know what it's going to do? 742 00:33:54,620 --> 00:33:57,180 Maybe we don't know the name of the animal yet. 743 00:33:57,180 --> 00:33:58,760 Therefore, we cannot get the name. 744 00:33:58,760 --> 00:34:00,290 But we know that at some point in the future, when 745 00:34:00,290 --> 00:34:02,900 we create different objects, they are going to have names. 746 00:34:02,900 --> 00:34:04,775 And we want them to be able to get the names. 747 00:34:04,775 --> 00:34:08,030 So we specify in our blueprint that there will be a getName method. 748 00:34:08,030 --> 00:34:10,050 But we don't create it just yet. 749 00:34:10,050 --> 00:34:15,835 And now when we go below to Swan, we are extending the Animal parent class, 750 00:34:15,835 --> 00:34:17,210 in this case, the abstract class. 751 00:34:17,210 --> 00:34:21,530 And then we are implementing the abstract method that was in Animal. 752 00:34:21,530 --> 00:34:24,600 Public string, getName-- we drop the abstract keyword, 753 00:34:24,600 --> 00:34:25,731 and now we have braces. 754 00:34:25,731 --> 00:34:26,855 And we implement it inside. 755 00:34:26,855 --> 00:34:29,790 756 00:34:29,790 --> 00:34:33,780 So notice that in this case, we have to make everything identical. 757 00:34:33,780 --> 00:34:36,777 And then by implementing this method, we are successfully taking 758 00:34:36,777 --> 00:34:39,110 our blueprint and building something concrete out of it. 759 00:34:39,110 --> 00:34:41,650 We're taking our getName method and putting a swan 760 00:34:41,650 --> 00:34:46,230 here because we now know exactly what information we want to put inside. 761 00:34:46,230 --> 00:34:48,100 Now, I have a question for you. 762 00:34:48,100 --> 00:34:52,500 What if we have the public class Swan extending the Animal, 763 00:34:52,500 --> 00:34:57,074 but we do not try to implement the getName method? 764 00:34:57,074 --> 00:35:00,240 What if, instead, we just have a random method here that says, like, getAge, 765 00:35:00,240 --> 00:35:01,739 and it takes the age and returns it? 766 00:35:01,739 --> 00:35:04,130 What would happen here? 767 00:35:04,130 --> 00:35:06,220 Will this compile? 768 00:35:06,220 --> 00:35:09,070 The answer is no, unfortunately, because Java 769 00:35:09,070 --> 00:35:12,402 notices that you had an abstract class with an abstract method. 770 00:35:12,402 --> 00:35:14,860 When you create your child class and you inherit everything 771 00:35:14,860 --> 00:35:18,370 from the parent class, you inherited an abstract method. 772 00:35:18,370 --> 00:35:20,007 But you did not implement it. 773 00:35:20,007 --> 00:35:22,340 You did not take your blueprint and do anything with it. 774 00:35:22,340 --> 00:35:23,560 So what is happening here? 775 00:35:23,560 --> 00:35:27,280 Java is saying that, unfortunately, your class cannot be concrete because it 776 00:35:27,280 --> 00:35:29,630 still has something abstract inside of it. 777 00:35:29,630 --> 00:35:30,650 How do we fix this? 778 00:35:30,650 --> 00:35:35,350 Well, the only way to fix it is to make the Swan class abstract, as well. 779 00:35:35,350 --> 00:35:38,770 Because Swan class is a child of Animal, if Animal 780 00:35:38,770 --> 00:35:42,460 has an abstract method the Swan does not implement concretely, 781 00:35:42,460 --> 00:35:44,920 Swan also has to be abstract. 782 00:35:44,920 --> 00:35:48,070 The other solution to this is obviously what we saw before, where it just 783 00:35:48,070 --> 00:35:50,419 implements the getName method. 784 00:35:50,419 --> 00:35:52,210 But if, for some reason, we didn't want to, 785 00:35:52,210 --> 00:35:55,820 we would have to specify Swan as an abstract class. 786 00:35:55,820 --> 00:35:59,040 Even though it might have these concrete methods inside of it, 787 00:35:59,040 --> 00:36:01,990 as it has more than zero abstract methods, 788 00:36:01,990 --> 00:36:04,720 as long as it has at least one abstract method, 789 00:36:04,720 --> 00:36:07,240 it has to be specified as abstract. 790 00:36:07,240 --> 00:36:11,200 And one of the downsides of abstract methods and abstract classes is that 791 00:36:11,200 --> 00:36:12,940 you cannot create instances of it. 792 00:36:12,940 --> 00:36:15,407 That means we can't create an Animal object. 793 00:36:15,407 --> 00:36:17,740 And in this case, we can't create a Swan object, either, 794 00:36:17,740 --> 00:36:22,420 because we have an abstract keyword inside, 795 00:36:22,420 --> 00:36:24,220 All right, so now, let's go ahead and talk 796 00:36:24,220 --> 00:36:27,800 about something a little bit more complicated, 797 00:36:27,800 --> 00:36:30,930 a little bit less concrete, too, interfaces. 798 00:36:30,930 --> 00:36:35,830 So in Java, sometimes you want to have multiple parents for a single class. 799 00:36:35,830 --> 00:36:38,710 But obviously, with abstract classes and inheritance in general, 800 00:36:38,710 --> 00:36:39,940 we can't do that. 801 00:36:39,940 --> 00:36:42,280 But we can do that with interfaces. 802 00:36:42,280 --> 00:36:46,270 And interfaces thus bring along the concept of multiple inheritance. 803 00:36:46,270 --> 00:36:49,870 One class can inherit multiple interfaces 804 00:36:49,870 --> 00:36:52,630 because interfaces, as we'll define in a moment, 805 00:36:52,630 --> 00:36:54,820 are kind of like what they sound like. 806 00:36:54,820 --> 00:36:58,510 They're very, very flexible, very, very elastic programs 807 00:36:58,510 --> 00:37:00,600 that do exactly what you want them to do, 808 00:37:00,600 --> 00:37:03,270 and don't have much already set inside of them. 809 00:37:03,270 --> 00:37:05,140 They're kind of like a touch screen. 810 00:37:05,140 --> 00:37:07,300 They're kind of like a holograph, something 811 00:37:07,300 --> 00:37:12,110 that you can control to your own will and manipulate it like you wish. 812 00:37:12,110 --> 00:37:14,240 So let's take a look at an interface example. 813 00:37:14,240 --> 00:37:16,840 So public interface has Tail. 814 00:37:16,840 --> 00:37:19,990 Notice the inside of this, we have an abstract method. 815 00:37:19,990 --> 00:37:22,029 But do we have anything else? 816 00:37:22,029 --> 00:37:24,070 The answer is no, because inside of an interface, 817 00:37:24,070 --> 00:37:25,900 you cannot have anything concrete. 818 00:37:25,900 --> 00:37:27,670 All you can have are abstract methods. 819 00:37:27,670 --> 00:37:30,210 You can't even have variables. 820 00:37:30,210 --> 00:37:34,330 So inside of this abstract class, we have specified something 821 00:37:34,330 --> 00:37:37,810 that we will potentially implement, a getTailLength method. 822 00:37:37,810 --> 00:37:41,320 But do we have any idea what our tail length is, or how we get it? 823 00:37:41,320 --> 00:37:42,430 Not right now. 824 00:37:42,430 --> 00:37:45,730 We have to wait until later when we implement the Mouse class. 825 00:37:45,730 --> 00:37:48,400 Notice here that the Mouse uses an important keyword 826 00:37:48,400 --> 00:37:50,530 called implement hasTail. 827 00:37:50,530 --> 00:37:54,400 This means that we are taking interface and putting it inside of our class, 828 00:37:54,400 --> 00:37:58,270 and we're going to specify concretely all of the abstract methods 829 00:37:58,270 --> 00:37:59,890 that the interface had before. 830 00:37:59,890 --> 00:38:02,950 In this case, the only abstract method we had was getTailLength. 831 00:38:02,950 --> 00:38:06,610 And so we did that below with our call of getTailLength, 832 00:38:06,610 --> 00:38:08,680 and then our returning of the tail length, 833 00:38:08,680 --> 00:38:13,120 as we now have a tail length to return, because we've specified a Mouse, 834 00:38:13,120 --> 00:38:17,760 and mice have tail lengths of 5, for some reason. 835 00:38:17,760 --> 00:38:22,510 So let's take a look at how interfaces are different from abstract classes, 836 00:38:22,510 --> 00:38:25,660 specifically because now, we are going to create two interfaces. 837 00:38:25,660 --> 00:38:27,100 One has the name hasTail. 838 00:38:27,100 --> 00:38:29,200 The other has the name hasWhiskers. 839 00:38:29,200 --> 00:38:31,510 And in this case, when we create a Mouse class, 840 00:38:31,510 --> 00:38:33,865 we can implement both of the interfaces. 841 00:38:33,865 --> 00:38:37,840 This is something we could not do with the extend keyword and we cannot do 842 00:38:37,840 --> 00:38:39,490 with our inheritance alone. 843 00:38:39,490 --> 00:38:43,570 In this case, we can inherit both of these interfaces, 844 00:38:43,570 --> 00:38:46,450 and then we have to implement both of them. 845 00:38:46,450 --> 00:38:48,750 Notice that here, I have accidentally missed something. 846 00:38:48,750 --> 00:38:52,730 I did not have a getNumberOfWhiskers method at the end. 847 00:38:52,730 --> 00:38:55,750 What do you think will happen to this code? 848 00:38:55,750 --> 00:38:56,630 That is correct. 849 00:38:56,630 --> 00:38:59,890 There will be a compiler error specifically for this line 850 00:38:59,890 --> 00:39:04,090 because here, we have specified a method that is abstract, 851 00:39:04,090 --> 00:39:06,420 but we have not implemented it below. 852 00:39:06,420 --> 00:39:08,980 We have not concretely implemented it below, and therefore, 853 00:39:08,980 --> 00:39:11,350 Mouse cannot be a concrete class. 854 00:39:11,350 --> 00:39:12,100 Now wait a minute. 855 00:39:12,100 --> 00:39:14,016 You might be telling me to back up because you 856 00:39:14,016 --> 00:39:18,150 see that in this line of code, we don't have an abstract keyword. 857 00:39:18,150 --> 00:39:20,890 Well, the reason is in interfaces, Java will 858 00:39:20,890 --> 00:39:22,747 assume that everything is abstract. 859 00:39:22,747 --> 00:39:24,830 We don't even have to put the word abstract in it. 860 00:39:24,830 --> 00:39:27,447 It will automatically assume that it is in there, 861 00:39:27,447 --> 00:39:30,280 and the reason being that it knows that if we put a semi-colon after 862 00:39:30,280 --> 00:39:33,040 the parentheses, there cannot be anything inside of this method. 863 00:39:33,040 --> 00:39:35,980 Because it's in an interface, that means that it has to be abstract. 864 00:39:35,980 --> 00:39:38,290 So this is another way that Java simplifies the hassle 865 00:39:38,290 --> 00:39:40,390 that programmers have to go through. 866 00:39:40,390 --> 00:39:43,079 But in this case, how do we solve the problem? 867 00:39:43,079 --> 00:39:45,370 Well, if we're trying to just solve the compiler error, 868 00:39:45,370 --> 00:39:47,703 something we could obviously do is just put the abstract 869 00:39:47,703 --> 00:39:51,500 in front of the Mouse class, and now, it is an abstract class. 870 00:39:51,500 --> 00:39:55,150 Because it has this abstract method inside that it has not implemented, 871 00:39:55,150 --> 00:39:56,730 we will just specify it is abstract. 872 00:39:56,730 --> 00:40:00,160 But now, we cannot create any Mouse objects. 873 00:40:00,160 --> 00:40:02,379 Obviously, there are such thing as mice objects, 874 00:40:02,379 --> 00:40:03,920 and therefore we want to create them. 875 00:40:03,920 --> 00:40:06,630 So how do we make a better solution to this? 876 00:40:06,630 --> 00:40:08,982 A better solution is here. 877 00:40:08,982 --> 00:40:10,690 Notice they have the same two interfaces, 878 00:40:10,690 --> 00:40:13,930 but now, we have implemented the method. 879 00:40:13,930 --> 00:40:17,290 We have said, getNumberOfWhiskers you return the number of whiskers, 880 00:40:17,290 --> 00:40:22,120 and we specify the number of whiskers above, as 10 for mice. 881 00:40:22,120 --> 00:40:23,890 But you have to be careful here. 882 00:40:23,890 --> 00:40:26,500 Notice how we have also created a constructor inside 883 00:40:26,500 --> 00:40:29,920 of our class that specifies a tail length. 884 00:40:29,920 --> 00:40:32,050 Well, why is this not good class design? 885 00:40:32,050 --> 00:40:34,980 886 00:40:34,980 --> 00:40:37,680 Yes, it's because we have no control over the number of whiskers 887 00:40:37,680 --> 00:40:39,030 the mouse might have. 888 00:40:39,030 --> 00:40:40,980 Good class design would dictate that we also 889 00:40:40,980 --> 00:40:45,180 create a second constructor that specifies both the tail length 890 00:40:45,180 --> 00:40:48,030 and the number of whiskers so that even though we're 891 00:40:48,030 --> 00:40:51,840 overloading the constructors, we can now change the number of whiskers 892 00:40:51,840 --> 00:40:53,912 that the mouse might have to our desire. 893 00:40:53,912 --> 00:40:55,620 And that way, when we get our tail length 894 00:40:55,620 --> 00:40:58,030 and we get out number of whiskers, we don't just get 10 every single time 895 00:40:58,030 --> 00:40:58,863 we call this method. 896 00:40:58,863 --> 00:41:01,680 We can get something different based on how we create our mouse. 897 00:41:01,680 --> 00:41:04,638 And this is part of the concept of good class design, because this way, 898 00:41:04,638 --> 00:41:07,360 you give the user more functionality with your program. 899 00:41:07,360 --> 00:41:10,110 The user can do more with it, and therefore, it has more potential 900 00:41:10,110 --> 00:41:11,565 to help you in other projects. 901 00:41:11,565 --> 00:41:14,502 902 00:41:14,502 --> 00:41:18,630 All right, so going forward, we have just covered some good class design 903 00:41:18,630 --> 00:41:22,050 by talking about inheritance, interfaces, abstract classes. 904 00:41:22,050 --> 00:41:26,750 And now, we're going to turn it over to the final section of the seminar, which 905 00:41:26,750 --> 00:41:30,370 will talk about methods and the concept of encapsulation. 906 00:41:30,370 --> 00:41:33,220 907 00:41:33,220 --> 00:41:37,550 To understand encapsulation, we first have to talk about what in the world 908 00:41:37,550 --> 00:41:39,170 are packages. 909 00:41:39,170 --> 00:41:43,370 Packages are kind of, in Java, what you think of in real life as folders. 910 00:41:43,370 --> 00:41:47,720 These are just little boxes that you put relevant classes inside of. 911 00:41:47,720 --> 00:41:51,210 And it groups it together so they have some common things 912 00:41:51,210 --> 00:41:55,290 and some common functions that you can call once instead of multiple times. 913 00:41:55,290 --> 00:41:58,270 So let's take a look at a package. 914 00:41:58,270 --> 00:42:02,974 So this, at the very top, is what is inside of your computer's directory 915 00:42:02,974 --> 00:42:04,390 when you are accessing this class. 916 00:42:04,390 --> 00:42:06,930 It is inside of a folder called packagea. 917 00:42:06,930 --> 00:42:08,890 It is called ClassA dot Java. 918 00:42:08,890 --> 00:42:11,350 Notice here that when we look at our actual program, 919 00:42:11,350 --> 00:42:14,937 we have what is specified at the top as package, packagea. 920 00:42:14,937 --> 00:42:16,270 This is the name of the package. 921 00:42:16,270 --> 00:42:18,610 This is the package-specifying keyword. 922 00:42:18,610 --> 00:42:21,820 And down below, we start implementing our actual class, 923 00:42:21,820 --> 00:42:24,850 which has a name as we have specified above in the file. 924 00:42:24,850 --> 00:42:29,139 So this just means that ClassA is inside of this package called packagea. 925 00:42:29,139 --> 00:42:30,430 Well, that's not that much fun. 926 00:42:30,430 --> 00:42:33,280 But what about if we created another package? 927 00:42:33,280 --> 00:42:36,430 So now, we are inside of a different folder, packageb. 928 00:42:36,430 --> 00:42:38,860 And we have a ClassB file inside of it. 929 00:42:38,860 --> 00:42:41,800 Notice that ClassB belongs to the package packageb, 930 00:42:41,800 --> 00:42:44,740 and it imports packagea dot class. 931 00:42:44,740 --> 00:42:47,150 We've talked about the import method before. 932 00:42:47,150 --> 00:42:49,570 And importing is very important because now, we 933 00:42:49,570 --> 00:42:52,990 can access things that are inside of ClassA within ClassB. 934 00:42:52,990 --> 00:42:55,630 Specifically, we now can create an object 935 00:42:55,630 --> 00:42:58,211 of type ClassA inside of our ClassB 936 00:42:58,211 --> 00:42:59,960 And this might get confusing very quickly. 937 00:42:59,960 --> 00:43:02,740 But for now, it suffices to say that packages are important 938 00:43:02,740 --> 00:43:04,570 just so we can group things together. 939 00:43:04,570 --> 00:43:07,907 And that will be relevant when we start talking about encapsulation. 940 00:43:07,907 --> 00:43:11,570 941 00:43:11,570 --> 00:43:15,470 All right, so going back to a slide that we talked about previously 942 00:43:15,470 --> 00:43:20,590 with making methods, I want us to take a moment to look at these words over here 943 00:43:20,590 --> 00:43:22,640 that I said we would talk about leader. 944 00:43:22,640 --> 00:43:26,420 Public is what is called an access modifier. 945 00:43:26,420 --> 00:43:31,190 An access modifier in Java specifies who exactly can see our information. 946 00:43:31,190 --> 00:43:35,390 When I talked earlier about the concept of encapsulation, 947 00:43:35,390 --> 00:43:39,720 it is so that only specific users, only specific programmers, that we want to, 948 00:43:39,720 --> 00:43:42,910 can have access to our code, can be able to change our code. 949 00:43:42,910 --> 00:43:45,320 Well, how do we specify who gets to look at our code? 950 00:43:45,320 --> 00:43:47,120 It is with our access modifiers. 951 00:43:47,120 --> 00:43:50,469 Here, everything is public, which sounds basically like what it means. 952 00:43:50,469 --> 00:43:52,010 It means that it's public to anybody. 953 00:43:52,010 --> 00:43:53,474 Anybody can use this. 954 00:43:53,474 --> 00:43:54,390 Anybody can change it. 955 00:43:54,390 --> 00:44:00,840 Anybody can reference these methods and reference this class. 956 00:44:00,840 --> 00:44:07,290 But what if we wanted a different degree of accessibility for our files, 957 00:44:07,290 --> 00:44:09,810 or for our variables, or for our methods? 958 00:44:09,810 --> 00:44:12,960 How do we specify that without just the public keyword? 959 00:44:12,960 --> 00:44:15,390 Well, Java actually has four different levels 960 00:44:15,390 --> 00:44:18,550 of specificity for different levels of access. 961 00:44:18,550 --> 00:44:21,250 Let us start with the most restrictive access modifier. 962 00:44:21,250 --> 00:44:22,630 It is called private. 963 00:44:22,630 --> 00:44:24,480 Private means exactly what it is. 964 00:44:24,480 --> 00:44:26,580 It is very private because when you specify 965 00:44:26,580 --> 00:44:31,830 a private variable or private method, only the class that is inside 966 00:44:31,830 --> 00:44:32,910 can use it. 967 00:44:32,910 --> 00:44:36,660 Nothing outside of that class can ever reference this private method 968 00:44:36,660 --> 00:44:38,640 or this private object. 969 00:44:38,640 --> 00:44:43,260 Afterwards, we have default, also known as package private access. 970 00:44:43,260 --> 00:44:46,140 Default is not a keyword we'll ever see in a program, 971 00:44:46,140 --> 00:44:48,360 because default is just what happens if you 972 00:44:48,360 --> 00:44:53,100 don't put an access modifier in front of your object or in front of your method. 973 00:44:53,100 --> 00:44:56,070 Default means that only other classes inside of your package, 974 00:44:56,070 --> 00:44:59,520 inside of your folder, will be able to access and change 975 00:44:59,520 --> 00:45:02,610 the parameters that are specified as default. Nothing outside of that, 976 00:45:02,610 --> 00:45:05,940 no other folders in other directories, and not even your child classes, 977 00:45:05,940 --> 00:45:08,440 can touch your objects and your methods. 978 00:45:08,440 --> 00:45:10,950 979 00:45:10,950 --> 00:45:14,420 The second to least most restrictive is protected. 980 00:45:14,420 --> 00:45:17,720 The protected access modifier says that any classes that 981 00:45:17,720 --> 00:45:23,180 are children class or inside the same package can access your methods 982 00:45:23,180 --> 00:45:26,240 and access your variables that you specify as protected 983 00:45:26,240 --> 00:45:27,680 as a keyword in front. 984 00:45:27,680 --> 00:45:29,630 And finally, we have obviously public, which 985 00:45:29,630 --> 00:45:33,890 says that anybody who references this class can reference the variables 986 00:45:33,890 --> 00:45:36,430 in the methods inside of it. 987 00:45:36,430 --> 00:45:42,070 So why does Java have all these different differing levels 988 00:45:42,070 --> 00:45:43,849 of accessibility? 989 00:45:43,849 --> 00:45:46,390 Why didn't we ever talk about this in C, and why did we never 990 00:45:46,390 --> 00:45:47,710 talk about this in Python? 991 00:45:47,710 --> 00:45:50,260 The reason being that it is especially important for Java 992 00:45:50,260 --> 00:45:54,280 as a real world coding language, because for a lot of Java projects 993 00:45:54,280 --> 00:45:58,690 that handle sensitive information, like potentially bank accounts or deposits 994 00:45:58,690 --> 00:46:00,940 and transactions, you don't want certain people, 995 00:46:00,940 --> 00:46:05,530 like customers potentially, or clients, to be able to see, like, the reserves 996 00:46:05,530 --> 00:46:10,420 that the bank has, or the deposits that other users are making. 997 00:46:10,420 --> 00:46:14,580 Therefore, making certain information private or default or protected 998 00:46:14,580 --> 00:46:18,670 to the differing levels that you want gives you customization, and also 999 00:46:18,670 --> 00:46:22,180 gives you security with your programs. 1000 00:46:22,180 --> 00:46:26,910 So let us talk about a duck study, a case study in which we will highlight 1001 00:46:26,910 --> 00:46:30,950 all the different features of accessibility and access 1002 00:46:30,950 --> 00:46:35,910 modifiers using a family of ducks that we are going to illustrate below. 1003 00:46:35,910 --> 00:46:39,690 Notice here that we have one big package called pond dot duck. 1004 00:46:39,690 --> 00:46:42,630 Inside, we have four classes, FatherDuck, MotherDuck, BadDuckling, 1005 00:46:42,630 --> 00:46:43,680 and GoodDuckling. 1006 00:46:43,680 --> 00:46:47,400 And then we create another package called pond dot swan. 1007 00:46:47,400 --> 00:46:51,130 And inside of it, we have what is called a BadCygnet class. 1008 00:46:51,130 --> 00:46:54,160 So what are we going to do with this case study? 1009 00:46:54,160 --> 00:46:57,760 Let's start by looking at the file for FatherDuck. 1010 00:46:57,760 --> 00:47:00,934 Notice the inside of it, we have the private keyword for everything. 1011 00:47:00,934 --> 00:47:03,100 FatherDuck is very private about all of his actions. 1012 00:47:03,100 --> 00:47:05,940 He doesn't want others to be able to see or imitate him. 1013 00:47:05,940 --> 00:47:09,270 So at the top, he obviously quacks because he is a duck. 1014 00:47:09,270 --> 00:47:11,430 But he is private with it. 1015 00:47:11,430 --> 00:47:15,870 Well, we can also call it to quack, or for it to make noise, 1016 00:47:15,870 --> 00:47:18,630 by using the quack method that is also private. 1017 00:47:18,630 --> 00:47:22,830 Notice here that we are referencing noise, which is a private parameter. 1018 00:47:22,830 --> 00:47:27,210 But because we are in the same class, it is valid for us to reference it. 1019 00:47:27,210 --> 00:47:33,510 Now, notice below that we are also specifying a makeNoise method. 1020 00:47:33,510 --> 00:47:38,070 And makeNoise is also going to reference the quack method inside of FatherDuck. 1021 00:47:38,070 --> 00:47:41,400 But because it is inside the same class, even though quack is private, 1022 00:47:41,400 --> 00:47:44,500 you can still reference it validly. 1023 00:47:44,500 --> 00:47:47,590 So having set FatherDuck in the water, we now 1024 00:47:47,590 --> 00:47:50,290 go back and talk about BadDuckling. 1025 00:47:50,290 --> 00:47:52,300 BadDuckling really wants to be like FatherDuck. 1026 00:47:52,300 --> 00:47:59,224 So inside of its makeNoise method, it will create a FatherDuck object. 1027 00:47:59,224 --> 00:48:01,390 First of all, how can you create a FatherDuck object 1028 00:48:01,390 --> 00:48:02,772 inside of the BadDuckling class? 1029 00:48:02,772 --> 00:48:04,480 It's because they're in the same package. 1030 00:48:04,480 --> 00:48:07,880 They're both part of pond dot duck, and therefore, like we saw before, 1031 00:48:07,880 --> 00:48:09,820 you can create instances of the other class 1032 00:48:09,820 --> 00:48:11,980 because they all start with the public keyword. 1033 00:48:11,980 --> 00:48:16,120 However, after you've created your FatherDuck object, 1034 00:48:16,120 --> 00:48:17,320 you try to make it quack. 1035 00:48:17,320 --> 00:48:20,380 But can BadDuckling really quack like FatherDuck does? 1036 00:48:20,380 --> 00:48:23,590 The answer is no, because quack is private, 1037 00:48:23,590 --> 00:48:27,250 and therefore, only FatherDuck inside of its own class can quack. 1038 00:48:27,250 --> 00:48:30,550 The FatherDuck inside of this class cannot quack because it is 1039 00:48:30,550 --> 00:48:34,270 in a different class, even though it is referencing the same type of object. 1040 00:48:34,270 --> 00:48:36,640 The same thing will happen when you go below 1041 00:48:36,640 --> 00:48:38,630 and you try to make the duck create noise, 1042 00:48:38,630 --> 00:48:40,150 and you access the noise variable. 1043 00:48:40,150 --> 00:48:43,360 The noise variable was also private inside of FatherDuck, 1044 00:48:43,360 --> 00:48:47,660 and therefore, it is not accessible here. 1045 00:48:47,660 --> 00:48:49,300 Now let's take a look at MotherDuck. 1046 00:48:49,300 --> 00:48:52,150 MotherDuck is a little bit more forgiving with what she does. 1047 00:48:52,150 --> 00:48:54,310 Instead of making everything private, she just 1048 00:48:54,310 --> 00:48:59,370 makes everything default, default meaning that when she quacks, 1049 00:48:59,370 --> 00:49:00,865 there is nothing in the front. 1050 00:49:00,865 --> 00:49:03,490 When she creates a quack method, there is nothing in the front. 1051 00:49:03,490 --> 00:49:06,060 And when she makes noise, there's nothing in the front. 1052 00:49:06,060 --> 00:49:07,900 Notice that these are all valid references 1053 00:49:07,900 --> 00:49:10,660 to the noise variable and the quack method, 1054 00:49:10,660 --> 00:49:15,430 because as the default specifier, not only can MotherDuck access 1055 00:49:15,430 --> 00:49:19,270 its own methods, but its children-- 1056 00:49:19,270 --> 00:49:22,870 and other classes inside of its package, not its children-- other classes inside 1057 00:49:22,870 --> 00:49:26,330 of its package can access its methods and as variables, as well. 1058 00:49:26,330 --> 00:49:29,200 So let's take a look at GoodDuckling. 1059 00:49:29,200 --> 00:49:32,950 GoodDuckling, as a class, is inside of the same package, 1060 00:49:32,950 --> 00:49:35,664 and therefore, it really wants to imitate MotherDuck. 1061 00:49:35,664 --> 00:49:36,580 And how does it do it? 1062 00:49:36,580 --> 00:49:39,880 It creates a MotherDuck object, which we can before because it was public, 1063 00:49:39,880 --> 00:49:43,900 and it tries to quack and make duck noises with MotherDuck. 1064 00:49:43,900 --> 00:49:47,140 The reason that this works is because MotherDuck was default, 1065 00:49:47,140 --> 00:49:49,960 and therefore, if it's in the same package, 1066 00:49:49,960 --> 00:49:52,930 you can validly implement these two lines of code. 1067 00:49:52,930 --> 00:49:54,760 You can reference the quack method, and you 1068 00:49:54,760 --> 00:49:59,102 can access the parameters and the variables inside of the MotherDuck 1069 00:49:59,102 --> 00:49:59,602 class. 1070 00:49:59,602 --> 00:50:04,030 1071 00:50:04,030 --> 00:50:06,110 Ah, sorry, sorry, yes. 1072 00:50:06,110 --> 00:50:09,500 I unfortunately made a typo here that says private makeNoise, which 1073 00:50:09,500 --> 00:50:14,925 means that if we specify it over here with a makeNoise Method that 1074 00:50:14,925 --> 00:50:16,550 would mean that we would have an error. 1075 00:50:16,550 --> 00:50:18,860 Thankfully, we're not referencing the makeNoise method. 1076 00:50:18,860 --> 00:50:21,200 So we're not going to generate a compiler error. 1077 00:50:21,200 --> 00:50:25,670 But that is a very accurate observation by you, yes. 1078 00:50:25,670 --> 00:50:27,950 All right, moving forward, we will go ahead 1079 00:50:27,950 --> 00:50:31,380 and talk about the BadCygnet class. 1080 00:50:31,380 --> 00:50:34,404 BadCygnet class is inside of a different package. 1081 00:50:34,404 --> 00:50:36,195 It is no longer inside of the duck package. 1082 00:50:36,195 --> 00:50:38,000 It is now a swan package. 1083 00:50:38,000 --> 00:50:42,170 And even though we are successfully importing the MotherDuck class 1084 00:50:42,170 --> 00:50:47,470 from the duck package, when we try to imitate the duck, when 1085 00:50:47,470 --> 00:50:50,240 we try to imitate the MotherDuck, we are successful in creating 1086 00:50:50,240 --> 00:50:53,030 an instance of the class because MotherDuck was public. 1087 00:50:53,030 --> 00:50:57,470 But when we try to quack, and when we try to print the noise of the duck, 1088 00:50:57,470 --> 00:51:00,530 we will get a compiler error because this is unfortunately not 1089 00:51:00,530 --> 00:51:04,310 in the same package, and therefore, MotherDuck's default access 1090 00:51:04,310 --> 00:51:06,470 does not reach BadCygnet. 1091 00:51:06,470 --> 00:51:09,600 And for those of you who don't know, cygnet is a young swan. 1092 00:51:09,600 --> 00:51:12,550 You learn new things every day. 1093 00:51:12,550 --> 00:51:15,720 So now, we're going to move on to protected access, which is probably 1094 00:51:15,720 --> 00:51:20,310 one of the more complicated features of access modifiers inside of Java. 1095 00:51:20,310 --> 00:51:23,550 So we're put a stop sign here to say, stop, 1096 00:51:23,550 --> 00:51:27,330 both if you're not allowed inside of this protected access, 1097 00:51:27,330 --> 00:51:30,570 and also so that we can gather ourselves. 1098 00:51:30,570 --> 00:51:33,710 Going forward, we have another second case study 1099 00:51:33,710 --> 00:51:38,039 that has to do with another package called pond dot shore. 1100 00:51:38,039 --> 00:51:40,080 Inside this package, we have a class called Bird. 1101 00:51:40,080 --> 00:51:46,210 And Bird has a lot of protected methods and variables. 1102 00:51:46,210 --> 00:51:49,170 So we have a protected variable called text 1103 00:51:49,170 --> 00:51:52,860 that says that this Bird is floating on the shore. 1104 00:51:52,860 --> 00:51:55,800 And then we have a protected method, floatInWater, 1105 00:51:55,800 --> 00:51:59,220 that prints out the fact that it is floating. 1106 00:51:59,220 --> 00:52:01,490 So remember what we said protected was. 1107 00:52:01,490 --> 00:52:05,090 Protected means that both other classes inside of its package, 1108 00:52:05,090 --> 00:52:13,300 as well as its child classes, can access these two parameters. 1109 00:52:13,300 --> 00:52:17,270 So let's look at a second class called Gosling. 1110 00:52:17,270 --> 00:52:20,970 Gosling extends Bird, because it is a child class of Bird. 1111 00:52:20,970 --> 00:52:23,190 Goslings are all birds, and therefore, even 1112 00:52:23,190 --> 00:52:27,290 though it is in a different package, it is still a child class of Bird. 1113 00:52:27,290 --> 00:52:30,570 And it imports the functionality of Bird through the import statement. 1114 00:52:30,570 --> 00:52:34,760 Now below, Gosling has a method where it tries to swim. 1115 00:52:34,760 --> 00:52:37,710 Notice here that we're able to call the floatInWater method 1116 00:52:37,710 --> 00:52:41,580 and also print out the text without even creating a Bird object. 1117 00:52:41,580 --> 00:52:45,390 That is because we are a child class, and because it is protected access. 1118 00:52:45,390 --> 00:52:49,200 We can access it as a child class even if we're not in the same package. 1119 00:52:49,200 --> 00:52:52,100 1120 00:52:52,100 --> 00:52:54,320 Now let's look at someone different. 1121 00:52:54,320 --> 00:52:56,150 In this case, we have a BirdWatcher. 1122 00:52:56,150 --> 00:52:59,190 This is obviously not a Bird, but it is still in same package, 1123 00:52:59,190 --> 00:53:01,490 and is still along the shore of the pond. 1124 00:53:01,490 --> 00:53:03,800 And therefore, inside of the BirdWatcher class, 1125 00:53:03,800 --> 00:53:08,362 when we try to call watchBird method, then we create a new Bird, 1126 00:53:08,362 --> 00:53:10,070 and we say that the Bird floats in water, 1127 00:53:10,070 --> 00:53:12,770 and the Bird prints out its text of floating 1128 00:53:12,770 --> 00:53:15,867 this will also work because of the fact that they're inside 1129 00:53:15,867 --> 00:53:16,700 of the same package. 1130 00:53:16,700 --> 00:53:21,450 And therefore, protected access extends to the BirdWatcher class. 1131 00:53:21,450 --> 00:53:24,350 But finally, obviously, we need to have something that does not work. 1132 00:53:24,350 --> 00:53:27,730 So let's create a package called inland. 1133 00:53:27,730 --> 00:53:31,620 And we try to import the Bird class from the shore package. 1134 00:53:31,620 --> 00:53:33,620 And we have a BirdWatcher from afar because this 1135 00:53:33,620 --> 00:53:37,390 is a BirdWatcher that is farther inland, and is not along the shore. 1136 00:53:37,390 --> 00:53:40,030 Well if it tries to create a method of watching the birds, 1137 00:53:40,030 --> 00:53:43,030 and it creates a new Bird, and it says the Bird floats in the water, 1138 00:53:43,030 --> 00:53:48,010 and it tries to print out the bird's floating text, is this going to work? 1139 00:53:48,010 --> 00:53:50,680 The answer is no, because there will be a compiler 1140 00:53:50,680 --> 00:53:53,530 error, since this is neither in the same package, 1141 00:53:53,530 --> 00:53:55,690 nor is it a child class of Bird. 1142 00:53:55,690 --> 00:53:58,000 Therefore, it does not have access to the protected 1143 00:53:58,000 --> 00:54:04,600 modifiers and the protected variables and methods inside of the Bird class. 1144 00:54:04,600 --> 00:54:09,950 All right, so now, we are arriving at the end of our time together. 1145 00:54:09,950 --> 00:54:12,490 But I want to leave you all with a quick quiz 1146 00:54:12,490 --> 00:54:14,700 to see if you've been paying attention. 1147 00:54:14,700 --> 00:54:16,870 So here, remember that at the beginning, we 1148 00:54:16,870 --> 00:54:20,440 talked about an Animal class and a Tiger subclass, 1149 00:54:20,440 --> 00:54:22,540 or child class that extended Animal. 1150 00:54:22,540 --> 00:54:26,110 Can anybody tell me if this will compile, and if it does not, 1151 00:54:26,110 --> 00:54:28,030 what the problem is with our code here? 1152 00:54:28,030 --> 00:54:31,334 1153 00:54:31,334 --> 00:54:34,599 SPEAKER 2: The Tiger is trying to call age, but it's [INAUDIBLE].. 1154 00:54:34,599 --> 00:54:35,390 SPEAKER 1: Exactly. 1155 00:54:35,390 --> 00:54:38,810 The Tiger is trying to call the age variable inside of Animal. 1156 00:54:38,810 --> 00:54:42,950 But notice how, when we talked about this example before, it was private. 1157 00:54:42,950 --> 00:54:46,820 Therefore, if I just straight up tried to call the age from the Animal class, 1158 00:54:46,820 --> 00:54:47,570 it will not print. 1159 00:54:47,570 --> 00:54:49,850 There will be a compiler error. 1160 00:54:49,850 --> 00:54:52,220 When we talked about this example on a previous slide, 1161 00:54:52,220 --> 00:54:54,530 I actually went in a roundabout way. 1162 00:54:54,530 --> 00:54:58,160 And when I created this method, I said to get the age first. 1163 00:54:58,160 --> 00:55:00,770 Because getAge is a public method, I can successfully 1164 00:55:00,770 --> 00:55:02,660 call inside of the Tiger class, and then I 1165 00:55:02,660 --> 00:55:05,180 can print out the Tiger's age afterwards. 1166 00:55:05,180 --> 00:55:07,370 So notice that there is this redundancy in this case 1167 00:55:07,370 --> 00:55:09,020 because we specified it as private. 1168 00:55:09,020 --> 00:55:11,780 But this could also be very beneficial because maybe we 1169 00:55:11,780 --> 00:55:15,470 do not want tigers to be able to change the ages of themselves 1170 00:55:15,470 --> 00:55:18,460 or of other animals. 1171 00:55:18,460 --> 00:55:22,777 All right, so now we arrive at the conclusion of our time together. 1172 00:55:22,777 --> 00:55:24,610 And the big question that we've been asking, 1173 00:55:24,610 --> 00:55:27,680 and that I have tried to help you answer, is why. 1174 00:55:27,680 --> 00:55:30,730 Why is Java important to us, and why is Java worth 1175 00:55:30,730 --> 00:55:33,700 learning today, in our day and age? 1176 00:55:33,700 --> 00:55:36,300 The answer is very, very simple. 1177 00:55:36,300 --> 00:55:42,700 It's that Java is a secure, very, very portable, and simple language that 1178 00:55:42,700 --> 00:55:45,550 uses these ideas of encapsulation, of inheritance, 1179 00:55:45,550 --> 00:55:48,460 in order to make writing code much easier for programmers. 1180 00:55:48,460 --> 00:55:52,030 Although these methods and these ideas can be seen in other programming 1181 00:55:52,030 --> 00:55:57,100 languages, potentially like Python, no programming language does it as much 1182 00:55:57,100 --> 00:56:01,780 and emphasizes it as much as Java does. 1183 00:56:01,780 --> 00:56:06,640 In the real world, you most likely see Java applied in applets, in games. 1184 00:56:06,640 --> 00:56:09,220 The reason why it is seen in all these scenarios 1185 00:56:09,220 --> 00:56:12,400 is because Java's inheritance structure makes it easy 1186 00:56:12,400 --> 00:56:16,000 for you to create players, to create obstacles, to create bosses and levels, 1187 00:56:16,000 --> 00:56:19,510 and so on, without spending a huge amount of code. 1188 00:56:19,510 --> 00:56:22,054 And that is where Java usually comes into light, 1189 00:56:22,054 --> 00:56:24,220 and where we will usually deal with it in real life. 1190 00:56:24,220 --> 00:56:27,460 Obviously, Java has many, many other potential applications 1191 00:56:27,460 --> 00:56:32,690 in storing data, in creating objects and projects, and stuff like that. 1192 00:56:32,690 --> 00:56:36,670 But in those areas, it is also potentially viable for other languages 1193 00:56:36,670 --> 00:56:38,189 to replace it. 1194 00:56:38,189 --> 00:56:39,230 So at the end of the day. 1195 00:56:39,230 --> 00:56:42,190 I hope that if you got anything out of this seminar, 1196 00:56:42,190 --> 00:56:45,580 it is an increased understanding and appreciation for Java, specifically 1197 00:56:45,580 --> 00:56:49,120 the nuances that come with encapsulation and inheritance, 1198 00:56:49,120 --> 00:56:52,180 so you can relate a little bit less to the meme I'll depart you all with. 1199 00:56:52,180 --> 00:56:58,830 1200 00:56:58,830 --> 00:57:00,930 Best of luck with all of your coding adventures. 1201 00:57:00,930 --> 00:57:02,440 This has been a CS 50 seminar. 1202 00:57:02,440 --> 00:57:05,060 Thank you very much for watching. 1203 00:57:05,060 --> 00:57:06,660