Results 1 to 11 of 11
Thread: Polymorphism - do you use it?
- 02-14-2011, 03:25 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
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!
- 02-14-2011, 03:44 AM #2
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.
- 02-14-2011, 03:53 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
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...
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..
?
- 02-14-2011, 03:59 AM #4
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.
- 02-14-2011, 04:04 AM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
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.
- 02-14-2011, 05:40 AM #6
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
GoldestJava Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-14-2011, 10:54 AM #7
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.
- 02-15-2011, 02:43 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 27
- Rep Power
- 0
Which raises a question i have, what is really the difference between inheritance and polymorphism? they seem similar to me.
- 02-15-2011, 03:03 PM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
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!
- 02-15-2011, 03:18 PM #10
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
- 02-15-2011, 05:42 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Polymorphism
By blug in forum New To JavaReplies: 3Last Post: 10-11-2010, 11:35 AM -
is Polymorphism the tool to use?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 05-04-2009, 06:02 PM -
Polymorphism Help
By AWPtic in forum New To JavaReplies: 5Last Post: 04-06-2009, 05:13 PM -
what is polymorphism
By Nari in forum New To JavaReplies: 5Last Post: 04-04-2008, 04:14 AM -
what's polymorphism?
By christina in forum New To JavaReplies: 2Last Post: 08-05-2007, 11:29 PM
Bookmarks