?
Solved

How to the declarations of generic classes affect the methods in those classes?

Posted on 2011-09-12
16
Medium Priority
?
265 Views
Last Modified: 2012-05-12
If I have a class like the one I just made below, and I want to call one of its methods from another class, am I required to call it by passing an argument specifically of type MyType? And in general, in GenericClass am I only allowed to create methods that require a parameter of type MyType? I can't create a method that takes String as a parameter, right? I'm trying to figure out what restrictions the <MyType> on the first line of the code has put on my program.

public class GenericClass<MyType>
{
     private MyType storedValue;
     public void testMethod(MyType x){
          storedValue = x}
}
0
Comment
Question by:shampouya
  • 9
  • 6
16 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 36521176
>>am I required to call it by passing an argument specifically of type MyType?

yes if you are calling testMethod.

>>in GenericClass am I only allowed to create methods that require a parameter of type MyType? I can't create a method that takes String as a parameter, right?
>>

not really, you can create other methods taking or returning differen type than MyType

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36521183

This is how gegeneric class will look, with T being some class, it cna be String or some other class

public class GenaericClass<T>{

private T storedValue;

 public void testMethod(T x){
          storedValue = x}
}


}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36521194
And yyou can then create instance of this GenericClass like thid

GenericClass<String> gcs = new GenericClass<String>();


This allows you to reuse the same code for different types - which is very conveneinet
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 47

Expert Comment

by:for_yan
ID: 36521205


In another case if you need you can reuse the code insde the same class
but replacing T with some other type like

GenericClass<Integer> gcs = new GenericClass<Integer>();

You can still use the methods defines in the class, they will work the same way - so you are thus  thus reusing  the code
0
 

Author Comment

by:shampouya
ID: 36525146
I see, so what's the point of declaring a generic class like this:

public class GenericClass<MyType>

Is that just a way of declaring MyType before you get into the body of the code?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36525167
Probably not much point to do it in the declaration

If you have the typ at the instatiatyion point as i showed above that makes sense, but in declaration, if you put concrete class, rather than <T>
don't know what may be the point
0
 

Author Comment

by:shampouya
ID: 36525199
Ok, and what's the point of putting an object type like Integer when you instantiate a class like this:

GenericClass<Integer> gcs = new GenericClass<Integer>();
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36525233

the point is that the class was declared with generics <T> so when you instantiate the class you specify what concrete type will be used instead of T -
in this case it will be Integer
0
 

Author Comment

by:shampouya
ID: 36525309
And is that useful because it forces the users of GenericClass to only send arguments that are subtypes of T?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36525378

T is just any class

the usefulness of generics is that code which you wrote once can be used in different circumstances - if you need you can use it for String or if you need you can
use it for Integer, or for anything else, so code written once can be re-used
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36525429
as T is just any class so there is no issue of being subtype of T

It is just that the methods are declared for any class - and before you start using them you  need to specify the actual type

You can think of this T as the special kind of mnethod argument which represents the type and you need to provide the value for this argument
as you need to provide other arguments for the method. The only difference that this argument represents the type (which may be a class name or interface name).
0
 

Author Comment

by:shampouya
ID: 36525465
So writing <T> when you declare an object just specifies the type of argument that the user can pass into GenericClass' methods? I still don't understand why that is useful and why that makes it easier to reuse code.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36525544

You see this is a piece of code
(this one does not do much just for sake of example, but could have been longer):

public class GenaericClass<T>{

private T storedValue;

 public void testMethod(T x){
          storedValue = x;
}
}


}

Open in new window


now in one place iof my program I need to do
this operation which I do in this method with an object of tye String:

I will wrte

GenericClass<String> gcs = new GenericClass<String>();
gcs.testMethod("myString");

in some other part of my code
I need to do exactly the same operation with Integer object

I'll write

GenericClass<Integer> gci = new GenericClass<Integer>();
gci.testMethod(new Integer(5));

now lets think UI have Java 1.4 when they diodn't have
generics

What could I do - I will have to write two distinct cklasses:

public class GenaericClassString{

private String storedValue;

 public void testMethod(String x){
          storedValue = x;
}
}

Open in new window


and

public class GenaericClassInteger{

private Integer storedValue;

 public void testMethod(Integer x){
          storedValue = x;
}
}

Open in new window


So instead of reusing the same five lines of code in both cases like in Generics,
I'll have to write two classes and use 10 lines fof cvode in toatl

Now imagine that I have class which has 1000 lines - then I can save 1000 lines of code suing generics












0
 

Author Comment

by:shampouya
ID: 36526076
Ok so the <T> in front of the class name gives the opportunity for all the methods in this generic class to use T as a parameter which will accept any object type in Java, as long as T is replaced by the desired type when this generic class is instantiated in another class, like this:

GenericClass<anyTypeHere> gc = new GenericClass<anyTypeHere>();

And this prevents the need for extra code because you don't need to make an extra copy of the class you are using just to accommodate a different object type. Instead, you only have to change how you instantiate the class in one line.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36526118
Yes this is alll  correct
0
 

Author Closing Comment

by:shampouya
ID: 36526186
many thanks!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

585 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