Wednesday, October 30, 2013

What is generics wildcard arguments? Give an example.


Below example exmplains what is wildcard arguments and how it helps us to solve problem. In the example, we have two classes called CompAEmp and CompBEmp extending Emp class. We have a generic class called MyEmployeeUtil, where we have utilities to perform employee functions irrespective of which comapany emp belogns too. This class accepts subclasses of Emp. Incase if we want to compare salaries of two employees, how can we do using MyEmployeeUtil class? U can think that below sample code might work, but it wont work.

?
1
2
3
4
5
6
7
8
9
//this logic wont work
public boolean isSalaryEqual(MyEmployeeUtil otherEmp){
     
    if(emp.getSalary() == otherEmp.getSalary()){
        return true;
    }
    return false;
}      
                    

Because once you create an object of MyEmployeeUtil class, the type argument will be specific to an instance type. So you can compare between only same object types, ie, you can comapare either objects of CompAEmp or CompBEmp, but not between CompAEmp and CompBEmp. To solve this problem, wildcard argument will helps you. Look at below sample code, which can solve your problem.

?
1
2
3
4
5
6
7
8
public boolean isSalaryEqual(MyEmployeeUtil otherEmp){
     
    if(emp.getSalary() == otherEmp.getSalary()){
        return true;
    }
    return false;
}      
                    

So "?" will solve the issue. Look below for complete example.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.java2novice.generics;
 
public class MyWildcardEx {
 
    public static void main(String a[]){
         
        MyEmployeeUtil empA
                    = new MyEmployeeUtil(new CompAEmp("Ram", 20000));
        MyEmployeeUtil empB
                    = new MyEmployeeUtil(new CompBEmp("Krish", 30000));
        MyEmployeeUtil empC
                    = new MyEmployeeUtil(new CompAEmp("Nagesh", 20000));
        System.out.println("Is salary same? "+empA.isSalaryEqual(empB));
        System.out.println("Is salary same? "+empA.isSalaryEqual(empC));
    }
}
 
class MyEmployeeUtilextends Emp>{
     
    private T emp;
     
    public MyEmployeeUtil(T obj){
        emp = obj;
    }
     
    public int getSalary(){
        return emp.getSalary();
    }
     
    public boolean isSalaryEqual(MyEmployeeUtil otherEmp){
         
        if(emp.getSalary() == otherEmp.getSalary()){
            return true;
        }
        return false;
    }
     
    //// create some utility methods to do employee functionalities
    //
    //
    //
}
 
class Emp{
     
    private String name;
    private int salary;
     
    public Emp(String name, int sal){
        this.name = name;
        this.salary = sal;
    }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
}
 
class CompAEmp extends Emp{
     
    public CompAEmp(String nm, int sal){
        super(nm, sal);
    }
}
 
class CompBEmp extends Emp{
     
    public CompBEmp(String nm, int sal){
        super(nm, sal);
    }
}

Output:
Is salary same? false
Is salary same? true

No comments: