Solved

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

Posted on 2011-09-12
16
221 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 total points
ID: 36526118
Yes this is alll  correct
0
 

Author Closing Comment

by:shampouya
ID: 36526186
many thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

930 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

10 Experts available now in Live!

Get 1:1 Help Now