• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphic arguments  RSS feed

 
Greenhorn
Posts: 13
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BOOK : Head First Java, 2nd ed.
TOPIC : Inheritance and Polymorphism
SUB - TOPIC : Polymorphic Arguments And Return Types.
PAGE NO. : 187





I'm not able to grasp the advantage of polymorphic argument here, We can also make this happen just by doing this:


d.makeNoise();
h.makeNoise();



instead of first calling giveShot method and then calling makeNoise() method for each sub-class.

I know I'm missing something here. Please help me by giving a suitable example so that I can understand this topic better.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what if giveShot() does a lot more than just making an animal noisy? Let's say it was a method of 20 lines. Would you still want to write that code for every animal in the start() method?
 
hari shankar bhatt
Greenhorn
Posts: 13
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, what if giveShot() does a lot more than just making an animal noisy? Let's say it was a method of 20 lines. Would you still want to write that code for every animal in the start() method?



Even if the method is of 20 lines, since it can take any subtype of type specified in arguments, the code will be same for every subtype.
Similarly we can emulated this code by taking a normal method and calling it invidividually, as the code is same for each subtype. Please correct me if I am wrong..
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hari shankar bhatt wrote: I'm not able to grasp the advantage of polymorphic argument here, We can also make this happen just by doing this:


d.makeNoise();
h.makeNoise();


You could, but notice that the subtopic is SUB - TOPIC : Polymorphic Arguments And Return Types.. If the giveShot method wasn't there, there would be no demonstration of polymorphic arguments. The author isn't saying this is the only or best way to write the code, (s)he's just using it as an example of the topic.

 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not able to grasp the advantage of polymorphic argument here


Think about it a different way. When the giveShot method is called on a Vet instance, it is likely that some logic will need to be executed regardless of the type of Animal being given the shot.
 
hari shankar bhatt
Greenhorn
Posts: 13
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:
I'm not able to grasp the advantage of polymorphic argument here


Think about it a different way. When the giveShot method is called on a Vet instance, it is likely that some logic will need to be executed regardless of the type of Animal being given the shot.



Please can you elaborate it a little, I didnt get you. It will be more helpfull if you can give an example.. thanks
 
Marshal
Posts: 56600
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to forget that you are giving a shot to a Cat object, or a Dog object. Just remember you are giving it to an Animal object.

Let’s have some real‑life examples of shots.
  • A long time ago I used to give shots of morphine (opium).
  • 20mg morphine to most people meant they didn’t feel the pain of the operation. 50mg puts you to sleep for hours.
  • 20mg morphine given to a heroin addict did nothing; he might need 400mg to stop the pain. That would kill you and me if given all at once.
  • If I had ever given a cat morphine, it would have had a different effect. The cat would have run round the ceiling insanely for the next 24 hours.
  • So you see, you can have an inheritance tree like this
    You have a receiveMorphine methodAny mammal can receive morphine, provided it has a skin you can get the needle through. I would not wish to try it on a rhinoceros

    Now polymorphism reflects real‑life behaviour. Your Human method might look like thisAnd the Cat method like thisForget that you are giving the shot to different kinds of animal. Remember that you are giving it to an animal, and different animals react differently. That is what polymorphism represents.
     
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Something that beginners often fail to realize is that once you get out of school, almost nobody ever writes every single line of code. You will often have teams withing your company writing different pieces. Further, you may even buy code from a third party vendor.

    So, your task is to write the class Vet. You know you need to give any creature that comes in a shot. You could do this:


    You figure life is good. You submit your code. You release it to millions of customers all around the world...Then your boss says "Hey, the Vet Class needs to handle a Bird.".
    You have to go in, write a new method for Birds, then deploy that to all your customers. Ten minutes after you finish that, the boss comes in and says "We can make a lot of money if we have the vet class handle Snakes!!!" Guess what?

    Now...if you wrote it this way:



    When your boss says to add a Bird, you think "Well...a Bird is an animal, and my class can handle an animal". You tell your boss you don't need to do a thing and it will work. Then when he wants to add the Snake class, again, you don't have to do anything. Your code will work for ANY animal.
     
    hari shankar bhatt
    Greenhorn
    Posts: 13
    Android C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:You need to forget that you are giving a shot to a Cat object, or a Dog object. Just remember you are giving it to an Animal object.

    .
    .
    .
    .
    .
    .
    Forget that you are giving the shot to different kinds of animal. Remember that you are giving it to an animal, and different animals react differently. That is what polymorphism represents.





    Thanks a lot for taking your time and making this complicate thing quite simpler. Life is so good now. ;)

    You are really are a good teacher. I am happy that you are replied, I was actually waiting for you to come and explain this...
     
    hari shankar bhatt
    Greenhorn
    Posts: 13
    Android C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote:Something that beginners often fail to realize is that once you get out of school, almost nobody ever writes every single line of code. You will often have teams withing your company writing different pieces. Further, you may even buy code from a third party vendor
    .
    .
    .
    .
    .

    When your boss says to add a Bird, you think "Well...a Bird is an animal, and my class can handle an animal". You tell your boss you don't need to do a thing and it will work. Then when he wants to add the Snake class, again, you don't have to do anything. Your code will work for ANY animal.


    Exactly what I needed, A perfect explanantion with a perfect example. Thank you for giving such a nice insight, So here comes the advantage of using polymorphism..
     
    hari shankar bhatt
    Greenhorn
    Posts: 13
    Android C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, Is there a way I can check which subtype's object has been passed to giveShot method??? So that I can do different things based on different type ???
     
    Ranch Hand
    Posts: 375
    1
    Java Python Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hari shankar bhatt wrote:Also, Is there a way I can check which subtype's object has been passed to giveShot method??? So that I can do different things based on different type ???


    Don't worry.. you are not alone...

    You have got "instance of" operator to help you out of this...
     
    hari shankar bhatt
    Greenhorn
    Posts: 13
    Android C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks... :-)

    Javaranch is such a nice community... I can have my mind free of doubts ... :-)
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    R. Jain wrote:
    hari shankar bhatt wrote:Also, Is there a way I can check which subtype's object has been passed to giveShot method??? So that I can do different things based on different type ???


    Don't worry.. you are not alone...

    You have got "instance of" operator to help you out of this...

    You can, but that would effectively destroy the advantages you gain. You are now back to having to edit your code each time you want to add a new animal type. When the boss says to add the Bird, you have to go in and write whatever special thing you need to do for the Bird, then go back again when you add the Snake.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Java Python Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote:
    R. Jain wrote:
    hari shankar bhatt wrote:Also, Is there a way I can check which subtype's object has been passed to giveShot method??? So that I can do different things based on different type ???


    Don't worry.. you are not alone...

    You have got "instance of" operator to help you out of this...

    You can, but that would effectively destroy the advantages you gain. You are now back to having to edit your code each time you want to add a new animal type. When the boss says to add the Bird, you have to go in and write whatever special thing you need to do for the Bird, then go back again when you add the Snake.


    And Here comes the design pattern..... In this case, AbstractFactory, if I am not wrong...
    It allows you to add new stuff to your existing program without having to change the existing code...

    Update: - It's FACTORY Method rather...
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it might be both, abstract factory and abstract method, but it is a very long time since I read about those patterns.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Java Python Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I think it might be both, abstract factory and abstract method, but it is a very long time since I read about those patterns.


    Actually, Abstract Factory pattern internally uses Factory Methods only... If we use it, then we have to do minimal changes when we add extra functionality...
    Of course, a little change will be there....

     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!