In: Computer Science
Java Generics (Javas built-in Stack)
What are the problems?
class genStck {
Stack stk = new Stack ();
public void push(E obj) {
push(E);
}
public E pop() {
Object obj = pop();
}
}
class Output {
public static void main(String args[]) {
genStck <> gs = new genStck ();
push(36);
System.out.println(pop());
}
}
HI,
Program:
import java.util.*;
//declare class with generic type <E>
class genStck<E> {
   Stack stk = new Stack();
   public void push(E obj) {
       stk.push(obj);
   }
   public E pop() {
       //return popped element
       return (E) stk.pop();
   }
}
//class with main method need to be public
public class Output {
   //main method
   public static void main(String args[]) {
       try {
           //we need to
provide the Obect type in '<>'
           //like
Integer/Double, String
          
genStck<Integer> gs = new genStck<Integer>();
          
gs.push(36);
          
System.out.println(gs.pop());
       } catch (Exception e) {
           //printing
errors if there any
          
e.printStackTrace();
       }
   }
}

Output Screenshot:
