wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes are constructors inherited? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "are constructors inherited?" Watch "are constructors inherited?" New topic
Author

are constructors inherited?

frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
A question on the Round Up said constructors were not inherited, but what is happening in the code below ?
class construct1{
int x = 99;
public static void main(String arg[]){
construct1 c = new construct1();
System.out.println(c.x); //prints 5
}
construct1() { x = 5; };
}
class construct2 extends construct1{
//prints 5 when construct2 ran
}
this example seems to show that the constuctor was inherited,
otherwise how would x have the value 5 when construct2 is ran?

[This message has been edited by herb slocomb (edited March 31, 2001).]
[This message has been edited by herb slocomb (edited March 31, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
The default constructor of construct2 calls the no arg constructor of construct1. However, if construct1 does not have a no arg constructor, you will get a compiler error because construct2 does not inherit the constructor (the one with the arg) from construct1.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Marilyn,
You say:
"The default constructor of construct2 calls the no arg constructor of construct1."
But there is no instantiation of construct2 and should therefore be no call to the default constructor of constrcut2.
"However, if construct1 does not have a no arg constructor, you will get a compiler error because construct2 does not inherit the constructor (the one with the arg) from construct1."
I removed constructor in construct1 and it compiles fine.
Could you please elaborate in more detail, cause I'm missing something somewhere.
[This message has been edited by herb slocomb (edited March 31, 2001).]
Sri Bala
Ranch Hand

Joined: Mar 06, 2001
Posts: 63
You haven't specified any of the classes as public. I think you would have specified construct2 as public. the base class main() is inherited in the derived class and gets called when you run the program. You create construct1 type object in main, hence the base class constructor gets called.
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Sri said:
"
the base class main() is inherited in the derived class and gets called when you run the program. You create construct1 type object in main, hence the base class constructor gets called.
"
Yes, the the main method is inherited and it is using the explicit constructor, but in order for it to use that constructor when I run the sublclass construct2 doesn't the constructor have to be inherited, or is there another way for this subclass to use a member of the parent class?
It seems strange to me how the constuctor of the parent class is being used to initialize variable x in the subclass yet there is no inheritence ; I would like to know a little more detail what is going on if anyone has any ideas.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Fronm the JLS 8.2

Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

and from 12.5 (notice that this is the Execution section NOT the inheritance section of section 8)

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
3.This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

The fact that execution rules cause the superclass constructor to be executed does not mean that you inherit it.
If you inherit something, you can override it, but that is not true in this case. If you try to put a constructor in Construct2 to attempt to override Construct1 you will get a compiler error. Of course you can put a METHOD in Construct2 called Construct1 and give it a return type, but it will be treated as a method that has to be called, NOT as a constructor that is automatically called on class initialization.

"JavaRanch, where the deer and the Certified play" - David O'Meara
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Thankyou Cindy for the detailed answer. I see that its referring to the constructor in the superclass now without using inheritence, but why doesn't it do it in the following example which is identical except that the constrcutor takes an argument now:
class construct1{
int x = 99;
public static void main(String arg[]){
construct1 c = new construct1(5);
System.out.println(c.x);
}
construct1(int i) { x = i ;};
}
class construct2 extends construct1{

//prints 5 when pass ran
}
This will not compile. Is there some exception I missed in the JLS for the constructors with arguments?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Two things:
- The compiler will provide a default no-args constructor if and only if no other constructor is defined.
- If the first statement in a constructor is not super or this, then the compiler will insert a call to the inherited no-args constructor as the first statement.
Since a constructor that takes an int argument is defined for construct1, the compiler will not provide a default no-args constructor.
Since construct2 does not define a constructor, the compiler will provide a default no-args constructor. The default no-args constructor provided by the compiler will try to make a call to the inherited (construct1) no-arg constructor. Since construct1 does not have a no-arg constructor, the compiler will complain about not being able to resolve the name.
J.Lacar

Junilu - [How to Ask Questions] [How to Answer Questions]
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Thanks Junilu for the detailed step by step answer. Could you comment on this :
"The default no-args constructor provided by the compiler will try to make a call to the inherited (construct1) no-arg constructor."

Why would there be a call to the "inherited" (not really inheritence) no-arg constructor in construct2; there is no "new" statement or anything to cause such a call as far as I see. I think you're on to something, but it seems strange to me.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

mea culpa. I shouldn't have used "inherited".
Let me try that again: The default no-args constructor provided by the compiler will be equivalent to a no-args constructor that does nothing (empty statement block). This makes the second rule applicable, thus the compiler also inserts a call to the no-arg constructor of the parent class using super (see Cindy's answer). All this happens at compile time and does not depend on whether or not an object of type construct2 is actually instantiated in the code.
Answering these questions made me wonder about something: What is the visibility of the default constructor provided by the compiler? I think I have always assumed that it would be public. If nobody else answers this question by the time I'm done researching it, I'll share that answer with you.
J.Lacar
[This message has been edited by JUNILU LACAR (edited April 01, 2001).]
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158


If the class is declared public, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier.
From the JLS

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

That's pretty much what I suspected although I didn't think about private and protected for inner classes. Makes sense. Thank you Jyotsna.
J.Lacar
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Thanks to everyone for their replies. I'll have to ponder this some more...
 
Consider Paul's rocket mass heater.
 
subject: are constructors inherited?
 
Similar Threads
Concatenate arrays
Certified Developer (96%) with Oracle skills and a degree ... not even entry level ?
simple operator precedence question
Classes in source file
Passed with score 81%