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
Cameras Reveal the Secret Lives of a Mountain Lion Family | Short Film Showcase
Mountain lion, puma, cougar— all names for an animal that has long been misunderstood, feared, hunted, and eliminated from most of its range. The cougar is often believed to be solitary and even heartless, but recently, deep in the Wyoming Wind River Rang…
4 Ways To Deal With 'Toxic People'
Today, I shall be meeting with interference, ingratitude, insolence, disloyalty, ill-will, and selfishness, all of them due to the offenders ignorant of what is good or evil. We all know someone in our lives that’s so exhausting to be around. There’s alwa…
15 Signs Of True Success
Plenty of people pretend to be successful for social clout. Pretending to be successful has become a sort of international sport. But there are some signs that you can tell if someone is actually successful or not. In this video, we’re going over 15 signs…
Free Markets Provide the Best Feedback
Mark Andreessen summarizes this nicely as “strong opinions loosely held.” So, as a society, if you’re truth-seeking, you want to have strong opinions but very loosely held. You want to try them, see if they work, and then error-correct if they don’t. But…
The History of the Bible, Animated | National Geographic
Along the shores of the Dead Sea, three Bedouin shepherds were tending their flocks when one of their goats strayed from the herd. Thinking it had wandered into a cave, one of them threw a stone to scare the animal out. The unexpected sound sparked their …
Understanding scatterplots | Representing data | Grade 5 (TX TEKS) | Khan Academy
We’re told the table below shows the ages of six people and the number of pets they own. So, this row is age of people, and then the second row is the number of pets. So the person who’s nine years old owned four pets. The person who’s eight years old ow…