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

Java Lesson 14 | Polymorphism pt.1


3m read
·Nov 3, 2024

Hey, this is a Mac heads 101. Welcome to your 14th Java tutorial, and today I'll be talking about a little comp. It's a complicated topic, which is inheritance and polymorphism, and it's kind of hard to explain what it is, but you'll see as I'm doing all this stuff.

As you can see, I created two more classes. One's called SuperClass, and one's called Third, which is just our third class, and you'll see why I call the superclass in a second. So, um, first I'm just going to be showing what inheritance is.

Let's say in SuperClass I made a method public void print(), and here I just print it out "Hello World." Alright, now here's the important part of inheritance: there is a way I can call this method using an object from this class, even if the method's not in this class. This is inheritance.

I throw extends and then the name of the class, which is SuperClass. Extends is for classes, and implements is for interfaces. We're not going to be using implements right now, but later on you will see implements. So I'm telling you, implements means as an interface, extends is for a class.

If I right here extends SuperClass, alright, so here's the method. The print method is in SuperClass; these two methods, I mean classes, are blank. They have no methods, but they extend SuperClass. So, if in here I made a second object and I do s.print(), I'm not getting an error. This is blank; if there's no methods in here, but yet I'm calling a method from that class how?

Extend SuperClass, and the name for this is actually a SuperClass, which is why I called it SuperClass. The method is in here, but they extend SuperClass. The methods are in these classes; they're just not explicitly written in there. So, if I run this, "Hello World."

So now I'm going to talk about overriding. Now, so what overriding is, if I have a print method – if I have a method here with the same name – this is print. I'm going to make another public void print(), and I'm going to print out "Hello World from second class."

Ok, so now if I call the print method from the second class, Second s = new Second(); s.print(); this print method overrides the print method in a SuperClass, but this doesn't have a method to override it. So, if I made a third object and t.print(), this is going to print the method from this class because it's not been overwritten.

This is going to print this method because it overwrote the print method from the SuperClass. So, when I run this: "Hello World from second class," and "Hello World," which is on the method from the SuperClass, and "Hello World from second class" is the method that overwrote the method from the SuperClass.

Ok, so now I'm going to show you something really cool; it's called a polymorphic array. Ok, so let's say I had my object Second s = new Second() and Third t = new Third(). I can make an array, and this array is going to be of the type. The data type is going to be SuperClass because it's going to be an array of objects. So, SuperClass, and I'll call Array.

Tell y'all I've been working with an array equals, and then in here I'm going to put s and t. So now this is an array holding objects. It's holding these objects.

Now look at this: for int i = 0; i < array.length; i++ and I'm going to call, um, what's it called? array[i].print(); and what this is going to do, alright? So this one has an overridden method, and this one's just going to be using the method from the SuperClass.

I have an ArrayList of these objects, so when I call the print method from s, right? When I call a print method from s, it's going to print the overridden method, and when I call the print method from t, it's going to print the method from the SuperClass.

Now I made an array with these objects, I looped through the array, and so this is going to be equal to 0, so it's going to call s.print(), and then i equals 1, it's going to call t.print(). So it's going to be looping through and calling all these methods, and there we go: "Hello World from second class" and "Hello World."

And that is called a polymorphic array. Ok, and that's all for the first polymorphism tutorial. In the second tutorial, I'm going to be going a little more. I'm going to be talking about abstract classes and some probably something now. So, see you next time!

More Articles

View All
Sam Harris: The Self is an Illusion | Big Think
What one of the problems we have in discussing consciousness scientifically is that consciousness is irreducibly subjective. This is a point that many philosophers have made – Thomas Nagel, John Sorrell, David Chalmers. While I don’t agree with everything…
My response to Kevin O’Leary | Shark Tank
What’s up guys, it’s Graham here. So let me just start off by saying this: I’ve been watching the show “Shark Tank” back when it first came out in 2009. For anyone who’s not aware of the show, it’s basically where entrepreneurs can pitch their ideas to a …
Java GUI Lesson 12 | Moving Graphics with arrow keys
Hey, this is Matt heads 101. My name is Jake, and in this job, okay with you? I’m gonna be teaching you how to move a graphic around with the arrow keys. In this case, it’s just going to be a simple oval, which is what I’m going to be using for the next c…
Similar triangles & slope: proportion of segments | Grade 8 (TX) | Khan Academy
We’re told triangle PQR and triangle ABC are similar triangles. Which proportion shows that the slope of PR, right over here, equals the slope of AC? So pause this video and see if you can figure that on your own before we do this together. All right, w…
Adora Cheung Speaks at Female Founders Conference 2015
Hey everyone, thanks Cat. So as Cat said, I am the CEO and co-founder of Homejoy. Um, woo, yeah, okay, this is going to be easy. Um, so Homejoy is the get help button for every home, and Cat said we connect people with home service professionals in the mo…
Judicial activism and judicial restraint | US government and civics | Khan Academy
What we’re going to do in this video is talk about the terms judicial activism and judicial restraint. In many videos, we have talked about how the judicial branch, one of its main powers, is to be a check on the executive and legislative branch; that it …