Interview Question @ IBM: Write your own generic class

Qu1: Can you write a small generic class by yourself ?

Answer: This could be like this......


public class GenericClassDemo<X, Y> {
   X name;
   Y age;


   GenericClassDemo(X name, Y age) {
      this.name = name;
      this.age = age;
   }


   X getX() { return name; }


   Y getY() { return age; }


   // test it by creating it with <String, Integer>


  public static void main (String[] args) {


       GenericClassDemo<String, Integer> obj= 
                     new GenericClassDemo<String, Integer>("Rajesh Kumar", 15);


       String empName = obj.getX();    // returns a String


       int empAge = obj.getY();    // returns Integer, unboxes to int


       System.out.println("Generic use::: EmpName  :"+empName +" and EmpAge :"+empAge);
}
}
----------------------------------------------------------------------------
Qu2. Create your own custom generic class using wildcard ?

Answer: Let's take an example..
---------------------------------------

abstract class Animal {
   public abstract void eat();
}


class Dog extends Animal {
  public void eat() { // implement Dog-specific code
    System.out.println("Dog is eating....");
  }
}


class Cat extends Animal {
  public void eat() { // implement Cat-specific code
     System.out.println("Cat is eat....");
  }
}








public class AnimalGenericTest<T extends Animal> {
   T animal;


   AnimalGenericTest(T animal){
     this.animal=animal;


   }   
   


   T getT(){
       return animal;
   }




   public static void main(String[] args) {
   
      AnimalGenericTest<Dog> dog = new AnimalGenericTest<Dog>(new Dog()); // OK
      AnimalGenericTest<Cat> cat = new AnimalGenericTest<Cat>(new Cat()); // OK


     // AnimalGenericTest<Integer> x = new AnimalGenericTest<Integer>(); // NO! Will not compile, 
      // because it takes only Animal of subclass of animal type object not Integer
      
      dog.getT().eat();
      cat.getT().eat();


       System.out.println("End of class........");
    }
}

--------------------------------------------------
The output will be as..........


C:\SCJP6\generics>javac AnimalGenericTest.java


C:\SCJP6\generics>java AnimalGenericTest
Dog is eating....
Cat is eat....
End of class........

----------------------------------------------------------
Qu3. How will you use generic method in this code??

See the code again:


import java.util.*;

abstract class Animal {
   public abstract void eat();
}


class Dog extends Animal {
  public void eat() { // implement Dog-specific code
    System.out.println("Dog is eating....");
  }
}


class Cat extends Animal {
  public void eat() { // implement Cat-specific code
     System.out.println("Cat is eat....");
  }
}








public class AnimalGenericTest<T extends Animal> {
   T animal;


   AnimalGenericTest(T animal){
     this.animal=animal;


   }   
   


   T getT(){
       return animal;
   }




   public <T> void makeArrayList(T t) { // take an object of an
      // unknown type and use a "T" to represent the type
      
      List<T> list = new ArrayList<T>(); // now we can create the list using "T"
      list.add(t);
      System.out.println("List is:"+list.toString());
   }


   public static void main(String[] args) {
   
      AnimalGenericTest<Dog> dog = new AnimalGenericTest<Dog>(new Dog()); // OK
      AnimalGenericTest<Cat> cat = new AnimalGenericTest<Cat>(new Cat()); // OK


     // AnimalGenericTest<Integer> x = new AnimalGenericTest<Integer>(); // NO! Will not compile, 
      // because it takes only Animal of subclass of animal type object not Integer
      
      dog.getT().eat();
      cat.getT().eat();
      
      dog.makeArrayList(dog.getT());


      cat.makeArrayList(cat.getT());


       System.out.println("End of class........");
    }
}
------------------------------------------------------------------------------

See the output:


C:\SCJP6\generics>javac AnimalGenericTest.java


C:\SCJP6\generics>java AnimalGenericTest
Dog is eating....
Cat is eat....
List is:[Dog@42e816]
List is:[Cat@9304b1]
End of class........


Comments

Popular posts from this blog

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

Upload csv file at Google Cloud Storage using java

Read Images from a xlsx file using Apache POI