Java Lesson 14 | Polymorphism pt.1
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!