Home » Java programming language

Java ObjectInputStream readObjectOverride() Method with Example

ObjectInputStream Class readObjectOverride() method: Here, we are going to learn about the readObjectOverride() method of ObjectInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 05, 2020

ObjectInputStream Class readObjectOverride() method

  • readObjectOverride() method is available in java.io package.
  • readObjectOverride() method is invoked by child class of ObjectOutputStream that formed ObjectOutputStream with the help of protected default constructor. The child class is expected to provide an override method with the "final" keyword.
  • readObjectOverride() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • readObjectOverride() method may throw an exception at the time of object overriding.
    • ClassNotFoundException: This exception may throw when the serialized object Class could not exist.
    • IOException: This exception may throw when getting any input/output error while performing.
    • OptionalDataException: This exception may throw when unexpected primitive data found instead of objects.

Syntax:

    protected Object readObjectOverride();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Object, it returns the Object read from the ObjectInputStream.

Example:

// Java program to demonstrate the example 
// of readObjectOverride() method of ObjectInputStream

import java.io.*;
public class ReadObjectOverride extends ObjectInputStream {
 public ReadObjectOverride(InputStream in_stm) throws IOException {
  super(in_stm);
 }

 public static void main(String[] args) throws Exception {
  Integer in = new Integer(10);
  // Instantiates ObjectOutputStream , ObjectInputStream 
  // FileInputStream and FileOutputStream
  FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
  ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
  FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
  ReadObjectOverride obj_in_stm = new ReadObjectOverride(file_in_stm);

  // By using writeObject() method is to write
  // object to the obj_out_stm stream
  obj_out_stm.writeObject( in );

  obj_out_stm.flush();

  // By using readObjectOverride() method is to read
  // object and convert it into Integer
  Integer i = (Integer) obj_in_stm.readObjectOverride();
  System.out.println("obj_in_stm.readObjectOverride(): " + i);
 }
}

Output

obj_in_stm.readObjectOverride(): null






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.