Interview questions on serialization in java for experienced

 Part 2

Interview questions on serialization in java for experienced covered in this post:

    Interview questions on serialization in java for experienced

            What are the steps for Serialization, Say suppose I want to serialize Employee

    Object?

    -          Create Employee class.

    -          Implement a Serializable interface.

    -          Define some fields.

    -          Provide getters and setter methods.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.io.Serializable; 

    public class Student implements Serializable { 

          private int id;

          private static String name;

          private String dept;     

          public Student(int id, String name, String dept) {

                super();

                this.id = id;

                this.name = name;

                this.dept = dept;

          }

          public int getId() {

                return id;

          }

          public void setId(int id) {

                this.id = id;

          }

          public String getName() {

                return name;

          }

          public void setName(String name) {

                this.name = name;

          }

          public String getdept() {

                return dept;

          }

          public void setdept(String dept) {

                this.dept = dept;

          }

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream; 

    public class SerializationDemo { 

          public static void main(String[] args) throws IOException, ClassNotFoundException {           

                Student s1 = new Student(100,"Yogesh","IT");           

                File file1 = new File("stud1.ser");

                FileOutputStream fos = new FileOutputStream(file1);

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(s1);           

                File file2 = new File("stud1.ser");

                FileInputStream fis = new FileInputStream(file2);

                ObjectInputStream ois = new ObjectInputStream(fis);

                Student s2 = (Student) ois.readObject();           

                System.out.println("Student ID :" + s2.getId());

                System.out.println("Student Name :"+ s2.getName());

                System.out.println("Student Dept :"+ s2.getdept());

          }

    }

    Output:

    Student ID :100

    Student Name :Yogesh

    Student Dept :IT

     

            Can you serialize primitive data types?

    -          Yes, All primitives data types are part of Serialization.


     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

     

            Can you serialize private and static variables?

    -          Private Variable: We can serialize private variables. There is no concern at all whether a variable is private, public or protected. It is simply used for serialization because it’s a part of an object.

    -          Static Variable: Static variables belong to a class, not an object. So static variables are not a part of the object state. So static variables cannot consider for serialization.

     

            How can you prevent particular variable to be serialized?

    -          Transient keyword can be used to prevent a particular variable to be serialized.

    -          You can declare the variable as static. Static variables are not considered for serialization.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.io.Serializable; 

    public class Student implements Serializable { 

          private static final long serialVersionUID = 1L;

          private int id;

          private transient String name;

          private String dept;     

          public Student(int id, String name, String dept) {

                super();

                this.id = id;

                this.name = name;

                this.dept = dept;

          }

          public int getId() {

                return id;

          }

          public void setId(int id) {

                this.id = id;

          }

          public String getName() {

                return name;

          }

          public void setName(String name) {

                this.name = name;

          }

          public String getdept() {

                return dept;

          }

          public void setdept(String dept) {

                this.dept = dept;

          }

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream; 

    public class SerializationDemo { 

          public static void main(String[] args) throws IOException, ClassNotFoundException {           

                Student s1 = new Student(100,"Yogesh","IT");           

                File file1 = new File("stud1.ser");

                FileOutputStream fos = new FileOutputStream(file1);

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(s1);           

                File file2 = new File("stud1.ser");

                FileInputStream fis = new FileInputStream(file2);

                ObjectInputStream ois = new ObjectInputStream(fis);

                Student s2 = (Student) ois.readObject();           

                System.out.println("Student ID :" + s2.getId());

                System.out.println("Student Name :"+ s2.getName());

                System.out.println("Student Dept :"+ s2.getdept());

          }

    }

    Output:

    Student ID :100

    Student Name :null – This is not considered for serialization.

    Student Dept :IT 

      

            Can I apply transient keyword to primitive, static and reference variables?

    -          Primitive Variables: transient keyword is applicable to primitive variables.

    -          Static Variables: technically you can declare static variable as a transient but, it doesn’t make a sense to make static variable as a transient because, transient key word is used when you don’t want to consider a particular field for serialization. Static variables by default not considered for serialization.

    -          Reference variable: Reference variable can be null and this variable doesn’t have any value it has an address. So transient keyword is not applicable to reference variables.

     

            If your child class is Serializable and parent class is not and child class is inheriting some properties of parent class, in that case can you serialize child class?

    -          Yes, In that case, we can serialize the child class object.

    -          But while serialization, JVM will ignore the original value of parent class instance variables and save the default value to file.

    -          At the time of de-serialization, if any non-serializable superclass is present then JVM will execute instance control flow in the superclass. To execute instance control flow in a class, JVM will always invoke the default(no-arg) constructor of that class. So every non-serializable superclass must necessarily contain a default constructor, otherwise, we will get InvalidClassException.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    public class Parent { 

          public int i;     

          public Parent() {

                System.out.println("Parent Class no-arg constructor called.");

          }     

          public Parent(int i) {

                this.i = i;

          }                     

          public void check(){

                System.out.println("Parent Class check() method");         

          }    

    } 

    package simplifiedjava.crackedInterview;

    import java.io.Serializable; 

    public class Child extends Parent implements Serializable{     

          int j;     

          public Child() {           

          }

          public Child(int i,int j) {

                super(i);

                this.j = j;

          }     

          public void check(){

                System.out.println("Addition in Child Class "+ (i + j));         

          }    

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream; 

    public class SerializationInHeritanceDemo { 

          public static void main(String[] args) throws IOException, ClassNotFoundException {           

                Child c1 = new Child(10,20);

                System.out.println(c1.i +" and "+ c1.j);  

                FileOutputStream fos = new FileOutputStream(new File("abc.ser"));

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(c1);           

                FileInputStream fis = new FileInputStream("abc.ser");

                ObjectInputStream ois = new ObjectInputStream(fis);

                Child c2 =(Child) ois.readObject();

                System.out.println(c2.i +" and "+ c2.j);

          }

    }

    Output:

    Values Before Serialization : 10 and 20

    Parent Class no-arg constructor called.

    Values After Deserialization : 0 and 20




    • Java interview questions and answers all MNC - Click here
    • Basic core java interview questions and answers for freshers - Click here
    • Core java interview questions for 3 years experience - Click here
    • Core java interview questions and answers for 3-5 years exp - Click here
    • Core java interview questions and Answers for 5 - 7 Years exp - Click here
    • Basic Java Interview Questions and Answers - Click here
    • Java interview questions and answers on oops - Click here
    • Java interview questions and answers on Strings - Click here
    • Java interview questions on exception handling - Click here
    • Interview questions on multithreading in java for experienced - Click here
    • Interview questions on serialization in java for experienced - Click here

    • Interview questions on inner class in java for experienced - Click here
    • Interview questions on Collections in java for experienced - Click here

    Upcoming questions in next post:


    Previous Post                                                                    Next Post



    Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. 

    I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. 

    You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. 

    Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. 

    My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares.  Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now.

    Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. 

    I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. 

    I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! 

    Please reach out to me for personal training for interview preparation. 

    You can reach out to me at mncjavainterview@gmail.com.

     









    Post a Comment

    0 Comments