Why ObjectOutputStream.readObject () accesses MyClass.readObject ()

I was learning the basics of Java IO, and I saw that to use a user-defined function writeObjectin a class, it must be declared private.

private void writeObject(ObjectOutputStream oos);

Then when we call

oos.writeObject(myClassObject);

This function searches for the private writeObject method in MyClass and executes it.

My question is: if this is true, then will it not be a violation of the concept of data abstraction when a function can call a private method of another class? What is the reason for this feature?

+4
source share
2 answers

Mostly I do education.

The way serialization works is logically tied to one class of objects. Actually, it makes no sense to inherit it, so it is forced to be private.

OO, . , .

0

, OO, (, ).

, , API .

0

Source: https://habr.com/ru/post/1598185/


All Articles