What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with simple example?
Thanks, Bose.
Answer Posted / vamsi
Generics can accept any datatype.
For example if the method write()takes integer type,but we
send them strings,is it possible for the mehod it takes
general variables,but it is possible using generics in java.
just use 'T' Synbol infront of ur method
eg- <T> write();
Not only for method it will be used in front ofclasses
<T>class gen{
int t1=1;
int t2=2;
String s1="generics"
String s2="example";
<T> write(<T> arg1,<T> arg2){
this.arg1=arg1;
this.arg2=arg2;
}
void display()
{
System.out.println(arg1+" "+arg2);
}
}
class example{
public static void main(String args[])
{
Gen g= new Gen();
g.display(t1,t2);
g.display(s1,s2);
}
}
Output is 1 2
Generics example
note# just i told u what i have known,if any thing is wrong
in this just tell me what is the corect way.
Thank u,
vamsi.
| Is This Answer Correct ? | 13 Yes | 6 No |
Post New Answer View All Answers
How do you define a parameter?
List out benefits of object oriented programming language?
Which class is used by server applications to obtain a port and listen for client requests?
How do I print a “?
How do you generate random numbers in java?
Difference between a class and an object?
What are the new features in java 8?
What does the “static” keyword mean?
Why is java called java?
What is Enum in Java?
If you do not want your class to be inherited by any other class. What would you do?
What is difference between filereader and bufferedreader?
What is a copy constructor in java?
If a class is declared without any access modifiers, where may the class be accessed in java programming?
What is the difference between quicksort & mergesort? When should they be used? What is their running time?