Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Is my understanding of a generic method correct?

Posted on 2011-09-11
Medium Priority
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.
Question by:shampouya
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 47

Expert Comment

ID: 36518571
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.

Author Comment

ID: 36518595
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?
LVL 47

Expert Comment

ID: 36518597
Yes, you are correct.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 47

Expert Comment

ID: 36518601
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
LVL 47

Expert Comment

ID: 36518607
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.

Author Comment

ID: 36518615
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?
LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36518618
Yes, you are correct

Author Comment

ID: 36518626
wow that was easy

Author Closing Comment

ID: 36518628

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

597 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