ALSO KNOWN AS RUNTIME POLYMORPHISM //************************************************* import java.util.Scanner; public class Main { public static void main(String[] args) {
//Dynamic Polymorphism
Scanner scanner = new Scanner(System.in); Animal animal;
System.out.println("What animal do you want?"); System.out.print("(1=dog) or (2=cat): "); int choice = scanner.nextInt();
if(choice==1) { animal = new Dog(); animal.speak(); } else if(choice==2) { animal = new Cat(); animal.speak(); } else { animal = new Animal(); System.out.println("That choice was invalid"); animal.speak(); } } } //************************************************* public class Animal { public void speak() { System.out.println("animal goes *brrrr*"); } } //************************************************* public class Dog extends Animal{ @Override public void speak() { System.out.println("dog goes *bark*"); } } //************************************************* public class Cat extends Animal{ @Override public void speak() { System.out.println("cat goes *meow*"); } } //*************************************************
Love this channel! I spent a whole week trying to understand polymorphism and it was really a nightmare since I couldn't get anything from the lecture at uni. Your video really save my life. Hope that you have patreons so that I could support :3
Thank you Bro Code. Your video helps me a lot to understand polymorphism! I've spent a few weeks catching those concepts before watching your videos. I should've found this early.❤
Hi Bro Code! okay, so I'm totally new to programming and been told to start with Java (something I'm still trying to understand why:)). Anyway, I do like your content but do have a question regarding this particular video. I guess I understand the concept and the logic behind this dynamic polymorphism thing but still having a hard time to understand exactly why and when I would use it. Here's the confusion in my head: Why can't I just use the following if/else statement to achieve the exact same goal: ------------------------------------------------------------------------------------------------------------------ if(choice==1) { Dog dog = new Dog(); dog.speak(); } else if(choice==2) { Cat cat = new Cat(); cat.speak(); } else { Animal animal = new Animal(); System.out.println("That choice was invalid"); animal.speak(); } ------------------------------------------------------------------------------------------------------------------ So, I instantiate a dog object or cat object when and if I need it at runtime just as in the way you have done it. Can you or someone reading this message and who knows the exact answer, please reply to me and let me know because it is driving me nuts trying to figure out. I have been Googling this thing for the past 2 days straight and have not been able to find a detailed explanation as to why someone would do it the Bro way when it can also be done my way. I'm sure there's a very good reason which is why it was implemented in the first place but WHAT IS THAT REASON? :)
The whole point of the example is to show how polymorphism works. So, the video shows animal variable being assigned first to a dog, then a cat and a general animal. If you remove the polymorphism by making Dog and Cat not extend Animal your code would compile and work as intended whereas the example in this video would not compile.
Good. But we should consider, if a Animal dog is treated as Animal, that is acceptable, but if that dog(which treated as Animal now) is treated as a cat, what exception will we get? I think we will learn that next.
wait i have to point something out real quick im pretty sure insted of using if or elseif statements couldnt of u just used a switch statement. great vid btw
I don't know if this is the correct answer, but here's what I think: if you're working on a very big project, the dog and cat objects will have many methods which are common, like eat(), sleep(), move(), and many more. And if you create more subclasses like Horses and Squirrels and others, their objects will have these methods too. So instead of writing these methods repeatedly in these classes, you could just create them in the Animal class and have these classes inherit the methods from the Animal class. I think that's why we need the Animal class (to keep the methods which are common to all the subclasses) alongside the subclasses (to keep the methods which are more specific to the subclasses). What I mean to say is, if you do Dog dog=new Dog(), you won't inherit the methods from the Animal class, which will force you to write the common methods all over again. But if you do Animal animal=new Dog(), you'll inherit all the common methods from the Animal class, as well as getting the methods specific to the Dog class. And yes, you can use a switch statement instead of an if block.
Hey bro, I got a question when watching the video, what's the difference when creating the object using Animal type compared to using the specific type of Dog? Appreciate it if someone could answer this.
If you learn Java, all the other languages seem much much easier. Cuz java is one of the languages with heavier syntax. So id recommend java for beginners. Hope that helped :)
It's personal preference. I learned more about computer science and programming with Java, but Python is more convenient to use since there's less syntax. If you begin with Java and switch to Python later, Python should be very very easy to learn.
1) Animal animal = new Dog/Cat();??? 2) Animal animal = new animal(); can someone explain this to me how the 1st statement is true as its taking the data type from different class while declaing another class object ?? i know my question make no sense jus try to humor me the only explanation i am think is the animal class is extend to both dog and cat so we can take the data from both the class
Yo just wanna say that GOD loved the world so much he sent his only begotten son Jesus to die a brutal death for us so that we can have eternal life and we can all accept this amazing gift this by simply believing in him (Jesus) asking for the forgiveness of your sins and forming a relationship with heavenly father.././...
ALSO KNOWN AS RUNTIME POLYMORPHISM
//*************************************************
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Dynamic Polymorphism
Scanner scanner = new Scanner(System.in);
Animal animal;
System.out.println("What animal do you want?");
System.out.print("(1=dog) or (2=cat): ");
int choice = scanner.nextInt();
if(choice==1) {
animal = new Dog();
animal.speak();
}
else if(choice==2) {
animal = new Cat();
animal.speak();
}
else {
animal = new Animal();
System.out.println("That choice was invalid");
animal.speak();
}
}
}
//*************************************************
public class Animal {
public void speak() {
System.out.println("animal goes *brrrr*");
}
}
//*************************************************
public class Dog extends Animal{
@Override
public void speak() {
System.out.println("dog goes *bark*");
}
}
//*************************************************
public class Cat extends Animal{
@Override
public void speak() {
System.out.println("cat goes *meow*");
}
}
//*************************************************
Dam u wrote a whole essay of codes in the comment section
Got it ...its easy thanks cool that you post code here...bro this an amazing channel...I dint know why its underrated
I cheated. I copied and pasted it from my IDE lol. Don't tell anybody
@@BroCodez Liar!!!!!!!
@@BroCodez it's ok, you still helped
Man I just looked at ur sub count and you have Grown!! Great job with the videos!
thanks Melt Down!
One of the most understandable lessons on RUclips. Thank you Bro!
Love this channel! I spent a whole week trying to understand polymorphism and it was really a nightmare since I couldn't get anything from the lecture at uni. Your video really save my life. Hope that you have patreons so that I could support :3
The example at the end was great. I think i got a good idea of how it works. Thanks a lot ❤
Great vid, I like the video game examples, keep them coming!
Thank you Bro Code. Your video helps me a lot to understand polymorphism! I've spent a few weeks catching those concepts before watching your videos. I should've found this early.❤
i liked that example with pokemon. that's what i like about this channel. examples are given of what certain methods are used for.
I watched a lot of RUclips Java tutorials, You are the best, and I hope you can create more practice Task questions and solutions videos
great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much
I played Pokémon when I was a kid
I played WoW when I was a teenager / young adult
I study Computer Science as an adult
I like this channel! :-)
Great, man. I like the way the example is given. To the point
You explained everything so well !! *subscribed*
Congrats, you beat my professors of my old university. My current one got the same zest as you. Good luck w the Algorithm boss
Thank you ,You explain it well
Amazing job...love the way you explain
thank you bro
Great! You made it very understandable! Good job!
Thank you very much!
Good explanation and examples
My ged u explain it clearly bro. Thank you
Thank you for free courses ❤
Good video.
BTW, can you make a small java game series or if not that then a video of making a cube in Java?
I'm going to be dedicating videos #90-100 in this playlist to games and projects mostly. I believe we are on #86 right now
*wow*! Thank you very very much!
But only one tutorial of making something 3D?
I actually suck at 3D graphics lol
I'll brush up on my skills and see what I can do
no promises tho
@@BroCodez thanks for your efforts
All these prayers go towards my next job
You are awesome man . Thanks a lot
Thank you bro! let's fight the algorithm :D you deserve it
Hi Bro Code! okay, so I'm totally new to programming and been told to start with Java (something I'm still trying to understand why:)). Anyway, I do like your content but do have a question regarding this particular video. I guess I understand the concept and the logic behind this dynamic polymorphism thing but still having a hard time to understand exactly why and when I would use it. Here's the confusion in my head: Why can't I just use the following if/else statement to achieve the exact same goal:
------------------------------------------------------------------------------------------------------------------
if(choice==1) {
Dog dog = new Dog();
dog.speak();
}
else if(choice==2) {
Cat cat = new Cat();
cat.speak();
}
else {
Animal animal = new Animal();
System.out.println("That choice was invalid");
animal.speak();
}
------------------------------------------------------------------------------------------------------------------
So, I instantiate a dog object or cat object when and if I need it at runtime just as in the way you have done it. Can you or someone reading this message and who knows the exact answer, please reply to me and let me know because it is driving me nuts trying to figure out. I have been Googling this thing for the past 2 days straight and have not been able to find a detailed explanation as to why someone would do it the Bro way when it can also be done my way. I'm sure there's a very good reason which is why it was implemented in the first place but WHAT IS THAT REASON? :)
I think depending on the need ,we can code as per your wish
The whole point of the example is to show how polymorphism works. So, the video shows animal variable being assigned first to a dog, then a cat and a general animal. If you remove the polymorphism by making Dog and Cat not extend Animal your code would compile and work as intended whereas the example in this video would not compile.
I really like this channel. Thanks Bro Code.
awesome videos for beginners
Thank you so much sir.
Please make videos for mobile app development.....
Nice content.
thanks for the video!
Thank You So Much
awesome
Nice Example Earned a sub :)
Yoo Man. Just got the idea from the video. Thanks.🙏
Like and subscribed, thank for saving my assignment grade
thank bro you literally saving my A$$ before exam
so amazing 😮
ty! Very good explained! :-)
great video. great teacher
Thanks, Bro! ☕ You're awesome!
Good
Thanks 👍🏾
Thanks, Bro!
# 1 best java teacher
as always, thanks
What will you do after Java?
Python and Javascript for a little while. almost there
thank you
Thanks
Bro explained Factory pattern, without saying it's Factory pattern 👌
Good. But we should consider, if a Animal dog is treated as Animal, that is acceptable, but if that dog(which treated as Animal now) is treated as a cat, what exception will we get? I think we will learn that next.
wait i have to point something out real quick im pretty sure insted of using if or elseif statements couldnt of u just used a switch statement. great vid btw
YOU ARE THE BEST
thanks
thanks!
Bros before codes! Good content!
nice video
🔥🔥🔥
hi Bro why we don't use "Dog dog=new Dog();" and can we use switch statement in a place of "if Statement". thank you bro love the way you explain
I don't know if this is the correct answer, but here's what I think: if you're working on a very big project, the dog and cat objects will have many methods which are common, like eat(), sleep(), move(), and many more. And if you create more subclasses like Horses and Squirrels and others, their objects will have these methods too. So instead of writing these methods repeatedly in these classes, you could just create them in the Animal class and have these classes inherit the methods from the Animal class. I think that's why we need the Animal class (to keep the methods which are common to all the subclasses) alongside the subclasses (to keep the methods which are more specific to the subclasses). What I mean to say is, if you do Dog dog=new Dog(), you won't inherit the methods from the Animal class, which will force you to write the common methods all over again. But if you do Animal animal=new Dog(), you'll inherit all the common methods from the Animal class, as well as getting the methods specific to the Dog class.
And yes, you can use a switch statement instead of an if block.
Great youtuber
nice video bro
Good video.
Impressive!
Hey bro, I got a question when watching the video, what's the difference when creating the object using Animal type compared to using the specific type of Dog? Appreciate it if someone could answer this.
La diferencia es que si usas Animal en lugar de Dog el texto que se muestra en la consola será "Animal goes *brrr* en lugar de "Dog goes *bark*!".
dope video
thanks bro
ty bro
Thankss bro :)
Cool I learn it
Do u think python is better or java
If you learn Java, all the other languages seem much much easier. Cuz java is one of the languages with heavier syntax. So id recommend java for beginners.
Hope that helped :)
@@meltdown6856 i started with python
It's personal preference. I learned more about computer science and programming with Java, but Python is more convenient to use since there's less syntax. If you begin with Java and switch to Python later, Python should be very very easy to learn.
where is the new videos?!
Love u Brother
aasam vro
Animals go BRUHHH
I subbed btw
what is dynamic polymorphism, is it overiding?
method overriding is an example of dynamic (runtime) polymorphism
1) Animal animal = new Dog/Cat();???
2) Animal animal = new animal();
can someone explain this to me how the 1st statement is true as its taking the data type from different class while declaing another class object ?? i know my question make no sense jus try to humor me
the only explanation i am think is the animal class is extend to both dog and cat so we can take the data from both the class
❤❤
Why not just create Cat someCat = new Cat() or Dog someDog = new Dog() inside that if-else?
long live bro
i am helping you right now
becuse im fuckin loved it
I'm Finding this familiar.
damn dude
and that's that easy :D
polywhirl
Am i the only one to still use notepad lmao
Download intellij
hey if it works for you, why not
k
111
comment
Bro
Yo just wanna say that GOD loved the world so much he sent his only begotten
son Jesus to die a brutal death for us so that we can have eternal life
and we can all accept this amazing gift this by simply believing in him (Jesus) asking for the forgiveness of your sins
and forming a relationship with heavenly father.././...
Thanks
thanks
Thanks 🌸
Thanks, Bro!
Free Palestine 🇵🇸