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 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.


    protected Object readObjectOverride();


  • It does not accept any parameter.

Return value:

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


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

public class ReadObjectOverride extends ObjectInputStream {
 public ReadObjectOverride(InputStream in_stm) throws IOException {

 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 );


  // 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);


obj_in_stm.readObjectOverride(): null


Comments and Discussions



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

© some rights reserved.