20

How do I write an equivalent of this in Java?

// C++ Code

template< class T >
class SomeClass
{
private:
  T data;

public:
  SomeClass()
  {
  }
  void set(T data_)
  {
    data = data_;
  }
};
0x6B6F77616C74
  • 2,559
  • 7
  • 38
  • 65
sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • 1
    Note that while the code you presented can be converted to Java, in general Java generics have only a subset of the functionality that C++ template have (you cannot specialize, no SFINAE...) – David Rodríguez - dribeas Dec 06 '09 at 10:26

4 Answers4

39
class SomeClass<T> {
  private T data;

  public SomeClass() {
  }

  public void set(T data_) {
    data = data_;
  }
}

You probably also want to make the class itself public, but that's pretty much the literal translation into Java.

There are other differences between C++ templates and Java generics, but none of those are issues for your example.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • 4
    Actually, one difference, which isn't a generic/template thing but a more fundamental difference between C++ and Java, is that the set method in C++ copies the supplied object, while the Java version just copies the reference. – Laurence Gonsalves Dec 06 '09 at 04:25
  • Wow, and the cool thing about "generics" is that I can even express something like public class SomeClass! I dont think that ability is present in C++. So cool! – sivabudh Dec 06 '09 at 04:33
  • 3
    ShaChris23: the prevalent idiom in the C++ stdlib is to use duck typing: "if all the attempted uses work with the object, it has the interface expected" (std::reverse_iterator being a good, clear example), and this is similar to Python and [my understanding of] Go. It is still sometimes useful to make those restrictions yourself (or at least people ask for it :P), and you can use boost::enable_if, static asserts (with type traits to detect inheritance in this case), and similar for that. Concepts were supposed to help address this, but that's another story. –  Dec 06 '09 at 06:39
  • 2
    @ShaChris23 [`is_base_of`](http://en.cppreference.com/w/cpp/types/is_base_of) does exactly that, not to mention C++ templates have countless abilities that java generics don't, they are turing complete for god sakes, IMO one of the best features of C++ – aaronman Oct 14 '13 at 22:55
  • @aaronman `is_base_of` was added in C++11, but ShaChris23's comment was from 2009. In other words, it wasn't part of (standard) C++ when he made that comment. – Laurence Gonsalves Oct 15 '13 at 03:29
6

You use "generics" to do this in Java:

public class SomeClass<T> {
  private T data;

  public SomeClass() {
  }

  public void set(T data) {
    this.data = data;
  }
};

Wikipedia has a good description of generics in Java.

Moishe Lettvin
  • 8,462
  • 1
  • 26
  • 40
3
/**
 * This class is designed to act like C#'s properties.
 */
public class Property<T>
{
  private T value;

  /**
   * By default, stores the value passed in.
   * This action can be overridden.
   * @param _value
   */
  public void set (T _value)
  {
    value = _value;
  }

  /**
   * By default, returns the stored value.
   * This action can be overridden.
   * @return
   */
  public T get()
  {
    return value;
  }
}
Bill
  • 536
  • 5
  • 7
1
public class GenericClass<T> {
    private T data;

    public GenericClass() {}

    public GenericClass(T t) {
        this.data = t;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // usage 
    public static void main(String[] args) {
        GenericClass<Integer> gci = new GenericClass<Integer>(new Integer(5)); 
        System.out.println(gci.getData());  // print 5; 

        GenericClass<String> gcs = new GenericClass<String>(); 
        gcs.setData("abc");
        System.out.println(gcs.getData());  // print abc;
    }
}
hichris123
  • 10,145
  • 15
  • 56
  • 70