-1

i m trying to calculate the salary in abstract class(in which i m implementing one interface) and trying to extend that abstract class into another class and adding some incentive to that salary but when i m calcuting then it is showing '+' opertor is underifned with generic type

import java.util.List;

public abstract class SalaryInfo<T, V extends Number> implements info<T, V> {
   T hours;
   T rate;
   T pay;

   public void calculatePay(T hours, T rate) {
      pay = hours * rate;
      // Do your math with Integer class methods help
   }

   // return new Double(hours.doubleValue() * rate.doubleValue());
   // System.out.println("pay is b :" +pay);
   public void show() {
      System.out.println("post is :");
   }
}

package com.demo;

public interface info<T,V>  
{
    public void calculatePay(T hours, T rate);
    public void perinfo(V empid,T empname );
    public void display();

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    Please show the code that is causing the error. Please compact your code a little so it's easier to read. Judicious use of whitespace helps improve your code's readability, but too much whitespace reduces readability. The more readable the code, the better answers you'll likely get. Thanks. – Hovercraft Full Of Eels Jun 23 '13 at 17:10
  • This has been asked several times. You can't use arithmetic operations with classes just with primitive types. – Luiggi Mendoza Jun 23 '13 at 17:12
  • @HovercraftFullOfEels TL;DR OP wants to use `Number a; Number b; return a + b;` and other arithmetic operations for these objects. – Luiggi Mendoza Jun 23 '13 at 17:13
  • @LuiggiMendoza: I'm sure you're correct, but regardless, he should post the offending code. It should be fairly obvious that if one asks a question about code that's causing a compilation error, that one should post the offending code and indicate which line it is. I'm just trying to encourage him to ask a complete question is all. – Hovercraft Full Of Eels Jun 23 '13 at 17:14
  • 1
    @HovercraftFullOfEels I agree with you about the format of the question :). Just to note, the problem is in this line: `pay= hours * rate;` – Luiggi Mendoza Jun 23 '13 at 17:15
  • Have you tried `` (in both `info` and `SalaryInfo`)? – Emil Lundberg Jun 23 '13 at 17:17
  • @EmilLundberg: that won't work either. Test it yourself and you'll see. – Hovercraft Full Of Eels Jun 23 '13 at 17:18
  • 1
    Not possible due to erasure. See [this SO][1] answer. [1]: http://stackoverflow.com/questions/3873215/arithmetic-operations-with-java-generics – Ayman Jun 23 '13 at 17:22
  • Ah. I'll admit I was too lazy to try it, just wanted to throw the idea out there. :> – Emil Lundberg Jun 23 '13 at 17:23
  • @Ayman: no, type erasure is not the cause of this. If you used `T extends Double` this could possibly work (but won't for other reasons). It's simply because Number does not allow use of math operators. – Hovercraft Full Of Eels Jun 23 '13 at 17:26
  • If you use Double, then you are autoboxing and converting everything to double. In which case, why bother with Numbers and Type at all? – Ayman Jun 23 '13 at 17:30
  • @Ayman: I agree. I'm just pointing out that type erasure isn't the culprit here. – Hovercraft Full Of Eels Jun 23 '13 at 17:37
  • No it is also not wprking and can u tell me the correct syntax how to rite this bcoz whn i m typung in interfacce its correct and still in class when i m typing like this still its correct but when i m completing the class line implements info it is showing error in this line why it is like that? – user2514033 Jun 23 '13 at 17:53
  • so u guyz asking me to print the full code actully it is program in which i m using abstract class,inheritance inner class concept so many things those thngs are wrking nw i want to make generalize my program so i want generic concept to introduce it and one part i have done through genric i can pass the name and emp id nw i want to calculate emp salary so plz help me tomorrow i have to show this program so plz guyzzz – user2514033 Jun 23 '13 at 18:01
  • Please consider using punctuation and correct spelling. – jahroy Jun 23 '13 at 20:43

1 Answers1

0

Your whole approach is wrong, it has been mentioned that you cannot do math with generics as the math operators are not defined for the autoboxing classes (Integer, Double etc). If your pays never exceed the maximum for doubles, which is something along 10^308 you can simply store the payrate in doubles and the rest of the interface implementation will be as before.

public interface info<T, V> {
public void calculatePay(double hours, double rate);

public void perinfo(V empid, T empname);

public void display();

}

With implementation

public class SalaryInfo<T extends Number, V extends Number> implements
    info<T, V> {

double hours, rate, pay;

@Override
public void perinfo(V empid, T empname) {

}

@Override
public void display() {

}

@Override
public void calculatePay(double hours, double rate) {
    pay = hours * rate;
}

public void show() {
    System.out.println(pay);
}

}
arynaq
  • 6,710
  • 9
  • 44
  • 74
  • thnks a lot sir but suppose if i want to use generic way to provide the values in salary calculation for that wat shuld i do ...plz tell me ..... – user2514033 Jun 23 '13 at 18:24
  • and u r writing T in perinfo so at run time it is showing some string and number related prblm bcoz as empname i m passing sm name to so it is shwoing sm string type error... – user2514033 Jun 23 '13 at 18:46
  • hey .... anybody tell me solution regarding this prblm – user2514033 Jun 24 '13 at 04:04
  • 1
    then. You should really read up on generics a bit before attempting to use it like this ;) – arynaq Jun 24 '13 at 13:03