Discussions

EJB programming & troubleshooting: Serliazable Interface !!

  1. Serliazable Interface !! (2 messages)

    What is there in Serliazable Inteface ,as i know it is only a marker interface.Can any body speak more on this.

    import java.io.Serializable;
    public class Q1
    {
    public static void main(String[] arg)
    {
    Serializable se;
    int []a={1,2,3};
    se=a;
    System.out.println("Se"+se);
    }
    }

    Why this works ?

    Threaded Messages (2)

  2. Serliazable Interface !![ Go to top ]

    The Java Language Specification (chapter 10, Arrays) says
    "...any array object can be assigned to any variable of these [Object, Cloneable, Serializable] types."

    Check out this demonstration code:

    /**
     * A demonstration of arrays being Objects that implement Serializable.
     */
    public class ArrayTest
    {
        /**
         * The main method driving the test.
         */
        public static void main(String[] args)
        {
            int[] intArray = { 1, 2, 3 };
            String[] stringArray = { "a", "b", "c" };
            NotSerializable[] notserArray = {
                new NotSerializable(),
                new NotSerializable(),
                new NotSerializable()
            };
            java.util.HashMap[] hashmapArray = {
                new java.util.HashMap(),
                new java.util.HashMap(),
                new java.util.HashMap()
            };

            printInterfaces(intArray);
            printInterfaces(stringArray);
            printInterfaces(notserArray);
            printInterfaces(hashmapArray);
        }

        /**
         * Prints the interfaces implemented by the given Object.
         */
        private static void printInterfaces(Object obj)
        {
            Class[] interfaces = obj.getClass().getInterfaces();
            System.out.println("\nInterfaces implemented by " + obj.getClass().getName());
            for (int i=0; i < interfaces.length; i++)
            {
                System.out.println(" " + interfaces[i].getName());
            }
        }

        /**
         * This class does NOT implement Serializable.
         */
        static class NotSerializable
        {
            public NotSerializable()
            {
            }
        }
    }
  3. Serliazable Interface !![ Go to top ]

    (1) Array has Object as it direct superclass, it implements Cloneable and Serializable. So any Array object can be assigned to type - Object, Cloneable, Serializable.

    Serializable se;
    int []a={1,2,3};
    se=a;
        
    (2) System.out.println("Se"+se), call se.toString(). Question arise here is since Serializable is marker interface without method declaration, how se.toString() can be called ? Reason is - if an interface has no direct superinterfaces, then it implicitly declares public abstract method corresponding to each public instance method declared in class Object, unless it explicitly declare the method.