Introducing Inheritance4:28 with Ben Deitch
In this video we'll learn what inheritance is and use it for the first time!
[MUSIC] 0:00 At this point, we've seen how to create objects with 0:09 fields representing their properties and methods representing their actions. 0:11 In this course, we'll see how to do a lot more with objects by using inheritance. 0:16 Inheritance is actually a pretty simple concept. 0:22 Let's say we have a class called A, and 0:25 this class has a couple fields and a method. 0:28 Now let's say we have another class, B, 0:31 which is exactly like A except it has an extra field and an extra method. 0:33 It's a lot of extra code, and we're definitely repeating ourselves. 0:39 But with inheritance, we can clean things up by just saying that B inherits from A, 0:43 which we do with the extends key word. 0:49 So this code is exactly the same as before, just much shorter. 0:52 Now, in most cases, the class names will make a lot more sense than just A and B. 0:57 But using these simple names, let's us focus on what inheritance actually is. 1:02 A class using another class as its foundation. 1:08 All right, that's enough theory. 1:11 Let's do some practice in a new project. 1:14 Let's pick Create New Project, then click Next, 1:17 select our Command Line App template, hit Next again. 1:22 And for the project name, let's just name it, inheritance, and click Finish. 1:27 Awesome. 1:35 Now, just to make things a little easier for you to see, 1:36 I'm going to put my IntelliJ into full screen mode, okay. 1:40 Now, right below the main class let's create a new class named animal. 1:44 Typically we'd put each class in its own file. 1:51 But since we're learning, it'll be nice to have everything on one page. 1:54 Let's type class Animal, and add the brackets. 1:59 And I'll go ahead and hide the project pane as well. 2:04 Also, note that if we try to make our animal class public, we'll get an error. 2:06 While you can have more than one class in a file, 2:13 all public classes need their own files. 2:17 Al lright, inside this class lets add a string 2:21 field named sound and set it equal to an empty string. 2:26 Then lets add a void method called makeSound and 2:34 use the S-O-U-T shortcut, 2:38 or sout to have it print out the sound variable. 2:41 Nice. 2:47 Next let's make a dog class that will bark when we call the makeSound method. 2:48 But first, let's head up to the main method and delete the comment. 2:54 Then let's copy and paste in the code from the teacher's notes. 2:59 Once we're done with the Dog class, we should be able to run this program, and 3:03 see that our dog has said bark. 3:07 So right below our Animal class, let's create our Dog class by using inheritance. 3:09 So actually, why don't you take a stab at it first? 3:14 Remember, the key word is extends. 3:18 Okay, pause me, and when you come back, I'll show you how I solved it. 3:22 Did you get it? 3:26 Here's how I did it. 3:28 First, create the Dog class, and make it extend from animal. 3:30 Then, inside the Dog class, we'll add a constructor, 3:42 Dog( ) and then the brackets, and inside the constructor, 3:48 we'll set sound = "bark" and add the semicolon. 3:54 Finally, if we run the program, 4:00 We'll see that our dog says bark. 4:06 Awesome job learning about Inheritance. 4:09 Being able to use another class as a foundation for 4:12 new one is a real time saver. 4:16 And it's not hard to imagine how you could start with something simple and 4:18 build your way up to something really complicated. 4:22 More on that in the next video. 4:25
You need to sign up for Treehouse in order to download course files.Sign up