• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

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

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
shampouya
Asked:
shampouya
  • 9
  • 6
1 Solution
 
ksivananthCommented:
>>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
 
for_yanCommented:

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
 
for_yanCommented:
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
Industry Leaders: 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!

 
for_yanCommented:


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
 
shampouyaAuthor Commented:
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
 
for_yanCommented:
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
 
shampouyaAuthor Commented:
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
 
for_yanCommented:

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
 
shampouyaAuthor Commented:
And is that useful because it forces the users of GenericClass to only send arguments that are subtypes of T?
0
 
for_yanCommented:

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
 
for_yanCommented:
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
 
shampouyaAuthor Commented:
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
 
for_yanCommented:

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
 
shampouyaAuthor Commented:
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
 
for_yanCommented:
Yes this is alll  correct
0
 
shampouyaAuthor Commented:
many thanks!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now