• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

are constructors inherited?

 
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone for their replies. I'll have to ponder this some more...
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic