Java Polymorphism Fully Explained In 7 Minutes
Вставка
- Опубліковано 15 жов 2024
- Complete Java course: codingwithjohn...
What does polymorphism mean in Java? So many classes and courses overcomplicate it, but polymorphism is really super simple. We'll talk about what polymorphism means in Java, and go over two different kinds.
Polymorphism is one of the 4 core concepts of Object-oriented programming languages. And it's such a scary sounding word, but it refers to a simple concept.
Learn or improve your Java by watching it being coded live!
Hey, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java developers.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletria...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com...
☕Complete Java course:
codingwithjohn...
codingwithjohn...
You kinda left out the main point of Polymorphism. The conclusion should be that you can store all the different instances (e.g. of Dog, Cat, ...) in Animal variables and it still does the proper thing! Usually you e.g. have a List which contains different animals, and you can just call the eat() function on each element without worrying what type it actually is.
This guy is way too underrated 🔥
Yes!
Boring stuff
@@MrSkinkarde why are you watching if you're not interested 😂
He was born to teach Java
@@MrSkinkarde You're Finnish, aren't you?
My Univ. professor couldn't explain it to me like this over a whole semester!
Now here I am understanding it in under 10min.
Thank you!
Perhaps you should have come here first
John has some very good videos, but this one is kinda disappointing, I'd say. It's not exactly polymorphism what John is explaining here. It's more like he's only describing inheritance. That said, I think you should have listened more carefully to your Professor instead.
"If you can't explain it simply, you don't understand it well enough." - Albert Einstein
maybe bc you love dogs and cats
@@michimarz What? Did you even watch the video?
Dude. You really just saved my entire life. I was having so much trouble understanding how to explain polymorphism and putting it into my own words. You explained it so perfectly in so many ways. Great content!
lol entire life? a little dramatic aren't we
@@mmq6525got somethin’ to prove now do we?
@@mmq6525 bros got gun to his head by boss as we watch
Subscribed!
with polymorphism also you can always assign a subclass object type to a super class reference type
like that
Animal cat = new Cat();
Animal dog = new Dog();
Animal a = new Animal();
Since
Animal cat = new Cat()
| |
| |
Reference type object type
Happy coding everyone.
Actually this is the correct explanation of polymorphism. The video content explains inheritence mechanics of OOP. Inherıtance provides child class type object act like parent class type object by inheritence. Polimorphism provides parent class type object can act like chıid class type object. Internal mechanics are very different.
So does the reference type or object type determine which method is used
@@elizaalwani9200 The object type
@@elizaalwani9200 The object type does.
@@elizaalwani9200 the object type does. BUT the availability of methods is determined by the reference type.
It's always nice to see you still got the basics down, even though I think I never EVER went and thought "I'm gonna use polymorphism on that sucker", but just went and did it.
2024 and this is still the best short explaination of polymorphism on UA-cam.
Maybe theyre wrong
Very good videos. You explain things in ways that eliminates confusion and covers all bases.
John is so concise in his teaching, my lecturer literally takes two hours to cover the exact same concepts
memento mori
@@eternalflames911 🖤🤍
I struggled with understanding polymorphism after reading the entire chapter twice, but I finally grasped the concept after watching this 7-minute video. Thank you so much for the help!
I always wonder why University teachers spend whole semester to explain the topic but you still don't understand, but here on you tube you watch 7 minutes video about the topic and fully understand what the heck it is all about .
Thank you John very much for your great content!
University teachers are not programmers. If you want to learn how to swim i think its best to learn from someone that already knows how to swim
John has some very good videos, but this one is kinda disappointing, I'd say. It's not exactly polymorphism what John is explaining here. It's more like he's only describing inheritance. That said, I think you should have listened more carefully to your Professor instead.
Surely your college teachers aren't spending an entire semester explaining one concept to you. Java [programming] doesn't come easily or quickly to everyone, maybe hit the books a bit harder or do what you're doing here: supplement your college lessons with YT videos.
I think what you have described here is simply inheritance & method overloading. Polymorphism is a bit more advanced than that. You could for example have declared the cat and dog objects as Animal. That would at least have introduced the concept of polymorphism.
What he explained could be called as polymorphism, specifically early-binding or compile-time polymorphism. What you are talking about is late binding or runtime polymorphism. Java can do both. But, yeah he did miss out on the runtime polymorphism.
You have no idea how I struggled to understand this, about 5 hours before exam i stumbled upon this video, thank you. It was super easy to understand, never thought someone could explain polymorphism this way
Thanks John! After hours of trying to find a clear explainaton you wrapped it up in 7 minutes, thanks again!
You have not fully understood a problem if you can not explain it to someone else.
This guy is an expert in his field making the explanation affordless
I don't know how to express my gratitude dude, you've saved me. I've been struggling really hard with my Java class and you have a gift for explaining in a way that scratches my brain the right way.
You are an amazing teacher, know that. I've been doing a lot of java courses and these topics are always convoluted and extremely difficult to understand, but you've managed to explain in a simple way. Thanks!
I really wish I had found you and your channel months ago. Even though you are speaking fast, I am grasping and retaining the concept to write into code.
Thank you so much!!!
excellent spent 2 hours watching diff videos to understand the concept but you explained way better and way easy from all of them
The best Java programming youtuber !! Wish I had known his channel back in 2021 beginning when I started the programming module in Uni.. Explains way better than many of my lecturers ! Thank You !!!
It was a nightmare to find a video to understand Polymorphism. Thanks a lot.
Hi John, thanks for your Java tutorials with non-monotone voice. If you can explain more about composition and aggregation in Java that would be great (your explanations are easier to digest compared to other youtube tutorials)!
straight to the point way of explaining this concept, most others just over-complicate it and are needlessly verbose.
That was such a simple explanation that was being made WAY too complicated in my class. Thank you for putting this stuff out there.
i usually never comment on videos, but this guy deserves it all. help me finally end my struggle with CSE 205.
I guess what was explained in the video is inheritance. As someone commented " Inherıtance provides child class type object act like parent class type object by inheritance. Polymorphism provides parent class type objects that can act like chıid class type objects. Internal mechanics are very different."
This was a fantastic explanation that I was able to wrap my head around, bravo good sir!
this guy explained a concept in 7 minutes so much better than my professor could in 2 hours. Unreal man. Such a lifesaver
I am currently studying for my finals exam in Germany and this is just perfectly explained. THANKS!
I'm in a Java course and wasn't understanding this topic. Now its perfectly clear. THANK YOU!
Great explanation. I've been trying to study up for an interview (has been years since I graduated and been in a different field so I forgot everything). When I was studying it wasn't clear that method overriding and method overloading were part of Polymorphism. They made it seem like they were totally different things so thank you for clearing it up!
Thanks for the video! You're helping a beginner coder in college out quite a bit :)
ay yo dont delete comments that actually explains what polymorphism is.
Long time im out of programming life but seeing this video kinda make me happy remembering some stuff. Also very good explanations!
The fact that this was explained so perfectly and clear and it just clicked so well. Amazing job done!
Hi. I am learning Java and I frequently drop here to get things straight. This video does a very good job of introducing polymorphism in a simple manner. But what I would also like to add, is spending some time to explain compile-time and run-time polymorphism. You can do one better by disclosing what polymorphism generally is(that is in the natural world) and how we can try to effect the same phenomenon to objects in Java (since objects in OOP languages mirror real-world objects). There are two ways to imbibe this phenomenon in an object in Java: at compile-time and run-time . The techniques of method overloading and method overriding then happened to be just a natural consequence of acheiving the desired effect. I don't know if I got it backward. I felt the emphasis should be on the phases in the lifecycle of a program.
Thank you for the clarification on overloading vs overriding.
I was going to quit my programming course and go back to working at Sonic, but then I came across your video and it just clicked. NO exaggeration, God bless you.
Polymorphism is overrider & overload Parent methods.
Thank you John
You've explained what this is and how to do it in half the time and made it 10x more interesting than my instructor. No insult to him. But he just made it sound way more complicated than it actually was.
It's because polymorphism is unfortunately more complicated than what he's saying here.
Words fall short to convey how helpful this lecture was sir.Thank you ❤❤
Explaining things better than youtubers with 500k+ Subscribers ...John you are doing gods work thank you so much
You're way of explaining is playful and very expressive. ❤️ it
excellent video. I am a CIS student this is now starting to all make sense.
Thank you! I'm so happy that I found your video. I definetely watch other content from you, since you explain this topic so clearly. This makes so much more sense even in English compared the videos of my native language in which I just don't understand this subject at all.
wow, i have asked and "re-asked" my professor to explain this to me. and i just wasnt getting it. ive learned more in the first 3 minutes of your video then what my prof. has been trying to explain to me in 4 weeks. Thanks so much.
Jonny Sins is the best at teaching programming
O that's great and I'm learning more on your channel! Thank you for this!
Love from the Philippines!
Just wanted to point that you have a good sense of humor… all this based on the Kramer picture in the back… legendary
My comp sci final is in two days and you are the only reason I'm not crying right now.
I just watched your video , I am little bit confused, you made it very easy to learn coding.
Hi,John...you made my life so easy..was struggling to understand the polymorphism concept until i found your video...much love from india
Thank you for helping me get through my first year as a graduate student!
He made it Simple. Thank you 😊
Amazing explanation, John! Can I overload a class constructor?
This guy knows the art of teaching, he explained all the concepts in such a smooth way, he makes learning fun & interesting 😇👍
0:38 - "morphism" actually originates from the greek word "morphi", which directly translates into the english word "form".
Cool! Got it right and didn't even know it 👍
I don't understand why nobody can explain Polymorphism in such a simple yet elegant way!
i bought your course in thinkific, you are awesome, but the Thinkific platform is streaming is so slow, and the player is buggy and keeps switching the video quality when i switch to full screen
John you might be one of the best tutors on UA-cam🙏🏼
Well done my guy. Great video
Wow!!! Extraordinary skills on explaining the concepts🎉
So fun with John...geez I have to get his training...makes Java fun.
very helpful. Understood polymorphism and encapsulation in less than 30 minutes.
Watching this video i exclaimed three times "Wow! this guy is great". Subscribed.
God bless any UA-camr including you John who teaches coding.
Wow this is just amazing.
Currently studying computer science and you help me so much!
I'm speechless for this great tutorial ever.... Earn my subscription....!!! Very simple to understand....!!!!
2-3 uni lectures in 7 minutes, ur amazing!
Not sure if you'll see this comment but here goes:
I been watching your video on Polymorphism and Inheritance. I understood everything until now where I saw syntax when declaring a new object such as this:
Beverage temp = new Coffee(); //Coffee is a subclass. There is an inheritance to this : class Coffee extends Beverage { etc,,}}
I always thought instead of reading Beverage, this must have the same name as the Constructor i.e it should say Coffee temp = new Coffee().
Does this work because Coffee is a subclass of Beverage? And with the code above, does this mean temp is now an instance of the Coffee class rather than Beverage class?
I do read every comment on all my videos (except possibly replies, cause the notifications are weird for those). I don't have the time to reply to all of them, but I do read them.
In short, you're correct. Coffee is a subclass of Beverage, which is often known as an "is a" relationship. Coffee "is a" Beverage, so Java allows that. You could even have a subclass of Coffee like "ClassicRoast", and you could do:
Beverage myBev = new ClassicRoast();
or
Coffee myCof = new ClassicRoast();
All ClassicRoasts are Coffees, and all Coffees are Beverages. But not all Beverages are Coffees, and not all Coffees are ClassicRoasts.
However there are some subtleties about how those objects are treated when you make them like that. One of my recent videos is all about upcasting and downcasting and it talks about this extensively. I think it will answer a lot of your questions:
ua-cam.com/video/HpuH7n9VOYk/v-deo.html
Great question, and thanks for watching!
I was really confused
but after watching this, I realized that I have done this in my program already but I don't know it's polymorphism .. thanks
Thank you so much. I was looking at a video at linkedin learning an I was about to quit learning java due to that video. You made it so simple and easy. Thank you from the bottom of heart.
Amazing explanation. I'm sure you already know but your teaching skills are helping thousands of students including myself. Thank you so much!
Yoo John you are incredibly brilliant at teaching Java.
Ur explanations are as clean as ur code John. Thank you
Thank you for explaining the topic so well..... I found your material way easier to digest than my java class! Doing God's work out there.
I look at topic in text book. I come here, I listen for 7 minutes. I save myself a headache. Come exam time, I just gotta chomp chomp and nom nom my way through that shit. Thank you so much, man.
Thank you for the videos. I have humble request for you to make a clear and understandable videos regarding polymorphism via inheritance and polymorphism via interface and passing an object as argument. Thank you once again for good video.
I learned the theory about 5 times but never had to use it because didn´t find a job as juniorDev. but this time I think I got it. THX John.
Awesome now I will never forget about method overriding and overloading
Thank you so much John! you have helped me so much with my internship! Shout-out from Australia
Man how did i only find this channel now great content so easy to understand concepts the way you explained in this video!!
My dude is awesome. Would be awesome to work under someone with his knowledge...and humor.
for beginners: Method Overloading is categorized under Compiletime polymorphism and Method Overriding is categorized under Runtime Polymorphism
You really are a great explainer, thank you! I also didn't get the concetp at uni
a living room and a highly knowledge guy. that's all it takes to teach people around the world
Just found you today and am so thankful I did! Thank you!!
How is this different from inheritance?
Im still very confused how to differentiate them
I have an important question: why do we override methods in java if we can create a new method in sub class with a different name??
Yo, can u make a video on explanation of Method Overriding in JAVA ,would really love to watch your explaination
Binge watching this man's videos. He's something else!
Thank you sir for making this concept easy to understand.
Heyy thank you for this video. Your pronunciation as well as explanation are really easy to understand for a Vietnamese student like me. Keep uploading more informative video please 🙏 Have a nice day ☺️☺️
Explaining what my comp sci professors couldn't explain during lectures...I do realize you have the benefit of hindsight and editing to make videos go a lot smoother than lectures, and there absolutely is value to lectures since we can actively ask questions and receive answers...But the way you structure your videos has been all but confusing thus far. Love this page.
man I'd love to watch spring videos you create
I'm struggling with the instantiation of the subclass. Well, it happens that the way I learned to do my instantiations is as it follows:
SubClass object = new SubClass();
But some people do it differently, and that is the way that it is represented in the official Java documentation about Polymorphism:
SuperClass object = new SubClass();
And I can't figure out why is this even a possibility, because when you instantiate objects with the second way, you can't access the SubClass methods (unless they are overrided methods and have the same name in both classes).
Could you explain this to me? Because I noticed that in the video you did the instantiation the same way as I do. Thanks in advance.
Great question! You can certainly do both, and using the superclass as the type has the limitations you noted.
Most of the time, you're just fine using the SubClass as the type, as you describe how you usually do it. However, there are situations where it makes sense to use the superclass, such as specifically when there is a polymorphic method like you noted.
Let's say you had an Instrument super class, and a bunch of sub classes of Instrument like Guitar, Trombone, Drum, etc.
Your Instrument class could have a play() method that makes a sound, and all subclasses would override that method with their own implementation.
You could write a method that looks like this:
void playSound (Instrument instrument){
System.out.println("Playing my sound: " + instrument.play());
}
This method doesn't know and doesn't care what type of instrument is passed in - it just needs it to be an Instrument, so that it knows it can call its play() method.
If you want to use that playSound() method in that kind of situation, you have to create your instrument like:
Instrument drum = new Drum();
playSound(drum);
When working with collections, you'll probably run into similar situations, just with Interfaces instead of superclasses.
For example, ArrayList is an implementation of the List interface. So usually when declaring an ArrayList, you should do this:
List names = new ArrayList();
This list happens to use an ArrayList as its underlying implementation, but any method that takes a List of Strings doesn't care whether you use an ArrayList, or LinkedList, or whatever. Then you can have a method like this:
void printNames (List nameList) {
...
}
Similar to the playSound() method above, this method doesn't care whether you used an ArrayList or some other kind of List implementation - it just needs to know it's some kind of List to do what it has to do. And the user of the method can use whatever type of List they want to use for their particular situation.
That's a very long answer, but hope it helps clarify it a bit!
@@CodingWithJohn I can't thank you enough for this!!!! It was really helpful. Thank youu
I am having the same question. Thank you for asking @Jorge and thank @John for the best explanation 😭😭💕
I just can't believe this. John actually helps people THIS WELL in the comments. I have no words. 👍👍👍
i think if you make a 2-3 hour video no one, and I mean no one will ever fail java! ever again Sir John.
Well, enjoyed all the munchs, chomps, and noms while learing something again in a new way. Was grateful this was in my feed/recommendations.
Very well explained... It will surely lead to Subscription++
It's crazy that I didn't know what the term polymorphism meant but I knew what is was if that makes sense. Like I understood the concept of overloading and overiding methods in child classes but the name was just daunting.