Abstract Class Extenstions Not exactly working as I would expect.

  • Thread starter Michael Boocher via JavaKB.com
  • Start date
M

Michael Boocher via JavaKB.com

I ran into a strange initialization order that was in my mind, counter
initutive. What I am looking for is for anyone to perhaps offer an
explanation to this snipet of code.

############### InitializeTest.java ########################
public class InitializeTest extends TestAbstract {
private String TheData = "Default Value -- Why THIS?";
public static void main(String[] args){
InitializeTest IT = new InitializeTest("Some Default Value");
System.out.println(IT);
}
public InitializeTest(String InitialValue){
super(InitialValue);
}
public void ForwardCall(String pData) {
// Use to initialize Data.
TheData = pData;
}
public String toString(){
return TheData;
}
}

############### TestAbstract.java ########################
public abstract class TestAbstract {
public TestAbstract(String Data){
ForwardCall(Data);
}
public abstract void ForwardCall(String Data);
}


Now, I know why it is happening I understand that the constructor order is
a bit off. And that is why the code in the Extended class is being called
before the class is actually instantiated. But I wonder if this is
actually the intended result.

This is more of a cute dicussion thread than anything else.

Enjoy.
 
A

A. Bolmarcich

I ran into a strange initialization order that was in my mind, counter
initutive. What I am looking for is for anyone to perhaps offer an
explanation to this snipet of code.

[snip snippet]
Now, I know why it is happening I understand that the constructor order is
a bit off. And that is why the code in the Extended class is being called
before the class is actually instantiated. But I wonder if this is
actually the intended result.

The superclass constructor is invoked before the instance variables of a
class are initialized. The details are in the "Creation of New Class
Instances" section of the Java Language Specification at

http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670
 
J

John McGrath

I ran into a strange initialization order that was in my mind, counter
initutive. What I am looking for is for anyone to perhaps offer an
explanation to this snipet of code.
Now, I know why it is happening I understand that the constructor order
is a bit off. And that is why the code in the Extended class is being
called before the class is actually instantiated. But I wonder if this
is actually the intended result.

Yes, it is *exactly* the intended result. The Java Language Specification
spells out the process in detail:

http://tinyurl.com/bsdy3
A.K.A:
http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#4
4670

<nitpick>

Note that the superclass constructor is not called before the object is
instantiated. Instantiation is what occurs *before* the constructors are
called - allocation of memory and setting the member variables to their
default values (e.g. null, 0, false).

</nitpick>
 
T

Tim Ward

Michael Boocher via JavaKB.com said:
Yeah I figured that bit out. Scary that you know the docs that well...

This is a very well known design error to anyone who's played with Java for
a while ...

OK, so you'll be told "there's nothing wrong with the language design, just
don't call virtual methods in base class constructors". Yeah, right. Like
you get the choice - nobody seems to have told the authors of Swing this
rule, for example.
 
M

Michael Boocher via JavaKB.com

Documentation or not, it just doesn't seem inituitive. To me at least.
 
M

Michael Boocher via JavaKB.com

I would have expected.... that java created the Super class then the
derived. but Magically have called the Initializers of the derived class
before the constructor of the super class Calls Forward.

Its ugly. I know, but thats what I would have expected.
 
J

John McGrath

I would have expected.... that java created the Super class then the
derived. but Magically have called the Initializers of the derived class
before the constructor of the super class Calls Forward.

Its ugly. I know, but thats what I would have expected.

If you do that, you can have the subclass using methods and fields of the
superclass before it has been initialized properly. Take the following
code:

class Base {
Dimension size;
public Base() {
size = new Dimension( 10, 20 );
}
public int getWidth() {
return size.width;
}
}

Assuming that initialization order were used with the above example, if
getWidth() were called from a subclass initializer, you would get an NPE,
since the Base() constructor would not have been called yet.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top