Java Polymorphism Tutorial – J031
DeegeU Java Course
The “Java Polymorphism Tutorial” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.
Transcript – Java Polymorphism Tutorial
Hi! DJ Spiess here. One of my favorite video games ever is Sinestar. The game requires you to fly around in space, mine crystals from asteroids to build bombs while fighting other workers and space tanks, and use these bombs to ultimately battle against the Sinestar. Think about how we would model this game with what we know so far.
We might have classes for the workers, tanks, asteroids, player ship, and Sinistar. Each one of these classes is a sprite that we draw on the game screen. They would all inherit from the game sprite class and all have a draw method. It would be nice to have one collection of sprites when we draw our game screen.
The problem we have is, we have several different classes all with the same behavior, draw(). An array can be only one type, so do we need 6 sprite arrays? That’s the problem we’re going to solve. In this lesson I will explain a new concept called polymorphism, and show you how we can treat different sub classes as the same super class.
Polymorphism is derived from the Greek words poly and morph. Poly meaning many, and morph meaning form. That’s it in a nutshell. Polymorphism means many forms. In Java this means an object can have different forms or types.
In our game, the sprite is our base class and every type of sprite is a subclass. We’ve seen this in previous lessons, and we’ve also hinted at what’s coming next. Now it would be nice if we could put all of the sprites in one sprite array. The advantage being that we could just run through each object in the array and draw it.
The polymorphic ability of a class means we can create a list of sprites like this , but we can fill it with the different subclasses. The key here is every class is a sprite. When we iterate over our list of sprites, Java treats each one as a sprite. The method called is the draw() method specific to the derived class.
Any class in Java that passes the “is-a” test is polymopophic. And that’s every class, because every class has Object as a superclass. We could create a list of Objects and hold any class, but we could only call methods available in the Object class. So our draw() method would not be available.
And that’s polymorphism. When you’re asked in an interview what are the 3 important concepts of object oriented programming, the answer is inheritance, encapsulation and polymorphism. We’ve now covered all three. It’s an important concept, so if you have any questions let me know in the comments. If you liked this video, make sure you like and share. This helps get the video in front of other viewers!
We’ll start diving deeper into more advanced class concepts in the next few lessons. New videos come out each week, so make sure you subscribe. You don’t want to miss a video! Thanks for watching, and I’ll see you in the next video!
All media created and owned by DJ Spiess unless listed below.
- No infringement intended
Get the code
The source code for “Java Polymorphism Tutorial” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:
git clone https://github.com/deege/deegeu-java-intro.git
Go to the Support > Getting the Code page for more help.
Don't miss another video!
New videos come out every week. Make sure you subscribe!
Your personal instructor
My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.