Locked lesson.
About this lesson
Inheriting information from other classes.
Exercise files
Download this lesson’s related exercise files.
Class Inheritance.docx59.5 KB Class Inheritance - Solution.docx
59.4 KB
Quick reference
Class Inheritance
Classes can inherit things from other classes.
When to use
Do this to reuse aspects of other classes inside of other classes.
Instructions
To inherit the things from another class:
class New < Old
end
Where Old is the class you want to inherit from.
Hints & tips
- It's easy to inherit things from other classes, just use <
- 00:04 I want to finish talking about classes with something called class inheritance.
- 00:08 Now, we could talk about classes for hours and hours and hours.
- 00:11 I just wanted to sort of give you a very quick introduction to classes,
- 00:15 explain some key concepts, and then we'll move on.
- 00:17 So class inheritance is basically, you can actually inherit something from one class
- 00:22 into another class, so that's what we're going to look at in this video.
- 00:26 So we have our Square class, we've got our attr_assessor, got our initialize,
- 00:31 our area.
- 00:31 So let's go ahead real quick and just create another class, and
- 00:35 let's call it paint.
- 00:39 All right, and I'm not going to do all the initialize, and all the stuff.
- 00:42 Let's just create a quick method called color, and
- 00:47 let's give it an instance variable of color, and set it = blue.
- 00:52 So just a very, very simple class, like I said, just for
- 00:57 purposes of showing this stuff.
- 01:00 So let's initialize our class.
- 01:03 Let's go my_paint = Paint.new and there's no
- 01:08 parameters that we need to pass in, so we're good to go.
- 01:13 So now, we can call a puts my_paint.color.
- 01:20 And if it works correctly, it should just print out blue.
- 01:23 Let's save this and hit Reload.
- 01:25 And sure enough, we see blue, and we're still getting code from our last couple of
- 01:29 videos, but this is what we've got blue.
- 01:31 So all we have in this class, just very, very simple, and
- 01:34 all it does is print out blue.
- 01:36 Well, that's not that useful, but we can actually pull
- 01:39 in all the stuff from this class into this class, and then use it if we want.
- 01:43 And to do that, it's really simple.
- 01:45 All we do is this less than sign, and then you just type in the name of your other class.
- 01:51 In our case, I type S, it already knows that we want square.
- 01:54 So now we can come down here, and let's just inspect this,
- 01:57 we're probably going to get an error here, I'll show you why.
- 02:01 So yeah, we get an error, and
- 02:03 that's because this class requires parameters to be passed in.
- 02:07 And when we initialized our new paint class, we didn't pass any parameters.
- 02:12 So let's pass in the parameter of 30, so that's going to be our side length, right?
- 02:16 Now if we save this and run it again, we get just this @side_length=30.
- 02:22 And you can see our Paint class now has side_length of 30 as a part of it,
- 02:27 even though that's in the Square class, not the Paint class.
- 02:32 So just by doing this, boom, we've sucked in all the stuff from the Square class
- 02:36 into our Paint class, and we can do stuff.
- 02:39 So we can go paint.color, like we did earlier, and we get blue.
- 02:44 We can also go paint.area, because that's a method inside the Square class, right?
- 02:50 So if we save this, boom 900, because area is 30 times 30, or 900.
- 02:57 What else do we have here?
- 02:58 Area initialize, we've got our getters and our setters.
- 03:01 We can use our getters and our setters now with our paint instance,
- 03:05 this instance of this Paint class that we have, this my_paint.
- 03:08 So that's just a very, very quick primer on class inheritance.
- 03:12 The main concept is you just add it right here, and
- 03:15 we'll use this a lot in Ruby on Rails.
- 03:17 This is obviously not a Rails course, but when you see Ruby on Rails, they do a lot
- 03:21 of class inheritance with all their stuff, and you'll see that a lot there.
- 03:25 You'll use it for other things as well.
- 03:26 But anyway, that's class inheritance, that's classes.
- 03:30 I know that was sort of a whirlwind last few videos worth of stuff, and
- 03:33 you might want to go back and watch all these class videos.
- 03:35 Again, it's only until you start using these, and
- 03:37 then things start clicking into place, and it makes sense.
- 03:40 It's just one of those things, classes take a while for people to sort of wrap
- 03:43 their brains around, so don't feel bad if you're kind of struggling to get it.
- 03:46 In the next video, we're going to look at files, and opening a file.
Lesson notes are only available for subscribers.