Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advanced search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Devaka Cooray
Tim Cooke
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Saloon Keepers:
Piet Souris
Bartenders:
Forum:
I/O and Streams
Inheritance and Serialization
harshada patil
Ranch Hand
Posts: 96
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If super-class is not serializable but subclass is serializable
1. The super class must have no argument constructor accessible to subclass.
2. This constructor will be used to initialize subclass' sub-object.
please explain above statement.
Rob Spoor
Sheriff
Posts: 22907
132
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.io.*; class SuperClass { public SuperClass() { System.out.println("SuperClass() called"); } public SuperClass(int i) { System.out.println("SuperClass(" + i + ") called"); } } class SubClass extends SuperClass implements Serializable { public SubClass(int i) { super(i); System.out.println("SubClass(" + i + ") called"); } } public class Test { public static void main(String[] args) throws Exception { SubClass sub1 = new SubClass(13); // two lines are printed // serialize ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(sub1); oos.close(); byte[] data = baos.toByteArray(); System.out.println("---"); // deserialize ByteArrayInputStream bais = new ByteArrayInputStream(data); ObjectInputStream ois = new ObjectInputStream(bais); SubClass sub2 = (SubClass)ois.readObject(); // only one line is printed } }
Try this code. After that remove the first constructor of SuperClass and try again.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
harshada patil
Ranch Hand
Posts: 96
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hey thanks rob for your solution... Now i got the point....
Rob Spoor
Sheriff
Posts: 22907
132
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're welcome
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Default constructor
Serialization via Inheritance
Serialization doubt
How Inheritance Affects Serialization
Deserialization
More...