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
Conor Corey on teaching and motivating students during distance learning | Homeroom with Sal
Wait till we’re live. Stand by. Hi everyone, welcome to the daily homeroom live stream. Sorry, running a little bit late. As you can see, I am in a different location. I think I finally got the Wi-Fi fixed in the house, so we’re going to try this. You mig…
How to sell a $3,500,000 private jet.
We need something for short distance: half million, 1,500 naal miles. I’m looking to improve the quality of the place. Now, I understand you’re working with a bigger corporate jet, but it’s my first one. No, no, I understand there’s nothing wrong with th…
What Founder Mode Really Means
You got to figure out your technique for cutting through the bureaucracy you’ve built. Yes, to figure out what’s going on. I think the really encouraging thing from Brian’s talk is that it doesn’t matter how big your company is and how big your bureaucrac…
When and why extraneous solution happen
In other videos, we’ve already introduced the idea of an extraneous solution, where you go about solving an equation. You’re given an original equation, and you do a bunch of algebraic steps. Then you solve it and you get some solutions. What we’ve seen, …
It’s Over: China Just Broke The US Dollar
What’s up guys, it’s Graham here. So it’s official: China and Brazil have just struck a deal to ditch the US dollar. Whoops! Okay, before everyone freaks out, don’t worry, it’s a fake build for dramatic effect, but the point still remains. The world’s sec…
Welcome to the Body Farm | Explorer
[music playing] FRANCESCA FIORENTINI (VOICEOVER): That’s how I ended up in a body farm, the biggest one in the country. The Forensic Anthropology Research Center in South Texas studies how bodies decompose, and why. Their research helps law enforcement o…