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 5 of 5

Thread: Interfaces

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Interfaces

    I am still a beginner in Java, so pls excuse me if this is a simple question. I'm given a class to which I do not have access to the code. I need to make this class implement serializable for saving purposes. Is there a work around on how to do this?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Interfaces

    Huh? You have to modify a class you don't have access to? I'm confused...
    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
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    So I take it that a work around does not exist to this situation.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Interfaces

    You could "hack" something together like trying to pull and save the data you need to re-create the object (basically serialization, but without Java's internal serialization mechanism), then when you read the data back manually create that object and set those fields to the read values. There could be some problems with this method: you wouldn't have access to private/protected fields. You'd only be able to reconstruct what you could using public getters and fields.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Interfaces

    Typically it is trickier than this and you don't provide enough info to say one way or another, but you could try and extend the class, having that child implement serializable...it might work but again there are a lot of tricky things which could prevent this from working. The API for Serializable states some of the problems that might be encountered using this technique (Serializable (Java Platform SE 6)).

    Depending upon your needs, Helloworld's advice is spot on - there are 3rd party libraries you can use to read and write a class as XML, and one can accomplish a similar thing using Reflection, or you could hack something together as well...depending upon the requirements I've personally found these techniques (converting an object to a basic text file) much more robust, backwards compatible, and much more amenable to data sharing between applications in the basic context of saving application data (there are certain contexts like enterprise applications where serialization is essential)...ymmv
    Last edited by copeg; April 5th, 2011 at 08:57 PM.

Similar Threads

  1. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 12:06 PM
  2. JDBC Script-Projet + Swing Interfaces
    By kawazaki123 in forum JDBC & Databases
    Replies: 0
    Last Post: July 5th, 2010, 01:25 PM
  3. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM
  4. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 06:11 AM
  5. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 07:44 PM