Serialization and Deserialization with Example in Inheritance Relationship

I am explaining this concept by using one Example.


package com.javaforecast4u;

import java.io.*;
class Test{
public static void main(String as[]){
Car car=new Car(45,"Mercedes");
System.out.println("Weight  Before "+car.weight+" Name Before "+car.name);
try{
FileOutputStream fos=new FileOutputStream("vehicle.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(car);
oos.close();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Successfully Serialized");
try{
FileInputStream fis=new FileInputStream("vehicle.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
car=(Car)ois.readObject();
ois.close();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Weight  After "+car.weight+" Name  After  "+car.name);
System.out.println("Successfully deserialized");
   }
 }


/* Superclass here without Serializable interface*/

class Vehicle{
int weight=40;
}

/* Subclass here with Serializable interface*/

class Car extends Vehicle implements Serializable{
String name;
public Car(int wt,String nm){
weight=wt;
name=nm;;
}
}
//Output Here
/*
 Weight  Before 45 Name Before Mercedes
Successfully Serialized
Weight  After 40 Name  After  Mercedes
Successfully deserialized

 */
Explanation
Subclass implements Serializable interface. but superclass is not implementing Serializable interface. Subclass Car inherited one variable (int weight)of Superclass Vehicle.So when the object is serialized the weight and name value are respectively 45 and Mercedes. but when it is deserialized the values are 40 (superclass value)and Mercedes. while deserializing then intialization of instance variable starts from the beginning i.e. constuctor called of superclass. so the value is 40 regardless serializing value is 45 for weight.








39 comments :

  1. Replies
    1. I have read your blog its very attractive and impressive. I like it your blog.

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

      Delete
    2. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

      Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

      Delete
  2. Nice Article.... Its serialization and deserialization in inheritance with suitable example...
    Android Training in bangalore

    ReplyDelete
  3. Hi admin, thanks for sharing your informative post on java. Java Training in Chennai

    ReplyDelete
  4. Thanks for sharing informative article on java. It helped me to understand the career prospects in java
    java training in Chennai

    ReplyDelete
  5. Thanks for posting this information.Explains the concept of deserialization and serialization in java with the help of inheritance concept. helps for the freshers to build their careers.
    Software Testing Training in Chennai

    ReplyDelete
  6. Great.. Explained about Serialization and Deserialization with Example in Inheritance Relationship.Its very useful..Way of explanation is very easy to understand.
    Android Training in Chennai

    ReplyDelete
  7. Nice article.This serialization and it's example are so good.It is very useful.
    Java Training in Chennai

    ReplyDelete
  8. Wow... Good topic. In inheritance serialization and deserialization is most important. That are clearly explained with coded examples.
    Informatica Training in Chennai

    ReplyDelete
  9. This is new to me in this concept thanks for sharing this concept which is useful and informative.


    dot net training in chennai

    ReplyDelete
  10. thanks for updated about the inheritance concepts at serialization and deserialization using java codes. thanks for your knowledgeable codes shared with us. keep us ipdate more.
    Loadrunner Training in Chennai

    ReplyDelete
  11. very informative blog. it clearly explains the serialisation and deserialisation concept. thanks for posting this information
    ssis training in chennai

    ReplyDelete
  12. The most important in inheritance is serialization and deserialization. this provides the best process and the programs are explained clearly. thank you for this useful information.
    VMWare Workstation Training in Chennai

    ReplyDelete
  13. It is to read and get great experience about the java forecast.It is a coding knowledge of explanation and is useful for me.
    Sharepoint admin training in chennai

    ReplyDelete

  14. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you. Helpful as always. Every post you write produce a massive value to your readers that is the only reason it is so popular and has great authority.

    Peridot Systems Chennai Reviews

    ReplyDelete

  15. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.


    PHP training in Adyar

    ReplyDelete
  16. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Hadoop Training Chennai | PHP Training in Chennai

    ReplyDelete
  17. We all can know the java topics here about inheritance..Clearly expalined by example nice..Hadoop training in hyderabad.All the basic and get the full knowledge of hadoop.
    hadoop training in hyderabad

    ReplyDelete
  18. Thanks for the detailed explanations. This is really useful and helpful. Providing a various description that gives me clear idea.
    Best Selenium Training in chennai | Selenium training in chennai | Best Automation training in chennai

    ReplyDelete
  19. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Digital Marketing Course
    Seo Training

    ReplyDelete
  20. Thank you for this valuable information. I have got some important suggestions from it. Get your business to the next level in simple steps.
    erp software solutions in chennai.

    ReplyDelete
  21. The best thing is that your blog really informative thanks for your great information!
    erp providers in chennai

    ReplyDelete
  22. The best thing is that your blog really informative thanks for your great information! Keep sharing.
    cloud erp in chennai

    ReplyDelete
  23. Your information about Java is useful for me to know more technical information. Really very informative post you shared here. Keep sharing this type of informative blog. If anyone wants to become a Java professional learn Java Training institute in Chennai. Nowadays Java has tons of job opportunities for all professionals. Java Training Institute in Chennai |
    Dot Net Training Institute in Chennai |

    ReplyDelete
  24. I have read your blog its very attractive and impressive. I like it your blog.
    sap training institute

    ReplyDelete
  25. Java is programming language which is used in almost all the applications and games which are on the web. Java is being used extensively and it will be used extensively in near future. So getting trained in Java will surely be helpful.
    Sap BW Training

    ReplyDelete
  26. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    office 2016 professional plus deutsch download

    ReplyDelete
  27. Good infromation useful for the learners. This is Very good information interested people also Learn mostly asked C Language Interview Questions and Answers and C Language Tutorials

    ReplyDelete