OOP-JAVA Practical 24

OOP-JAVA Practical 24
  • Aim: Define MYPriorityQueue class that extends Priority Queue to implement the Cloneable Interface and implement the clone () method to clone a priority queue.
  • Code:
    import java.util.PriorityQueue;
    public class practical24 {
        public static void main(String[] args) {
            MyPriorityQueue<String> queue= new MyPriorityQueue<>();
            queue.add("ABC");
            queue.add("PQR");
            queue.add("MNO");
            queue.add("XYZ");
            System.out.println("Queue: \n"+queue);
            try {
                MyPriorityQueue<String> queue1=(queue.clone());
                System.out.println("After Cloning to another Queue:");
                System.out.println(queue1);
            } catch (CloneNotSupportedException e) {
                System.out.println("Error");
            }
        }
        public static class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable{
            public MyPriorityQueue<E> clone() throws  CloneNotSupportedException{
                MyPriorityQueue<E> clone= new MyPriorityQueue<>();
                this.forEach(clone::offer);
                return clone;
            }
        }
    }
  • Output:
    Output of practical 24

Comments