The only differences I can find:
1. The names of the arguments in the readObject and writeObject methods in the B class. Instead of "in" and "out" I use "aInputStream" and "aOutputStream". Shouldn't matter, right?
2. The order in which classes are declared. I have A and B declared before Main, the solution has them after. Again, this shouldn't matter.
3. I use "this.nameA" instead of just "nameA" in the two methods. I have run it without "this" and it has the same error, so that isn't it. I generally prefer including "this" when accurate because it allows a quick glance to recognize if the variable belongs to the object or just a temporary one I created in the method.
I have been looking over and over everything else, and it appears to be identical to the solution. Any ideas?
Oh, and the notserializableexception is generated on line 40 (the defaultWriteObject line).
package com.codegym.task.task20.task2018;
import java.io.*;
/*
Find the bugs
*/
public class Solution
{
public static class A
{
protected String nameA = "A";
public A(String nameA)
{
this.nameA += nameA;
}
public A()
{
}
}
public class B extends A implements Serializable
{
private String nameB;
public B(String nameA, String nameB)
{
super(nameA);
this.nameA += nameA;
this.nameB = nameB;
}
private void writeObject(ObjectOutputStream aOutputStream) throws IOException
{
aOutputStream.defaultWriteObject();
aOutputStream.writeObject(this.nameA);
}
private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException
{
aInputStream.defaultReadObject();
this.nameA = (String) aInputStream.readObject();
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException
{
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(arrayOutputStream);
Solution solution = new Solution();
B b = solution.new B("B2", "C33");
System.out.println("nameA: " + b.nameA + ", nameB: " + b.nameB);
oos.writeObject(b);
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(arrayInputStream);
B b1 = (B)ois.readObject();
System.out.println("nameA: " + b1.nameA + ", nameB: " + b1.nameB);
}
}