Loading
Current section: 3. Inheritance and Polymorphism 5 exercises
solution

Method Overriding

Transcript

00:00 Alright. Y'all done? Great. Let's start by creating a get area function for a shape. We're gonna have this just be the default.

00:08 If you haven't overridden this, then we're just gonna return zero. You could also throw an error and say, hey, they didn't implement it. They should have. You could add an interface and say it needs to implement this, however you wanna do that. Then we can override that get area.

00:22 So right here, for getting the area of a circle, you're gonna do math pi times this dot radius power two. And that is gonna get you the area of a circle. And so, now if I come down here and let's just do the circle, and we're gonna see 78. But if I don't override this, then we're gonna see zero. So that, overrides the behavior of the parent.

00:49 That is pretty much it. And then we can do the same thing for rectangle and we'll comment those back here and ta da, we've got, 200 for a 10 by 20. So that makes sense to me. I hope that was interesting and fun for you. That is, method overriding.