Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using Composition and Inheritance

00:00 So now you have some ideas of what composition and inheritance means. In this lesson, you’re going to be looking at an example of using both to build up a system.

00:10 It’s going to start with the Point class. This is going to be our low-level class. And we’re also going to make use of the Shape class that uses .points as one of the attributes displaying composition.

00:23 So we’ve got composition in this model already, but we want to extend this model. Instead of just having Shape, we want to have specific shapes. We want to have squares and triangles, for instance.

00:37 But we still want to keep Shape because there’ll be methods that apply to all shapes, not just squares and triangles. So we want to keep this type of structure.

00:50 Let’s see how we might extend that and add in a square shape. The first step would be just creating a new class. Let’s create Square, and we’ll inherit from Shape.

01:02 So now what do we want? One of the useful things about classes is that in the constructor method, you can create checks to make sure that the developer, which in many cases will mean yourself—and yes, you do need to protect yourself against yourself in programming—by creating a check. So let’s override the .__init__() for now.

01:25 We’ll do pretty much the same thing,

01:29 but we want to add in something here. We want to do a check

01:35 if (len(points)—because points is going to be a list—if (len(points) != 4)—because a square has four points, right?—we’ll want to raise an exception, but we don’t want to raise just a bare exception.

01:52 That is considered bad practice because it doesn’t tell you anything about the exception. It just says, oh, this is an error. So let’s create a new exception in the way that is very common in Python.

02:04 So let’s just do it at the top here. class. Let’s call this WrongNumberOfPoints because we’re going have a bunch of shapes, and we’ll subclass that from the base Exception class, and we’ll pass. So we’re just using everything from Exception, but we’re giving it a new name.

02:23 And then we can just use this name here to raise that error if the number of points is not four. So say we pass in five points, that would suggest that it’s a pentagon and not a square, so we want that to give a WrongNumberOfPoints.

02:39 And then after that check is done, then we can assign the attribute self.points = points. Save that, Control + S, and then F5 to run it. Okay, we’re good.

02:51 So now let’s see, square = Square(), and we’re going to have to pass in some points here because if we don’t, you’ll just get WrongNumberOfPoints, the error that we’re raising over here.

03:10 So we’ll take this and create a point.

03:18 Let’s create 1 across, 0 up;

03:25 1 up, 1 across; and then back to 0, 1.

03:32 And then we can close the list and close brackets to complete the instantiation. And now if you look at square, we can see that that’s a Square object, and it will behave in the same way as the Shape object, which contains an array of points.

03:48 But we know the square will have four points in its .points attribute.

03:55 To sum up what you’ve done in that snippet, you created a class which subclasses Exception, and you called it WrongNumberOfPoints.

04:03 This is very common practice in Python, to subclass Exception. All you need to do is pass it because you basically want the same behavior as Exception. You just want it to have a custom name.

04:16 Since we’re going to be checking for the number of points, like in a square has to have four, a triangle has to have three, we want this custom exception. So here is a quick example of inheritance, but that’s not the main example. The main example is the square.

04:30 The Square class subclasses Shape, and then it overrides the .__init__() constructor. Within that, it checks whether the length of the points list is 4, because a square has to have exactly four points. If it doesn’t, then it raises the WrongNumberOfPoints exception.

04:52 If that passes, then it assigns its own attribute to the points list. You could extend that to a triangle, for instance. Pretty much the same thing, except you would check if it has three points instead of four.

05:07 That’s an example of using composition and inheritance within the same system. You’re using composition in the .points attribute because the objects within the list of points are themselves classes that you created.

05:24 And you’re using inheritance because Triangle subclasses Shape to be able to carry out a custom check that is unique to triangles, but triangles are still types of shape.

05:35 So we want to keep that relationship in this case.

Become a Member to join the conversation.