Friday, October 16, 2015

Examples of Generic Class, Interface, Method, Constructor


 1. Generics Class Example
public class Msg<T>
{
         public T t;
}
 
Msg<String> m1=new Msg<String>();
m1.t="Hello";

Msg<Integer> m2= new Msg<Integer>();
m2.t=5;


2. Generics Interface Example
public interface A1<T>
{
        public T m1();
}
 
public class B1 implements A1<String>{
        
        public String m1(){
 
        }
}
public class C1 implements A1<Rectangle>{
        public Rectangle m2(){
 
        }
}




3. Generic Constructor Example
class Sample {
  <T> Sample() {
              

               }
}

  Sample s1 = new <String> Sample();


4. Generic Method Example
static <T> void m1(T a, String s) {
    …
}

a1.m1(“hello”,”hi”);

No comments:

Post a Comment