Java Polymorphism Tutorial – 031

///Java Polymorphism Tutorial – 031

Java Polymorphism Tutorial – 031

FavoriteLoadingAdd to favorites

In the Java polymorphism tutorial video, we look at a new concept called polymorphism, and how we can treat different sub classes as the same super class.


Transcript :
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!

This video is part of a larger free online class called Free Java Course Online. You can find more information about this class on

Concepts: Java, Programming, polymorphism, object oriented programming

Social Links: Don’t hesitate to contact me if you have any further questions.

About Me:

Related Videos:

Free Java Course Online

If You Watch 1 Playlist About Java Classes Watch this One!

Media credits: All images are owned by DJ Spiess unless listed below

Sinister owned by Williams Electronics

Background image (dancing-156041.png) from

Killing Time by Kevin MacLeod is licensed under a Creative Commons Attribution license (

By |2017-06-17T02:17:58+00:00June 17th, 2017|Java Video Tutorials|12 Comments


  1. 20SecondsWithMC June 17, 2017 at 2:18 am - Reply

    I feel like I get smarter each time I watch your videos. Now I need to start at the beginning so I know what you are talking about. 😀

  2. suyash gupta June 17, 2017 at 2:21 am - Reply

    Brilliant explanation, particularly like the way you tend to use real-life examples. These videos are much better than most paid videos.

  3. sophi a June 17, 2017 at 2:22 am - Reply

    Thank you so much! finally understand why I need a polymorphism.

  4. Lol bruh June 17, 2017 at 2:28 am - Reply

    How the HELL did we make an instance of the sprite class isn’t it abstract? When I say instance I refer to the sprite array.

  5. nazirahmad saidzada June 17, 2017 at 2:29 am - Reply

    I’ve spend two weeks reading all the java books and watching tutorials online to get the idea of what polymorphism is and how it works but I couldn’t understand it and now I think I’ve learned everything I needed in less than three minutes. 😉 Thanks dude and hope u continue sharing the knowledge and lastly I was wondering If u could do some real world problems using polymorphism in eclipse ot netbeans would be very nice of you. again tnx a bundle.

  6. 20SecondsWithMC June 17, 2017 at 2:29 am - Reply

    BTW – I ♥ "Embrace Your Inner Nerd." It caught my eye.

  7. PABLO CASVAR June 17, 2017 at 2:29 am - Reply

    This is the first time I really understand what polymorphism is… Thanks a lot… great tutorial.

  8. Shehryar Aziz June 17, 2017 at 2:49 am - Reply

    Sir, it was a great video.

  9. mohamed elghannay June 17, 2017 at 2:50 am - Reply

    thanks a lot sir for clyrifiying the concepts you are doing an awsome job in simlifiying the java thing . your channel should be promoted

  10. Rongzmee June 17, 2017 at 3:05 am - Reply

    WOW this is the coolest polymorphism explanation video ever!!! You are exactly how a cool programmer is supposed to be like 😀 Wish this video gets more views. It certainly deserve!!!

  11. Junaid Tanoli June 17, 2017 at 3:15 am - Reply

    where can i find this series inheritance video

  12. Сеймур Айвазов June 17, 2017 at 3:18 am - Reply

    Awesome tutorial! Definitely like!

Leave A Comment