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
Beach Bodies (in spoken word) - David Fasanya and Gabriel Barralaga
Transcriber: Andrea McDonough Reviewer: Bedirhan Cinar I don’t know about you, but I’m trying to get this beach body, that P90X, Brad Pitt, Bradley Cooper, Tyrese, Trey Songz, Matthew McConaughey beach body! I’m trying to sweat in front of everybody. Wor…
How To Invest $500 Per Month
What’s up, you guys? It’s great here! So, I realized in many of my videos I talked about investing ridiculous sums of money or putting 20% down to buy real estate, which could work out to be like six figures in cash. But I wanted to take a different appro…
Warren Buffett: Stop Listening to Economic Predictions
Given how crazy the economy, the stock market, and even the world has been over the past few months, there is a scary word that is appearing more and more often in headlines and in the news. This word is scary enough for some investors that even just the …
Dave Rubin and Jordan Peterson live in Phoenix (12 Rules tour)
It’s on me. There we go. All right, we’re live. We’re live! Holy cow! Hello YouTube! We are live in Phoenix. We are at Comerica Theater. I’ve got this Jordan Peterson guy. I’m Simon. I’m somehow hosting on his channel, but that doesn’t seem right. We’re a…
Are You Lightest In The Morning?
[Applause] So recently a friend of mine says to me, “Derek, you know you’re heaviest at night before you go to bed and lightest in the morning when you wake up.” Okay, but that doesn’t really seem to make sense. “Of course it does. Overnight, you’re not…
Everything You Need to Know About Planet Earth
Planet Earth is the home of every lifeform known to us in the universe. Its age is about 1⁄3 of the age of the universe, and admitted, it is a thing of beauty. A slightly squashed sphere with a heavy metal core and a lighter surface crust, wrapped in a th…