1 00:00:00,053 --> 00:00:09,210 [MUSIC] 2 00:00:09,210 --> 00:00:11,959 At this point, we've seen how to create objects with 3 00:00:11,959 --> 00:00:16,670 fields representing their properties and methods representing their actions. 4 00:00:16,670 --> 00:00:22,145 In this course, we'll see how to do a lot more with objects by using inheritance. 5 00:00:22,145 --> 00:00:25,288 Inheritance is actually a pretty simple concept. 6 00:00:25,288 --> 00:00:28,003 Let's say we have a class called A, and 7 00:00:28,003 --> 00:00:31,059 this class has a couple fields and a method. 8 00:00:31,059 --> 00:00:33,968 Now let's say we have another class, B, 9 00:00:33,968 --> 00:00:39,640 which is exactly like A except it has an extra field and an extra method. 10 00:00:39,640 --> 00:00:43,900 It's a lot of extra code, and we're definitely repeating ourselves. 11 00:00:43,900 --> 00:00:49,900 But with inheritance, we can clean things up by just saying that B inherits from A, 12 00:00:49,900 --> 00:00:52,670 which we do with the extends key word. 13 00:00:52,670 --> 00:00:56,540 So this code is exactly the same as before, just much shorter. 14 00:00:57,640 --> 00:01:02,970 Now, in most cases, the class names will make a lot more sense than just A and B. 15 00:01:02,970 --> 00:01:08,050 But using these simple names, let's us focus on what inheritance actually is. 16 00:01:08,050 --> 00:01:11,860 A class using another class as its foundation. 17 00:01:11,860 --> 00:01:14,050 All right, that's enough theory. 18 00:01:14,050 --> 00:01:17,197 Let's do some practice in a new project. 19 00:01:17,197 --> 00:01:22,033 Let's pick Create New Project, then click Next, 20 00:01:22,033 --> 00:01:27,328 select our Command Line App template, hit Next again. 21 00:01:27,328 --> 00:01:33,726 And for the project name, let's just name it, inheritance, and click Finish. 22 00:01:35,908 --> 00:01:36,560 Awesome. 23 00:01:36,560 --> 00:01:40,414 Now, just to make things a little easier for you to see, 24 00:01:40,414 --> 00:01:44,517 I'm going to put my IntelliJ into full screen mode, okay. 25 00:01:44,517 --> 00:01:49,708 Now, right below the main class let's create a new class named animal. 26 00:01:51,585 --> 00:01:54,913 Typically we'd put each class in its own file. 27 00:01:54,913 --> 00:01:59,288 But since we're learning, it'll be nice to have everything on one page. 28 00:01:59,288 --> 00:02:04,043 Let's type class Animal, and add the brackets. 29 00:02:04,043 --> 00:02:06,084 And I'll go ahead and hide the project pane as well. 30 00:02:06,084 --> 00:02:13,718 Also, note that if we try to make our animal class public, we'll get an error. 31 00:02:13,718 --> 00:02:17,804 While you can have more than one class in a file, 32 00:02:17,804 --> 00:02:21,387 all public classes need their own files. 33 00:02:21,387 --> 00:02:26,546 Al lright, inside this class lets add a string 34 00:02:26,546 --> 00:02:32,509 field named sound and set it equal to an empty string. 35 00:02:34,020 --> 00:02:38,743 Then lets add a void method called makeSound and 36 00:02:38,743 --> 00:02:41,814 use the S-O-U-T shortcut, 37 00:02:41,814 --> 00:02:47,470 or sout to have it print out the sound variable. 38 00:02:47,470 --> 00:02:48,480 Nice. 39 00:02:48,480 --> 00:02:54,060 Next let's make a dog class that will bark when we call the makeSound method. 40 00:02:54,060 --> 00:02:57,540 But first, let's head up to the main method and delete the comment. 41 00:02:59,270 --> 00:03:03,000 Then let's copy and paste in the code from the teacher's notes. 42 00:03:03,000 --> 00:03:07,070 Once we're done with the Dog class, we should be able to run this program, and 43 00:03:07,070 --> 00:03:09,750 see that our dog has said bark. 44 00:03:09,750 --> 00:03:14,946 So right below our Animal class, let's create our Dog class by using inheritance. 45 00:03:14,946 --> 00:03:18,792 So actually, why don't you take a stab at it first? 46 00:03:18,792 --> 00:03:22,123 Remember, the key word is extends. 47 00:03:22,123 --> 00:03:26,464 Okay, pause me, and when you come back, I'll show you how I solved it. 48 00:03:26,464 --> 00:03:28,631 Did you get it? 49 00:03:28,631 --> 00:03:30,215 Here's how I did it. 50 00:03:30,215 --> 00:03:34,718 First, create the Dog class, and make it extend from animal. 51 00:03:42,665 --> 00:03:48,446 Then, inside the Dog class, we'll add a constructor, 52 00:03:48,446 --> 00:03:54,709 Dog( ) and then the brackets, and inside the constructor, 53 00:03:54,709 --> 00:04:00,020 we'll set sound = "bark" and add the semicolon. 54 00:04:00,020 --> 00:04:02,504 Finally, if we run the program, 55 00:04:06,730 --> 00:04:09,916 We'll see that our dog says bark. 56 00:04:09,916 --> 00:04:12,949 Awesome job learning about Inheritance. 57 00:04:12,949 --> 00:04:16,650 Being able to use another class as a foundation for 58 00:04:16,650 --> 00:04:18,773 new one is a real time saver. 59 00:04:18,773 --> 00:04:22,493 And it's not hard to imagine how you could start with something simple and 60 00:04:22,493 --> 00:04:25,143 build your way up to something really complicated. 61 00:04:25,143 --> 00:04:28,116 More on that in the next video.