tags:

views:

109

answers:

3

Hi all, i just have a quick question about the Generic Type. i have an interface class

public interface myInterface<T> {
    T add();
}

and a sub class

public class interfaceImp<T> implements myInterface
{
    private T t1;
    private T t2;

    interfaceImp(T t1, T t2){
        this.t1 = t1;
        this.t2 = t2;

    }

    public Object add() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

However, i have no idea about how to implement add(). Should i check the following

1. t1.getClass() == Integer.class&&t2.getClass() == Integer.class</br>
2. t1.getClass() != Integer.class&&t2.getClass() == Integer.class</br>
3. t1.getClass() == Integer.class&&t2.getClass() != Integer.class</br>

f Then case t1 or t2 to the proper type? Or there is a better way of doing it? thanks!!

+1  A: 

Sorry to be so critical, but it's almost impossible to figure out what you're trying to do here. But to start,

public interface MyInterface<T>
  T add();
}

public class InterfaceImp implements MyInterface<Date> { // Or whatever class you want 
  private Date t1;
  private Date t2;

  interfaceImp(Date t1, Date t2){
    this.t1 = t1;
    this.t2 = t2;
  }

  public Date add() {
    throw new UnsupportedOperationException("Not supported yet.");
  }
}
Mike
looks like you updated your question. We still need to know what you want to do with add().
Mike
What i am doing is trying to learn some generics techniques by writing a Calculator application, so add() adds two numbers together.The calculator will have some basic functions, such as +, -, *, /.Also the Calculator is able to do some sample cryptography calculations, such GCD of two integers.Maybe The Calculator application is not a good example to learn generics as basic operations (+,-,*,/) can be easily carried without using generics.
+1  A: 

I think that first of all you have to figure out what your add method does.

That said, what happens if two String are passed? What if a String and an Integer are passed?
Since by the contract you are returning a T what T will you return if two "unaddable" objects are passed?

You are trying to see if t1 and t2 are Integer but if they are always Integer, why the need of generics?

Btw, to know if, for example, t1 is an Integer or not I would've done:

if(t1 instanceof Integer){
    //do something
}

Just a little piece of advice: try not to use class names that start with a lower case letter.

Alberto Zaccagni
Thanks for this"Just a little piece of advice: try not to use class names that start with a lower case letter."
A: 

This isn't how generics are supposed to be used at all. You can't check if T is an Integer, that is not what the class is saying. What it currently says is that it can be defined to be any type of class, and will then add those two together, if it only worked with ints it would be

interfaceImp(Integer t1, Integer t2)

which is obviously pointless as Integers can be easily added.

Instead you are given an interface that provides for an add method, what you want to do is make sure the types your class takes implements that interface, and then you can use the add method.

What your class does, but in method form (to make you do some work for the homework answer this is)

public <? implements myInterface> addTheseItems (<? implements myInterface> first, 
                                                <? implements myInterface> second) {
       return first.add(second);
}

You need to inform the compiler that the supplied types implement the interface or you won't have access to the add method you need.

Chris