(Lab-2 Manual) CS-242-DSA - Writing Generic Methods and Classes
(Lab-2 Manual) CS-242-DSA - Writing Generic Methods and Classes
Fall 2019
Lab-2 Manual
Writing Generic Methods and Classes
Lab-2 Manual 2019
Page 1
Lab-2 Manual 2019
Page 2
Lab-2 Manual 2019
Example
Following example illustrates how extends is used in a general sense to mean either "extends"
(as in classes) or "implements" (as in interfaces). This is an example which takes two generic
arguments and return true if the numbers are equal, otherwise returns false.
Page 3
Lab-2 Manual 2019
public T getValue() {
return this.value;
}//Getter
//Printing Values
System.out.println("Integer Value: " +
integerBox.getValue());
System.out.println("String Value: " +
stringBox.getValue());
}//main
}//class
NOTE: Open the Box.java code from the Code folder. Compile and run the code.
Page 4
Lab-2 Manual 2019
private T obj1;
private T obj2;
//Constructor
public GenericClassLab1(T obj1, T obj2)
{ }//GenericClass
//Setters
public void setObj1(T obj1) {
}//Set Obj1
//Getters
public T getObj1() {
}//get Obj1
public T getObj2() {
}//get Obj2
Page 5