Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cloneable interface

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Cloneable interface

    Question:
    The purpose of Cloneable interface is to tell JVM that this class is eligible for cloning. Note that the class Object doesn't implement , hence if you call the Object class "clone()" method without implementing Cloneable interface, a CloneNotSupportedException will be thrown.

    How do I implement Cloneable interface, in my Taxpayer class below. Since I am doing An override of the clone method in this class. I am already implementing Taxable interface. Would it be possible to implement Taxable interface and Coneable interface?

    public class Taxpayer implements Taxable
    {
     
        private String iName = "";
        private float iIncome = 0;
        private double iTax = 0;
        private boolean iclone = false;
     
     
     
        public Taxpayer(String name, double income)
        {
     
             iName = name;
             iIncome = (float) income;  
     
        }
        public Object clone()
        {
               Taxpayer clone = null;
     
                try{
     
                   iclone = true;
                   clone = (Taxpayer) super.clone();
                } catch (CloneNotSupportedException xcp ) { clone = null;}
     
               return clone;
     
        }
    }

    Thank you,


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Cloneable interface

    What have you tried? Note that it's entirely possible to implement multiple interfaces. Just separate them by commas.

    PS- You have said that you can't call clone() on an Object. So what you do you expect super.clone() to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Cloneable interface

    Please do not post multiple copies of the same question. I've deleted your duplicate thread.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Interface
    By kaasi in forum Member Introductions
    Replies: 4
    Last Post: April 21st, 2011, 10:45 AM
  2. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  3. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  4. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM
  5. Interface problem please help!!
    By joff1403 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2010, 11:09 AM