Because when you create an object in java a constructor of each extending
type in the hierarchy is called.
If there is no explicit constructor called (at first line in your
constructor, the default (emptyargs) constructor is called automatically. 

Example: 
Object -> A -> B
If you do something like B b = new B(); following happens:

B() is called 
A() is called
Object() is called
Object() is finished
A() is finished 
B() is finished, call returns.

You can simply try it out with following objects:

class A{
        int foo;
        public A(int aFoo){
                foo = aFoo;
        }
} 

class B{
        public B(){
        }
}

class B will not compile, because it needs A to have a no args constructor.
If you change the constructor of B to do:
public B(){
        super(123);
}
It would compile, because since you explicitely called the constructor no
automatic call to an empty constructor of the superobject is needed anymore
(this call is generated by javac by the way).


Regards
Leon

> -----Ursprüngliche Nachricht-----
> Von: Yaakov Chaikin [mailto:[EMAIL PROTECTED] 
> Gesendet: Mittwoch, 29. Juni 2005 23:34
> An: Struts Users Mailing List
> Betreff: Re: [OT] Serialization and no-arg constructor. Is it needed?
> 
> On 6/29/05, Laurie Harper <[EMAIL PROTECTED]> wrote:
> > Yaakov Chaikin wrote:
> > > Here is the line from the spec:
> > > A Serializable class must do the following:
> > > .....
> > > Have access to the no-arg constructor of its first 
> non-serializable 
> > > superclass ....
> > >
> > > What does this mean and why do you need this requirement? But it 
> > > does
> > 
> > It means that if you have an inheritance hierarchy and any class in 
> > that hierarchy implements serializable, that class's parent must 
> > provide a no-arg constructor. That's necessary so that the 
> > serializable class can be instantiated by the 
> deserialization process.
> 
> Yes, I understand the obvious. How does the deserialization 
> use the no-arg constructor of the parent? Why wouldn't you 
> need the no-arg constructor of this class itself (assuming it 
> would call the
> newInstance() method)? Why "first non-serializatible superclass"?
> 
> This is what I am really asking.
> 
> Thanks,
> Yaakov.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to