Currently I am learning Generic Programming. I didn't understand why Generic Born. when we have Object Class(data Type). Below Code, The functioF1 and function21 in both I have passed the HashCodeclass1 as parameter. Both need to cast The passed parameter to particular Class and the output of operation will be same. Then why Generic Comes into picture?
package know.generic; import knowl.collection.hashcodeInternal.HashCodeclass1; public class ClassGeneric { public <T> void functioF1(T arg) { System.out.println(arg.getClass().getName()); System.out.println("val1 " + arg); HashCodeclass1 hCC = (HashCodeclass1) arg; } public void functioF21(Object arg) { System.out.println(arg.getClass().getName()); System.out.println("val2 " + arg); HashCodeclass1 hCC = (HashCodeclass1) arg; } } public class TestGeneric { public static void main(String arg[]) { ClassGeneric cg = new ClassGeneric(); cg.functioF1(new HashCodeclass1()); cg.functioF21(new HashCodeclass1()); } }