L
Liz
An addition:
For example there's nothing that prevents the Java developers from
creating a new root of the class hierarchy,
But there's all that documentation SUN would have to change.
An addition:
For example there's nothing that prevents the Java developers from
creating a new root of the class hierarchy,
Michael said:Yes. It works with current versions of the *Java* language because every
object is an instance of Object. Therefore you can cast any object to
type Object and only therefore you can call getClass. Don't think in
Java, draw e.g. an UML static structure diagramm and you'll see, what I
mean.
would rely on the serializable interface, the cast to Object would e.g.
result in a ClassCastException.
The most experienced java developer/architect I know is my dog.
In fact :-(
Ok, then let's assume that overriding isn't the argument. Even so, I
don't think that writeObject should take a Serializable. I think that
this has something to do with design.
If writeObject would take an argument of type Serializable, this would
only work because AFAIK all objects in (current) Java are instances of
Object.
Serializable doesn't declare any methods. If we would rely only on the
Serializable interface we wouldn't be able to use any method. E.g. there
would be no way to call getClass (because Serializable doesn't define it).
ROTFLTony said:I want some of that stuff you're smoking.
Lee said:I don't see any casting in the code above.
Yes, every object in Java must be an instance of Object. That can't change, so there's
no need to say current versions.
I have no idea what "UML static structure diagram" means in this context. I don't know
what you mean.
lyallex said:On Wed, 09 Jun 2004 20:04:47 +0200, Michael Rauscher
So
public class MyClass implements Serializable{
public void makeStupidComment(){
System.out.println("My dog knows Java");
}
...
}
Serializable s = new MyClass();
s.getClass() //OK, comes from Object
s.makeStupidComment(); //Can't do this because serializable is higer
up the inheritence hierarchy.
I just noticed that it is writeObject ( Object o )
rather than writeObject ( Serializable o )
how come?
Michael said:Serializable doesn't declare any methods. Therefore either the developer
or the compiler must do a cast to Object.
Between the interface Serializable and the class Object there's no
relation. In the first example, it would be wrong (or bad design) to
send a getClass message to "a" at design time, because "a" is
Serializable and Serializable doesn't declare such a method.
The fact, the in the current version of the model anything is an
instance of Object wouldn't make the design better.
That would be the same as if one method takes a Collection and
internally assumes that this Collection is a Vector:
public Enumeration getEnumeration( Collection c ) {
return ((Vector)c).elements();
}
an 'is a' relationship with that interface. It's java's way of
providing (pseudo) multiple inheritence without many of the unpleasent
side effects of full multiple inheritence.
When you implement an interface the resulting type exists 'above' the
implementing class in the hierarchy but 'below' object
There are implicit widening conversions in Java, which are sometimes
confusingly referred to as "upcasts". Nevertheless, unless you choose
to do so unnecessarily, these conversions don't require a cast.
Besides, this isn't a widening conversion anyway. There's never a wider
type involved in this expression.
And this pretty much sums up why you shouldn't become obsessed with UML
for making design decisions for you. Here you're confusing a quirk of
the UML modelling format with justification for API design. Let me put
this simply: there's absolutely no good reason to avoid calling methods
of java.lang.Object via an interface type. When you start considering
that, it's a good sign that you're letting your desire to do "clean"
design interfere with your ability to use the language you're working
in.
"The current version of the model"?
You seem to be missing the point. You are apparently picking one out of
a potential seven billion changes that could be made to Java -- and a
*very* unlikely one at that -- and advocating designing around the
possibility that the language might change in that way. If the language
changes that dramatically, or (far more likely) if you decide on a
different implementation language or write bindings for a different
language, you're going to have a *lot* of porting and adaptation to make
your code work, and whether your API is declared to take an interface
parameter or not in Java is just plain insignificant in that scenario.
So why not design around the possibility that Sun might eliminate
interface types entirely, or that you might switch to a language that
doesn't have those? Or ever better, what about automatic memory
management? Should I be designing my Java APIs to manage who has
responsibility for reclaiming memory, even though it really doesn't need
to be done at all? After all, Sun might change Java to eliminate the
garbage collector, or I might decide to provide a C++ interface to the
library. What then?
Well, no it's not. One situation involves a type conversion that's not
guaranteed to succeed. The other involves no conversion at all, but
rather simply using the reference according to its declared interface.
Michael said:lyallex said:]
So
public class MyClass implements Serializable{
public void makeStupidComment(){
System.out.println("My dog knows Java");
}
...
}
Serializable s = new MyClass();
s.getClass() //OK, comes from Object
s.makeStupidComment(); //Can't do this because serializable is higer
up the inheritence hierarchy.
You can't do this because MyClass and Serializable don't build a class
hierarchy.
Your example shows exactly the design issue: if a method uses a
Serializable but want to use methods from another type, there must be in
general a (implicit or explicit) cast to the desired type.
In your example the first call leads into a cast which is done implicit
by the compiler:
((Object)s).getClass();
Not quite true.
What you are describing is the Decorator Design Pattern.
This does not assist you in "working around" lack of multiple inheritance.
Single-inheritance, together with the Adapter Design Pattern, is what helps
you with that.
I'd draw a little UML diagram for you, but...
Roedy Green wrote
From all the possible reasons already mentioned I think that the best
candidate is that some designer(s) thought that the Serializable marker
interface was an implementation detail that had no place in the API
signatures. One could indeed argue that since supporting the
Serializable marker interface do not imply serializability alone, the
compile-time benefit of using Serializable in signatures will be small;
callers of a writeObject(Serializable) would still have to prepare for
getting a NotSerializableException (or equivalent).
My personal second guess will be that the API is like that because of
practical considerations at design time, like for instance a requirement
that the serialization mechanism should able to serialize instances of
classes that was designed before or independent of the mechanism itself.
Perhaps someone with very early Java API documentation can check whether
or not the
standard classes expected to be serializable implements
Serializable. They seem to do so in Java 1.1 which is the earliest
documentation I have. From my testing on the matter with Java 1.4.2 I
haven't been able to find a class that is supposed to be serializable
but which do not implement Serializable (in fact, I even learned that
arrays
actually are instances of Serializable).
Finally, I would like to remark that if the signature was
writeObject(Serializable), then for symmetry one could argue that the
signature for readObject should be Serializable readObject() and that
would mean you couldn't deserialize into non-serializable objects like
you can today.
You said:I just noticed that it is writeObject ( Object o )
rather than writeObject ( Serializable o )
how come?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.