Part 4
Interview questions on serialization in java for experienced covered in this post:
Which changes are considered compatible and incompatible in serialization mechanism?
-
Compatible Changes are as follows.
1.
Changing access modifier:
If you change public, protected, default, private there will be no effect
on serialization.
2.
Adding New fields:
Adding new fields is also compatible. There is no restriction to adding
new fields in serialization.
3.
Changing the field from static to non-static:
It is kind of adding new fields in serialization.
4.
Changing the field from transient to non-transient:
If you are removing the transient keyword then that field will be eligible
for serialization. It’s like adding a new field that is compatible with
change.
-
Incompatible Changes are as follows.
1.
Changing the field from non-static to static: It is equivalent to deletion of the filed.
2.
Changing the field from non-transient to transient:
It is equivalent to deletion of the field.
Can we transfer object through
network if serialization mechanism is not available?
-
Yes, we can transfer object through network if
serialization mechanism is not available.
1.
JSON: We can
convert the java object into JSON object. JSON object can be transferred
through the network.
2.
XML: We can
convert the java object into XML and then we can transfer through the
network.
Can we serialize the class if class
has some collection objects like List, Map and Set?
-
Yes, we can serialize the collection objects like List, Map
and Set.
-
All collections object implements a Serializable
interface.
-
Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.io.Serializable;
import
java.util.List;
import
java.util.Map; import java.util.Set; public class MyCollection implements Serializable{
List<String> myList;
Set<String> mySet; Map<Integer,String> myMap;
public
MyCollection(List<String> myList, Set<String> mySet,Map<Integer,String> myMap) {
this.myList
= myList;
this.mySet
= mySet;
this.myMap
= myMap; }
@Override
public
String toString() {
return
"MyCollection [myList="
+ myList
+ ", mySet="
+ mySet
+ ", myMap="
+ myMap
+ "]";
}
} package simplifiedjava.crackedInterview;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.HashSet;
import
java.util.List;
import
java.util.Map; import java.util.Set; public class SerializeCollectionObjectsDemo { public static void main(String[] args) throws IOException, ClassNotFoundException {
List<String> myList
= new
ArrayList<String>();
Set<String> mySet
= new
HashSet<String>(); Map<Integer,String> myMap = new HashMap<Integer,String>();
myList.add("One");
myList.add("Two"); myList.add("Three");
mySet.add("Four");
mySet.add("Five"); mySet.add("Six");
myMap.put(1, "One");
myMap.put(2, "Two"); myMap.put(3, "Three"); MyCollection collection = new MyCollection(myList, mySet, myMap);
FileOutputStream fos
= new
FileOutputStream("coll.ser");
ObjectOutputStream oos
= new
ObjectOutputStream(fos); oos.writeObject(collection);
FileInputStream fis
= new
FileInputStream("coll.ser");
ObjectInputStream ois
= new
ObjectInputStream(fis); MyCollection updatedCollection = (MyCollection)ois.readObject(); System.out.println(updatedCollection);
}
}
Output: MyCollection [myList=[One, Two, Three], mySet=[Five, Six,
Four], myMap={1=One, 2=Two, 3=Three}] |
Can you Serialize Singleton class and
at the time of deserialization object should be in the same state as
it was during serialization?
-
Yes, we can serialize the singleton class.
-
An object returned by deserialization process is in same
state as it was during the serialization process.
-
We can
implement
readResolve() method to
ensure that we don’t break singleton pattern during deserialization.
-
Signature of readResolve() method.
private
Object readResolve() throws
ObjectStreamException {
return
INSTANCE;
} |
-
You have to define readObject() as well instead of creating
new object.
private
void
readObject(ObjectInputStream ois) throws
IOException,ClassNotFoundException{
ois.defaultReadObject();
synchronized
(SingletonClass.class) {
if
(INSTANCE
== null) {
INSTANCE
=
this;
}
} } |
Can you tell me any purpose to
Serialize Singleton Class?
-
Serialization is one technique to break the singleton
design pattern.
-
Suppose you serialize the singleton object and then if you
de-serialize that object, it will create a new instance, hence breaking
the singleton design pattern.
Why java.lang.Object class doesn't
implement Serializable interface?
-
Serializable is marker interface, which is empty, but when
a class is marked Serializable that means its objects are
Serializable.
-
The reason the java.lang.Object didn’t implement
Serializable is because, what if you do not want to make certain fields as
Serializable and you may missed to add transient to that field, then will
be a havoc.
-
By making the programmer implement Serializable for his
class, it makes awareness among the programmer that he has consciously
implemented it, and should take necessary steps to prevent anything to be
serialized which should not be.
What are the callback methods? What
is the purpose of callback methods? Can you tell me the examples of
callback methods?
-
A callback function is a function passed into another
function as an argument, which is then invoked inside the outer function
to complete some kind of routine or action.
- 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
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. |
0 Comments