Results 1 to 11 of 11
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Polymorphism - do you use it?

    Hi,
    I have gone through the video explaining Polymorphism 3 times, and i'm still confused.

    According to the video Polymorphism makes most sense when its used in/as a "Polymorphic array"... which again does not make a whole lot of sense.

    Ok, the array can hold objects of different classes... so what?
    My question to you guys who program a lot, do you use Polymorphism much in your "real life" programs?
    If yes, what are your main reasons for doing so?

    Thanks!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Of course people use polymorphism, it is fundamental to Object Oriented programming and inheritance.

    Say you have an Animal class and subclasses Dog, Cat, Pig, etc then you can create a data structure (array, List) to hold Animal objects then you can add Dog, Cat and Pig objects to it.

    Or maybe you have a method that takes an Animal object as a parameter. Once again you can pass a Dog, Cat or Pig object to that method.

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Of course people use polymorphism, it is fundamental to Object Oriented programming and inheritance.
    Ok... :( Guess I got to keep viewing that tutorial till I "get it" then...

    Say you have an Animal class and subclasses Dog, Cat, Pig, etc then you can create a data structure (array, List) to hold Animal objects then you can add Dog, Cat and Pig objects to it.

    Or maybe you...
    I understand that, but I dont get WHY I would want to have a polymorphic array/list that holds these animal objects in the first place.
    What is the advantage to having such an array and doing it the "old fashioned way" of

    Cat newcat = new Cat();
    Dog newDog = new Dog();
    and so on..

    ?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Always think real world.

    Imagine a payroll system. A company might have numerous different types of employees. Permanent, Casual, Part Time, Contractors, Sales (working on commision) etc etc. You have one parent class Employee and various subclasses representing the different types of employees. Having one data structure to hold all Employee objects make a lot more sense than keeping them all separate. When it comes time to run the payroll job it just loops over the one data structure and calls the one method all those objects might have - calculatePay.

    This is just one example. I'm sure if you put you grey matter to good use you could come up many more examples.

  5. #5
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Ah! That makes more sense!

    The problem I have with most examples is that its not "real world", this one you gave me I can imagine a bit more.

    I have googled "Java polymorphism" and gotten a crapload of results... but would prefer if you guys could recommend a good explanation/article/link so I can start fresh.

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by N00Bie View Post
    if you guys could recommend a good explanation/article/link so I can start fresh.
    There are some articles on java world regarding polymorphism. They are a bit lengthy, but see if you can have a look at them.

    Polymorphism

    Subtype Polymorphism

    Parametric Polymorphism

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by N00Bie View Post
    Ok... :( Guess I got to keep viewing that tutorial till I "get it" then...


    I understand that, but I dont get WHY I would want to have a polymorphic array/list that holds these animal objects in the first place.
    What is the advantage to having such an array and doing it the "old fashioned way"

    immagine you have a class Doctor that offers a checkup for animals. without inheritance you would have to write a method that takes dogs for a checkup, one method that takes cats for a checkup and so on in the class Doctor. but with inheritance, all pets that extends the class animal will be accepted as parameter, so our Doctor class need only one method for any kind of pets. furthermore in your code you need to handly only one array with animals.

  8. #8
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Which raises a question i have, what is really the difference between inheritance and polymorphism? they seem similar to me.

  9. #9
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Debugger View Post
    Which raises a question i have, what is really the difference between inheritance and polymorphism? they seem similar to me.
    I have mostly asked questions here but I think I can answer the above ;)

    Google helped me and I will save you the search:
    Difference between polymorphism and inheritance (Beginning Java forum at JavaRanch)

    And

    Java Interview Questions Java Interview Questions

    I found to be pretty helpful.

    Good luck!

  10. #10
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by Debugger View Post
    Which raises a question i have, what is really the difference between inheritance and polymorphism? they seem similar to me.
    Inheritance is used during the development of classes, polymorphism is used to interact with the objects created from those classes.

  11. #11
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    thank you, i think i get it a little more, will keep looking into it

Similar Threads

  1. Polymorphism
    By blug in forum New To Java
    Replies: 3
    Last Post: 10-11-2010, 10:35 AM
  2. is Polymorphism the tool to use?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 05-04-2009, 05:02 PM
  3. Polymorphism Help
    By AWPtic in forum New To Java
    Replies: 5
    Last Post: 04-06-2009, 04:13 PM
  4. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 03:14 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •