• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Constructor usage in superclasses

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to come to grips with constructor methods and how they are implicitly used in Java. Consider the following scenario:
Class C extends Class B, which extends class A. I then create an instance of C, and C's constructor method C() executes.
Now, when C's constructor method C() executes...
1) Are the B() and A() constructor methods implicitly invoked?
2) In which order are these superclass constructors invoked?
3) What if Class B does not have a B() constructor method? (Will this compile/run?)
4) If C was instantiated with an argument(say x), which invoked C's C(x) constructor, would the A() and B() constructors still be implicitly invoked, or would Java look for matching-signature constructors in A and B?
Is it standard to for every Java class to have a Class() constructor method? Is this enforced, or is it just bad practice not to have one?
Whew! Just asking all these questions wiped me out. Will appreciate any input on the above
Mark.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
1.) Yes. Superclass default constructors are implicitly invoked at the beginning of the current class's constructor, unless a specific superclass constructor is exlicitly invoked, or unless another constructor in the same class is explicitly invoked (i.e. this() ). Note: explicit calls to other constructors must be the first thing in your constructor.
2.) B() then A()... then all superclasses of A all the way up to Object() in order.
3.) Okay... if the B() constructor does not exist because no constructors at all exist in B this will work, because java creates the default constructor for you if you supply no constructor. However, if you define a constructor like B( int x ) and define no B() constructor, then Java will not automatically define one for you, and you will get a run-time error if you try to explicitly or implicitly access B().
4.) No... unless you explicitly call super(x), the default constructor (i.e. super() ) will be called.
Each java class does not have to have a constructor... if none are provided Java will create an empty default ( i.e. ClassName() ) constructor for you... but it's bad practice not to have one (at least in my opinion) for most classes...
\
Answering all these questions tired me out too... -
/
HTH,
-Nate
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Yes, they are implicitly invoked. You can also use super(args) to explicitly invoke them.
2) The constructors begin at the top of the hierarchy. In your example, the order would be A,B,C.
3) For classes that don't have any constructors, a default constructor with no arguments is created by the compiler.
4) Unless you explicitly call the constructor with methods using super(args), the default constructor will be used.
The following code helps explain your questions:
<code>
public class ConstructorTest {
public static void main(String[] args) {
C test = new C();
C test2 = new C(5);
}
}
class A {
public A() {
System.out.println("constructing A");
}

public A(int x) {
System.out.println("constructing A with " + x);
}
}
class B extends A {
public B() {
System.out.println("constructing B");
}
}
class C extends B {
public C() {
System.out.println("constructing C");
}

public C(int x) {
System.out.println("constructing C with " + x);
}
}
</code>
The output of the program is:
constructing A
constructing B
constructing C
constructing A
constructing B
constructing C with 5
Not every class has to have a Class() constructor. In some cases, it makes more sense to have only constructors that accept arguments. I personally consider it bad programming not to have *any* constructors, just because it makes it less clear what happens when the object is initialized.
[This message has been edited by Angela Ann (edited February 22, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that the order includes the initializers.
It would be
static initializer of A
initializer block of A
constructor of A
static initializer of B
initializer block of B
constructor of B
static initializer of C
initializer block of C
constructor of C
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic