Results 1 to 3 of 3
  1. #1
    Shellback3 is offline Member
    Join Date
    Jan 2011
    Location
    Riveside, CA
    Posts
    16
    Rep Power
    0

    Default Reflection Invoking method of "super" class

    Disclaimer: I'm new to Java.
    I have an abstract class that implements Serializable and has two methods, one to write (as XML) to disk the other to read and create an instance of the class that was serialized. I have several classes that implement this class which are used to save initialization data for programs. I've been able to figure out how to use reflection arbitrarily access the files and display field names, field values, and field types (so that the values may be edited (without writing a separate editor for each class). I *think* I've written code that replaces values with edited ones (not yet tested) but I don't understand how to invoke the method of the abstract class to serialize the edited object.

    How do I access the "serialize" method of the abstract class? The abstract class and an example of a derived class are below.

    Abstract Class:
    Java Code:
    import java.io.Serializable;
    import java.beans.*;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public abstract class SerialXMLIO implements Serializable {
    	public SerialXMLIO() { }
    
     	public final void  serialize(String filename) {
    		try {
    		XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
    					new FileOutputStream(filename)));
    			encoder.writeObject(this);
    			encoder.close();
    		}
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    	}
    
    
    	public final Object deserialize(String filename) {
    	Object ob = null;
    		try {
    		XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
    					new FileInputStream(filename)));
    			ob = (SerialXMLIO) decoder.readObject();
    			decoder.close();
    		}
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    		return ob;
    	}
    }
    Derived Class:
    Java Code:
    public final class LastFolderSaver  extends SerialXMLIO {
    
    	private String lastFolder;
    	
    	public LastFolderSaver() {}
    	
    	public String getLastFolder() {
    		return lastFolder;
    	}
    
    	public void setLastFolder(String lastFolder) {
    		this.lastFolder = lastFolder;
    	}
    }

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    All the methods of the superclass that are not 'private' are available to subclasses. In this case, create an instance of LastFolderSaver...

    Java Code:
    LastFolderSaver lastFolderSaver = new LastFolderSaver ();
    lastFolderSaver.serialize("afilename");

  3. #3
    Shellback3 is offline Member
    Join Date
    Jan 2011
    Location
    Riveside, CA
    Posts
    16
    Rep Power
    0

    Lightbulb Solved

    Thanks, I had forgotten that in exactly one spot in my app the class that I'm working with is known so was able to implement deserialize. Got to solve one more problem before I test it though but it compiles OK.

Similar Threads

  1. calling super method via reflection
    By oida in forum Advanced Java
    Replies: 5
    Last Post: 01-22-2011, 01:00 PM
  2. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •