in Technology by
What are Cloneable Interface in JAVA?

1 Answer

0 votes
by

Cleanable interface in Java is also a marker interface that belong to java.lang package. It generates replica (copy) of an object with different name. We can implement the interface in the class of which class object to be cloned. It indicates the clone() method of the Object class. If we do not implement the Cloneable interface in the class and invokes the clone() method, it throws the ClassNotSupportedException.

Note that a class that implements the Cloneable interface must override the clone() method with a public method. Let's see an example.

Product.java

  1. import java.util.Scanner;  
  2. public class Product implements Cloneable   
  3. {  
  4. int pid;  
  5. String pname;  
  6. double pcost;  
  7. //Product class constructor   
  8. public Product (int pid, String pname, double pcost)  
  9. {  
  10. this.pid = pid;  
  11. this.pname = pname;  
  12. this.pcost = pcost;  
  13. }  
  14. //method that prints the detail on the console  
  15. public void showDetail()   
  16. {  
  17. System.out.println("Product ID: "+pid);  
  18. System.out.println("Product Name: "+pname);  
  19. System.out.println("Product Cost: "+pcost);  
  20. }  
  21. public static void main (String args[]) throws CloneNotSupportedException   
  22. {  
  23. //reading values of the product from the user  
  24. Scanner sc = new Scanner(System.in);  
  25. System.out.print("Enter product ID: ");  
  26. int pid = sc.nextInt();  
  27. System.out.print("Enter product name: ");  
  28. String pname = sc.next();  
  29. System.out.print("Enter product Cost: ");  
  30. double pcost = sc.nextDouble();  
  31. System.out.println("-------Product Detail--------");  
  32. Product p1 = new Product(pid, pname, pcost);  
  33. //cloning the object of the Product class using the clone() method  
  34. Product p2 = (Product) p1.clone();  
  35. //invoking the method to print detail  
  36. p2.showDetail();  
  37. }  
  38. }  

Output:

Enter product ID: 139872
Enter product name: Printer
Enter product Cost: 3459.67
-------Product Detail--------
Product ID: 139872
Product Name: Printer
Product Cost: 3459.67

Related questions

0 votes
    What are the Serializable Interface in JAVA?...
asked Dec 21, 2020 in Technology by JackTerrance
0 votes
    What is an interface in Java?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    What is marker interface in JAVA?...
asked Dec 21, 2020 in Technology by JackTerrance
0 votes
    Which version of java added Flushable interface? (a) java SE 7 (b) java SE 8 (c) java SE 6 (d) java ... Management, JSP & API of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of these interface is not a member of java.io package? (a) DataInput (b) ObjectInput (c) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of the interface contains all the methods used for handling thread related operations in Java? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which version of java added Flushable interface? (a) java SE 7 (b) java SE 8 (c) java SE 6 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    The Java __________ specification defines an application programming interface for communication between the Web ... chapter Database Programming Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    What are the ways to customize the functionality of the Django admin interface?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    What are the primary components of the Kibana interface?...
asked Jun 4, 2021 in Technology by JackTerrance
0 votes
    What are the uses of Marker interface?...
asked Dec 21, 2020 in Technology by JackTerrance
0 votes
    Bots are the new User Interface. Choose the correct answer from below list (1)True (2)False...
asked Oct 27, 2020 in Technology by Editorial Staff
0 votes
    What are the components of a marker interface? (a) Fields and methods (b) No fields, only methods (c) Fields, no ... JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these is a method of ObjectOutput interface used to finalize the output state so that any buffers ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What are the components of a marker interface? (a) Fields and methods (b) No fields, only methods ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...