yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 15 | Polymorphism part 2


6m read
·Nov 3, 2024

Hey, this is Mac Heads 101. Um, my name is Jake, and I'm going to be doing another polymorphism tutorial. So, I'm going to be talking about a couple of things in this tutorial, um, hopefully including abstract classes. That's going to be the big one here, but, um, hopefully memorizing my last tutorial.

So, yeah, okay. The first thing I'm going to explain is about, um, making objects. So, when we made an object before, Second s = new Second(); or something like that, right? But here's the thing: what this Second over here means is actually the data type. So, we're making an object of the type Second.

Okay, but here's the thing: these extend the superclass. So, Second is of the type superclass. And I know this is confusing, but just, um, try to follow along. If at any point you get lost, you can pause, think, you know, watch it over, whatever, because this is a confusing topic. It took me a while to understand.

But basically, since it extends the superclass, it inherits from the superclass. Second is of the superclass type, so I can do this: Superclass s = new Second();. I can make a Second object using the data type superclass because Second extends the superclass.

Okay, so that's an important thing to know. Now, here's something you should not do: Superclass sc = new Class();. You don't want to inherit from the superclass. You're not getting an error; it's just not the greatest idea. It's a little too general, I guess is a good way to explain it. You want to have from specific classes; you know, inheriting from the superclass just isn't the best idea for a bunch of reasons.

Um, it's just, you just shouldn't do it. It's kind of hard to explain exactly why, but mainly because it's not specific enough is the best way I can put it, I guess. But just, um, it's not the greatest idea, so just so you don't make that mistake.

You can do this: abstract classes, you cannot make an object out of them. Now there's also concrete. Anything that's not abstract is concrete. These subclasses, they're called subclasses if they extend from a superclass, are concrete classes. This is an abstract class because you cannot make an object out of it.

So, if I try to do that: Superclass sc = new Superclass();, I get an error because it's abstract. I can't make an object from it. So, when you make a superclass, you should probably make it abstract for that reason. And the reason that you can then make abstract methods.

In the next couple of tutorials, I'm going to talk about different kinds of methods, but this is the first one I'll be talking about: an abstract method. To do that, um, abstract public void and then you name it print. And I'm gonna print out, actually, what am I talking about? An abstract method, you don't need a body.

There's no reason to have a body because here's what an abstract method is: it's a method that has to be overwritten. You have to override this method; you will get an error if you don't override this method. Your code won't run, so there's no point in making a body because I'm going to override it anyway.

I apologize, um, but that's what an abstract method is. You cannot have an abstract method in a concrete class. You can have concrete methods in an abstract class, but this abstract method has to be in an abstract class.

Okay, so, as you can see here, I'm getting errors. This is because I didn't override this print method. So, when I do this: public void, and I have to call it print, I have to call it print because I'm overriding it. I talked about overriding in the last tutorial, um, third, or sorry, second, because this is the second class.

And in here, I also have to make a print method. And the reason I'm explaining this to you is because you might get a little confused, um, when I start doing GUI stuff when you're implementing like listeners and stuff. There are a bunch of methods, and you have to use specific names for them.

Um, just like now we have to use a specific name for this because we have to call it print because it's an abstract method from that superclass we're inheriting. So, things you don't have to worry about that, but I'm just saying I'm teaching this because things will make a little more sense later.

And I'm going to print out third here, right? So that's the abstract method, and these overwrite it. Now if I call something like print, I'm getting an error because I have to override the print method. So, I have to call it print.

Now, here I'm just going to make some objects and superclass. I'll just call 01 because this is object 1 = new Second(); and I'm doing that because I can. Because it's a superclass, equals new Third(); all right?

And I'm just going to, um, create a polymorphic array. So, data type Superclass type array = {01, 02}; And now I'm just gonna call the print method from each one. So, this is to demonstrate everything we've learned.

That you can make a Second object of, you know, using the superclass of the data type, the polymorphic array. And, um, I'm going to be calling the print method from each class. So, for int i = 0; and I'm going to be doing that using the polymorphic array. So just to cover everything, study this a lot because it pretty much has everything in it.

i < array.length; i++ and now I'm just going to call array[i] in here because it's going to be either 0 or 1. This is going to be 0, this is going to be 1. Oh, oops, 02 should be in there, and then .print(); So I'm going to be calling the print method from each of those classes.

So I'm just going to run this, and there we have Second and Third, exactly what I wanted to do. So I'm just going to go over this one more time. If you already understand it, then you can just end the video now because I'm not going to be teaching anything more.

But basically, I ex, we made an abstract class and then put this abstract method in there. I didn't make a body because it has to be overwritten; it's going to be given a body here, so there is zero point in making a body. In fact, um, you might, yeah, you get an error if you try to make a body for an abstract method because, um, there's just no reason for it.

So, but all I did here is that I made an abstract method. I basically made a deal saying, "All right, I'm making this abstract method; it has to be overwritten in every class that extends from the superclass." Because every class that extends from the superclass inherits this abstract method, and an abstract method has to be overwritten, which is why I had to override it here.

And here I had to call it print because, um, when you override something, um, what's it called? In order to override something, you make a method of the same name so that when you call that name, it does this one instead of this one. That's just how overriding works. I went over that in the last tutorial.

So now that I have these two methods that overwrote this abstract method, here's what I did: since they extend from the superclass, Second and Third of the superclass type, so the data type superclass works to create a Second method, I mean object and a Third object, which I call 01 and 02.

I then made an array of objects of the superclass type, and the reason I can do this is the same reason I can do this. Then I looped through this array, and, um, so because array.length is, you know, two in there, so it's gonna be, it's gonna do one, I mean zero and one. This object is zero; this object is one.

And calling print from here calls this method, and calling print here calls this method. That's exactly what I did: I looped it so I called print from both, um, classes, the print method that, um, overwrote the abstract method in the superclass.

So, thank you for watching Mac Heads 101. Subscribe and goodbye!

More Articles

View All
How To Clean Up Space Junk
On October the fourth, 1957, the first satellite, Sputnik I, was launched into space. Although it burned up in the atmosphere three months later, many satellites launched since then have not, leaving us with a virtual junk yard orbiting the earth. Now, th…
Becoming Mr. Wonderful | Kevin O'Leary Tells it All
This is the place. Coming up was right here when she said, “You’re fired.” I didn’t even know what fired meant. How dragons are really made? I have never ever in my life worked for someone again and never will. Can’t believe I’m so emotional. [Music] Yo…
Harmonic Functions
Hello everyone. So, here I’d like to talk about harmonic functions. Now, harmonic functions are a very special kind of multivariable function, um, and they’re defined in terms of the Laplacian, which I’ve been talking about in the last few videos. So, th…
Help Khan Academy supercharge learning
Hi everyone, Sal Khan here from Khan Academy, which you probably know is a not-for-profit with a mission of providing a free, world-class education for anyone, anywhere. And not-for-profit means no one owns Khan Academy. We are a public charity. You own …
Kevin O'Leary REACTS To Graham Stephan's $10 MILLION DOLLAR Investment Portfolio
A lot of people don’t understand how debt can put you out of business if things go wrong. Imagine being in your 40s and being wiped out, having to go bankrupt. So, I want you to react to something. Sure. I have my entire portfolio—worth a little bit over…
YC Fireside: Surbhi Sarna and Tracy Young - Founder of TigerEye and PlanGrid
Hi Tracy, welcome, and welcome to everybody in the audience as well. Tracy, you are such a legend more broadly and a legend in the female founder community. Certainly, I can’t think of anyone better to kick off Women’s History Month with. Thanks for havi…