Java dynamic polymorphism ✨

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Java dynamic runtime polymorphism tutorial explained
    #java #dynamic #polymorphism #runtime
    //*************************************************
    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*");
    }
    }
    //*************************************************
  • Věda a technologie

Komentáře • 126

  • @BroCodez
    @BroCodez  Před 4 lety +45

    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*");
    }
    }
    //*************************************************

    • @noisyguest5249
      @noisyguest5249 Před 4 lety

      Dam u wrote a whole essay of codes in the comment section

    • @AHSANALI-ev7sx
      @AHSANALI-ev7sx Před 4 lety +1

      Got it ...its easy thanks cool that you post code here...bro this an amazing channel...I dint know why its underrated

    • @BroCodez
      @BroCodez  Před 4 lety +7

      I cheated. I copied and pasted it from my IDE lol. Don't tell anybody

    • @noisyguest5249
      @noisyguest5249 Před 4 lety +1

      @@BroCodez Liar!!!!!!!

    • @noah77
      @noah77 Před 4 lety

      @@BroCodez it's ok, you still helped

  • @nikitamarkovski7121
    @nikitamarkovski7121 Před 2 lety +8

    One of the most understandable lessons on CZcams. Thank you Bro!

  • @LamNguyen-nm1fq
    @LamNguyen-nm1fq Před 2 lety +5

    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

  • @kemann3815
    @kemann3815 Před 2 lety +4

    The example at the end was great. I think i got a good idea of how it works. Thanks a lot ❤

  • @cdjosh3568
    @cdjosh3568 Před 3 lety +4

    Great vid, I like the video game examples, keep them coming!

  • @meltdown6856
    @meltdown6856 Před 4 lety +19

    Man I just looked at ur sub count and you have Grown!! Great job with the videos!

  • @MarcusGrey
    @MarcusGrey Před 3 lety +2

    You explained everything so well !! *subscribed*

  • @yonatan_song
    @yonatan_song Před 3 lety +1

    Great, man. I like the way the example is given. To the point

  • @joviksalmeron9664
    @joviksalmeron9664 Před 3 lety +2

    Great! You made it very understandable! Good job!

  • @AHSANALI-ev7sx
    @AHSANALI-ev7sx Před 4 lety +1

    Amazing job...love the way you explain

  • @kedea159
    @kedea159 Před 3 lety

    Thank you bro! let's fight the algorithm :D you deserve it

  • @omersond4891
    @omersond4891 Před 3 lety +1

    i liked that example with pokemon. that's what i like about this channel. examples are given of what certain methods are used for.

  • @sunshine1538
    @sunshine1538 Před měsícem

    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.❤

  • @Dinesh0502
    @Dinesh0502 Před 2 lety

    Nice Example Earned a sub :)

  • @muhammedenesgokdeniz6201

    You are awesome man . Thanks a lot

  • @iztillanurulla151
    @iztillanurulla151 Před 7 měsíci

    I watched a lot of CZcams Java tutorials, You are the best, and I hope you can create more practice Task questions and solutions videos

  • @kippenvogelchen9717
    @kippenvogelchen9717 Před 3 lety

    ty! Very good explained! :-)

  • @azizchahbi3538
    @azizchahbi3538 Před rokem

    Good explanation and examples

  • @danny.3036
    @danny.3036 Před 3 lety

    Thanks, Bro! ☕ You're awesome!

  • @jamesarden7554
    @jamesarden7554 Před 3 lety

    great video. great teacher

  • @khaledelsayed3507
    @khaledelsayed3507 Před rokem

    Thank you for free courses ❤

  • @renecabuhan1675
    @renecabuhan1675 Před 3 lety

    My ged u explain it clearly bro. Thank you

  • @hgatl
    @hgatl Před 3 lety

    awesome videos for beginners

  • @SereneEverhart
    @SereneEverhart Před 2 lety

    # 1 best java teacher

  • @Wai-uc7tg
    @Wai-uc7tg Před rokem

    I really like this channel. Thanks Bro Code.

  • @angelcastineira2561
    @angelcastineira2561 Před 4 lety

    thanks for the video!

  • @user-zq6yx7of5f
    @user-zq6yx7of5f Před rokem

    Thank you very much!

  • @noisyguest5249
    @noisyguest5249 Před 4 lety +1

    I subbed btw

  • @camerald8257
    @camerald8257 Před 2 lety

    so amazing 😮

  • @vpenywise
    @vpenywise Před 2 lety

    Bros before codes! Good content!

  • @meggieelo
    @meggieelo Před 2 lety

    YOU ARE THE BEST

  • @vindydog7664
    @vindydog7664 Před 2 lety

    Great youtuber

  • @rafikcherfa478
    @rafikcherfa478 Před 2 měsíci +1

    thank you bro

  • @minhtetpaing1695
    @minhtetpaing1695 Před 3 lety

    Thank you so much sir.

  • @mackensonreginaldmichel399

    Nice content.

  • @TheEvertonDias
    @TheEvertonDias Před rokem

    Thanks, Bro!

  • @suryaa5564
    @suryaa5564 Před 3 lety

    Thank You So Much

  • @bossowa315
    @bossowa315 Před rokem

    Yoo Man. Just got the idea from the video. Thanks.🙏

  • @uritaabdelmasih292
    @uritaabdelmasih292 Před rokem

    Impressive!

  • @izaeljunior9007
    @izaeljunior9007 Před rokem

    nice video bro

  • @alikkk9169
    @alikkk9169 Před 2 lety

    nice video

  • @baubaudinamo
    @baubaudinamo Před 3 lety

    Good video.

  • @TheSoprah
    @TheSoprah Před 3 lety +7

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

  • @speklex779
    @speklex779 Před 11 měsíci

    dope video

  • @augischadiegils.5109
    @augischadiegils.5109 Před 3 lety

    Thankss bro :)

  • @thedarkwhisperingcloud
    @thedarkwhisperingcloud Před 7 měsíci

    Thanks 👍🏾

  • @akoldousamuel8484
    @akoldousamuel8484 Před rokem

    Good

  • @majid1885
    @majid1885 Před 2 lety

    awesome

  • @danielmilewski7659
    @danielmilewski7659 Před rokem

    thanks!

  • @Simis999
    @Simis999 Před rokem

    All these prayers go towards my next job

  • @eugenezuev7349
    @eugenezuev7349 Před 2 měsíci

    as always, thanks

  • @nehalayaaz9406
    @nehalayaaz9406 Před 4 lety

    Thanks 🌸

  • @mathumathusan154
    @mathumathusan154 Před 2 lety

    aasam vro

  • @noah77
    @noah77 Před 4 lety +5

    Good video.
    BTW, can you make a small java game series or if not that then a video of making a cube in Java?

    • @BroCodez
      @BroCodez  Před 4 lety +7

      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

    • @noah77
      @noah77 Před 4 lety +3

      *wow*! Thank you very very much!
      But only one tutorial of making something 3D?

    • @BroCodez
      @BroCodez  Před 4 lety +5

      I actually suck at 3D graphics lol
      I'll brush up on my skills and see what I can do
      no promises tho

    • @noah77
      @noah77 Před 4 lety +1

      @@BroCodez thanks for your efforts

  • @wallstreetbets7741
    @wallstreetbets7741 Před 2 lety

    Cool I learn it

  • @rashmikakelum8803
    @rashmikakelum8803 Před rokem

    thank you

  • @pouriahich4215
    @pouriahich4215 Před 2 lety

    🔥🔥🔥

  • @mudassirnaz1813
    @mudassirnaz1813 Před 2 lety

    Thanks

  • @ahmadsan2907
    @ahmadsan2907 Před 3 lety

    thank bro you literally saving my A$$ before exam

  • @PoorwayTraning
    @PoorwayTraning Před rokem

    thanks

  • @rakhalsubbarao8375
    @rakhalsubbarao8375 Před 3 měsíci

    Love u Brother

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 Před 2 lety

    thanks bro

  • @alaric-bs3348
    @alaric-bs3348 Před 8 měsíci

    Like and subscribed, thank for saving my assignment grade

  • @theeverleinhour9616
    @theeverleinhour9616 Před rokem

    ty bro

  • @praisethesun1
    @praisethesun1 Před 2 lety

    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

  • @kaushalprasadyadav9242

    Please make videos for mobile app development.....

  • @eng6070
    @eng6070 Před 2 lety +1

    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.

    • @marcosa.6886
      @marcosa.6886 Před 2 lety

      ‎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*!".‎

  • @bentameurmohammedayoub1464

    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

    • @agnivodas
      @agnivodas Před rokem +1

      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.

  • @hannibalbianchi1466
    @hannibalbianchi1466 Před 3 lety

    ❤❤

  • @armandso1
    @armandso1 Před 3 lety +2

    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? :)

    • @robinshere8697
      @robinshere8697 Před 2 lety

      I think depending on the need ,we can code as per your wish

    • @elzacontiero2770
      @elzacontiero2770 Před 2 lety +1

      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.

  • @chakmak
    @chakmak Před 3 lety

    where is the new videos?!

  • @equaliser19
    @equaliser19 Před 3 lety

    Animals go BRUHHH

  • @mustafaercan686
    @mustafaercan686 Před 5 měsíci

    Bro explained Factory pattern, without saying it's Factory pattern 👌

  • @mariorossi1456
    @mariorossi1456 Před 4 lety +2

    What will you do after Java?

    • @BroCodez
      @BroCodez  Před 4 lety +1

      Python and Javascript for a little while. almost there

  • @be432n3
    @be432n3 Před 2 lety

    long live bro

  • @Mohammed-tx1ok
    @Mohammed-tx1ok Před 4 lety +1

    what is dynamic polymorphism, is it overiding?

    • @BroCodez
      @BroCodez  Před 4 lety

      method overriding is an example of dynamic (runtime) polymorphism

  • @girl6994
    @girl6994 Před 4 lety

    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.

  • @lamias7712
    @lamias7712 Před 2 lety

    and that's that easy :D

  • @fatihbasal6085
    @fatihbasal6085 Před rokem

    i am helping you right now

  • @shalajko
    @shalajko Před 3 lety

    I'm Finding this familiar.

  • @alexyakoveno4682
    @alexyakoveno4682 Před rokem

  • @neuzen
    @neuzen Před rokem

    Why not just create Cat someCat = new Cat() or Dog someDog = new Dog() inside that if-else?

  • @noisyguest5249
    @noisyguest5249 Před 4 lety +2

    Do u think python is better or java

    • @meltdown6856
      @meltdown6856 Před 4 lety +4

      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 :)

    • @noisyguest5249
      @noisyguest5249 Před 4 lety +1

      @@meltdown6856 i started with python

    • @BroCodez
      @BroCodez  Před 4 lety +1

      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.

  • @greeneggsandmushrooms9855

    polywhirl

  • @anjapietralla5767
    @anjapietralla5767 Před 6 měsíci

  • @UltraSolarGod
    @UltraSolarGod Před 2 lety

    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

  • @noisyguest5249
    @noisyguest5249 Před 4 lety +1

    Am i the only one to still use notepad lmao

    • @noah77
      @noah77 Před 4 lety +1

      Download intellij

    • @BroCodez
      @BroCodez  Před 4 lety +1

      hey if it works for you, why not

  • @ashishthakur9983
    @ashishthakur9983 Před rokem

    111

  • @maciejkaminski8535
    @maciejkaminski8535 Před 3 lety

    comment

  • @kianfrawley8364
    @kianfrawley8364 Před 2 lety

    k

  • @ordanino9354
    @ordanino9354 Před rokem

    Bro

  • @zstar8397
    @zstar8397 Před rokem

    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.././...

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Před 2 lety

    Thanks

  • @mudassirnaz1813
    @mudassirnaz1813 Před 2 lety

    thanks

  • @romanrubanovich9273
    @romanrubanovich9273 Před 3 lety +1

    Thanks, Bro!

    • @WajdaanM
      @WajdaanM Před 7 měsíci

      Free Palestine 🇵🇸

  • @aboyiphilippa1136
    @aboyiphilippa1136 Před 3 měsíci

  • @kamandshayegan4824
    @kamandshayegan4824 Před 2 lety

    Thanks

  • @xxg3fallxx589
    @xxg3fallxx589 Před měsícem

    thanks