user1420042
user1420042

Reputation: 1709

What is the point of making a class generic?

when you have a method, I understand that it makes sense to declare it generic so that i can take generic arguments. Like this:

public <T> void function(T element) {
    // Some code...
}

But what exactly is the idea behind making a whole class generic if I can simply declare every method generic?

Upvotes: 6

Views: 729

Answers (4)

PermGenError
PermGenError

Reputation: 46428

Well, the difference is if you try to make each method in your class generic the generic type you'd use in your say firstGenericMethod may or may not be the same type.What i mean is.

public <T> void firstGenMethod(...){

}

public <T> void secondGenMethod(...){

}

Test:

  SomeClass ref = new SomeClass();
  ref.firstGenMethod("string");
  ref.secondGenMethod(123);//legal as this generic type is not related to the generic type which is used by firstGenMethod

In the above case there is no gaurentee that both the methods have the same generic type.It depends on how you invoke them. If you make the class generic though, the type is applied to all the methods inside that class.

class Test<T>{

    public void firstGenMethod(T t){

    }

    public  void secondGenMethod(T t){

    }
}

Test:

 Test<String> testingString = new Test<>();
   testingString.firstGenMethod("abc");
   testingString.firstGenMethod(123);// invalid as your Test class only expects String in this case

You'd usually make your class generic where you'd want the entire behaviour(methods) of that class to process on the same type. best examples are class's in Java Collection Framework

Upvotes: 8

Despicable
Despicable

Reputation: 3957

The main idea is to bound the class/method for a type.The best example is the concept of Generics in programming language.It is the real application of polymorphism
Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees and so on where operations such as adding and removing items from the collection are performed in much the same way regardless of the type of data being stored.

For most scenarios requiring collection classes, the recommended approach is to use the ones provided in the programming languages.
Reffering from oracle docs
A generic type is a generic class or interface that is parameterized over types

public class Box {
    private Object object;

    public void set(Object object) { this.object = object; }
    public Object get() { return object; }
}


By generically you can strict it to

public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}


I also suggest you to see this tutorial

Upvotes: 1

phtrivier
phtrivier

Reputation: 13367

Precisely, to enfore that all the methods in this class apply to a certain type.

This is typically used in "containers" classes. If you're building a List, and you make it generic, you want to be sure that adding element, getting element, iterating, etc... always uses the same type.

As you mention, this also allows you to use instance variables of a certain type, and have several methods use this instance (maybe returning it, affect it...) in a coherent way.

If you have a class that is "generic" with several different types, it might be a sign that you're really writing two generic classes in one... (although this can be a legitimate thing.)

Upvotes: 2

Axel
Axel

Reputation: 14169

Have a look at the Collection classes. Take List<T> as an example. If the class weren't declared generic, how would you make sure that only elements of the correct class could be inserted into the list? And how would you know what you get when calling ArrayList.get(i)?

Upvotes: 4

Related Questions