Monday, January 13, 2014

Java Generic Programming Example

//Note:  Analysis the programs errors,outputs and Understand Generic in Java 



//[1]

class Student<T>{//Generic class
private T name;
public void setName(T n){
name=n;
}
public T getName(){
return name;
}
}

class GenericDemo{
public static void main(String arg[]){
Student<String> s=new Student<String>();
s.setName("sun");
System.out.println(s.getName());
Student<StringBuffer> s1=new Student<StringBuffer>();
s.setName("sun1");
System.out.println(s.getName());


}
}

No comments: