Learnitweb

Category: Java tutorial

  • Serialization with respect to inheritance

    Case 1: Parent class implements Serializable When parent class is serializable then child class is also serializable. That is if parent class implements Serializable interface then this behavior is inherited to child. Hence, it is not required by child class to explicitly implement Serializable interface. Case 2: Parent class does not implement Serializable but Child…

  • Custom serialization in Java

    Consider a case where an object has username and password as fields. We don’t want password field to be serialized like username field. So we use transient with password field. As we already know, transient fields are not persisted. During serialization, use of transient may lead to data loss. Sometimes, it is needed to serialize…

  • Object graph in Serialization

    It is possible that an object references(uses) other objects as well. When we serialize an object, all objects which are referenced by the serialized object are also serialized. This group of objects is Object Graph. In object graph every object should be serializable. If any of the object is not serializable then we’ll get NotSerializableException.…

  • Serialization of multiple objects in same file

    Yes, we can serialize multiple objects in a single file. While deserialization the order in which objects are deserialized should be same in which the objects were serialized. In the following example, we are serializing object of Student, Employee and Car class. While deserialization, the order is same as serialization. Output Now try changing the…

  • static transient field and final transient field

    The question is sometimes asked as – Can we define a field as ‘static transient’ and ‘final transient’ field? Yes, we can define a field as ‘static transient’. We don’t receive any error and compiler doesn’t complain. However, this is not useful as static fields are not serialized. Static variables belong to a class and…

  • transient keyword in Java and example

    Variables may be marked transient to indicate that they are not part of the persistent state of an object. transient is the modifier applicable only for variables. If you define any data member as transient, it will not be serialized. At the time of serialization JVM ignores the original value of transient variable and save…

  • Serialization and Deserialization example

    In the following example, we are serializing instance of Student class. Student class has two fields, rollNo and age. We are saving state of the object in test.ser file. ObjectOutputStream class has method writeObject to write object state and ObjectInputStream class has method readObject to read object state.During serialization we are reading state of object…

  • Serialization and Deserialization in Java

    Generally all applications require objects to be saved. Once a program is executed/terminated, the object is destroyed by itself. If you want to persist the state of an object you can use the serialization concept by converting data into a byte stream. Serialization means to convert state of an object into a byte stream. Deserialization…

  • What Is RandomAccess Interface?

    This interface is a member of the Java Collections Framework. This interface introduced in Java version 1.4. It marks the implementations of list which can be accessed randomly. This way a caller can either access the data randomly if that is efficient, or use another means if it is not. Marker interface used by List…

  • What is EnumSet?

    EnumSet can be explained with following points: EnumSet example Output EnumSet allOf() and noneOf() example Output EnumSet add() example Output