Solved

Is my understanding of a generic method correct?

Posted on 2011-09-11
9
294 Views
Last Modified: 2012-05-12
I'm trying to summarize for myself what a generic method is. Am I correct here?

Let's say you are implementing a method that accepts a collection of objects and sorts the objects. The method signature specifies the specific type of object you will be storing in the collection. Let's say the method takes "Collection<myType>" as its parameter. You can call this method with any collection whose element type in this collection is a parent of myType. This is an example of a generic method.
0
Comment
Question by:shampouya
  • 5
  • 4
9 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
you can call this method with any collection of object either of myType or of type inherited from myType.
So if myType is Object you can call with  the Collection of String.
This is because it meas in your method you may be using methods of myType, and you may rely that any objects which inherit form myType
do have methods of myType class.
0
 

Author Comment

by:shampouya
Comment Utility
So if the method took "Collection<Object>" as it's parameter, then a collection of String could be passed as the argument, because String is a child of Object. Am I correct?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Yes, you are correct.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
The ooposite would not work:

imagine I have a method which  relies on collection of Strings
It means that it may well happened theat somewhere inside the method
I may extract some element and say use method substring() of the String elelment

Now if you passed me collection of Objects - these elemets will not have such method as subvstring()  - so the method cannot work for such collection
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 47

Expert Comment

by:for_yan
Comment Utility
However String has all those methods which Object has,
therefore if my method was devised for a collection of Objects - it means inside the method I could only apply to lelements of the collection only methods of class Object.
So if I now pass collection of Strings to thuis method everything will workbecause Strinngs do have all those methods which Objects have - so no problem may arise -
metods declared for Collection of Objects will worjk for collection of Strings also.
0
 

Author Comment

by:shampouya
Comment Utility
Ok that makes sense. So can I think of a generic method as a method that:

a) takes type parameters
b) has the type specified in the angled brackets <> within the method signature
c) expects "exampleType" to be in its parameters but it will also accepts any children of exampleType because they inherit from exampleType

Am I correct here?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
Yes, you are correct
0
 

Author Comment

by:shampouya
Comment Utility
wow that was easy
0
 

Author Closing Comment

by:shampouya
Comment Utility
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now