Java Polymorphism Tutorial - 031

preview_player
Показать описание
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!

Concepts: Java, Programming, polymorphism, object oriented programming

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

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

Music:
Рекомендации по теме
Комментарии
Автор

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

rong
Автор

Have an interview this afternoon and wanted to brush up on terminology, these videos are fantastic!

davidadams
Автор

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

suyashgupta
Автор

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

pablocasvar
Автор

Finally, I found a real-world example! I've had enough to find only examples with Animals. Maybe 90% are like that. Thank you! Short and to the point.

razvan
Автор

The best explanation ever! Thank you!!!

williambluish
Автор

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.

freshbreeze
Автор

how come at 1:50 you have 2 instances with the index of 2? i'm new to this so just trying to figure it out.

rjmurphyo
Автор

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

TheGazanews
Автор

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. :D

SecondsWithMCOfficial
Автор

All the java concept videos have helped me tremendously and for that id like to that you sir.

ola
Автор

Wow
now I know how it helps in case of OOP
Great explanation!

Ali-miup
Автор

Thank you, nailed it in sub 3 mins. :) Keep on.

AlexTzaran
Автор

I would like to see a variety of examples how polymorphism is used in practice in coding basic Java. Had to view other videos get a better sense as this video is more theoretical.

chinatownbus
Автор

where can i find this series inheritance video

junaidtanoli
Автор

However, if there's no overridden method then you need to cast to go down in the hierarchy

Ali-miup
Автор

Now i'm ready to any interview... hahahaha

Master_Trip_BR
Автор

i think u have a little typo at @1:50, last array element should be array[4] .
very good video! thanks for ur job

andymnc
Автор

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

SecondsWithMCOfficial
Автор

FIX THE SPLASH SCREEN VOLUME AT 0:01 - OMFG

brooks