• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Constructor Rules

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little confused about when a parent class needs a no argument constructor and when it doesn't.
In the following code, I've got a subclass which calls a constructor from the parent class with an argument. The parent class does NOT have a no argument constructor and all is well.
//Demo.java
public class Demo {
static char x = 'p';

public Demo(char x){
System.out.println("Calling Demo's constructor with " +x);
}
}
//DerivedDemo.java
public class DerivedDemo extends Demo {
int m, n;
static char x;

public DerivedDemo(char ch){
super(ch);
x = ch;
}
public static void main(String args[]){
DerivedDemo dd = new DerivedDemo('c');
System.out.println("The Derived Demo value is "+x);
}

}

Now I've added an additional constructor only to the subclass and now I've got the compilation error: "No constructor matching Demo() found in Demo".
//Demo.java ==> same as above
//DerivedDemo.java ==> added another constructor.
public class DerivedDemo extends Demo {
int m, n;
static char x;
//new constructor
public DerivedDemo(int x, int y){
m=x; n=y;
}

public DerivedDemo(char ch){
super(ch);
x = ch;
}
public static void main(String args[]){
DerivedDemo dd = new DerivedDemo('c');
System.out.println("The Derived Demo value is "+x);
}

}

If I add a no argument constructor to Demo it works fine. Why?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vasha,
In the first case, when the derived demo calls super(ch), the super class has a constructor for that Demo(int x)?. There is no need for a default constructor because there are no implicit calls like super() in the subclass constructor.
In the second case, when you add DerivedDemo(int x, int y), if you do not specify a super call in a constructor, the compiler automatically calls super() which is the default constructor. Hence the error. In the second case, if you had a Demo(int x, int y) and in the subclass if called super(x,y), it would have worked fine. I hope it makes sense.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remember following rules...
If you do not explicitly create a Constructor on a class a default No Arg constructor IS created
If you do create a customized Constructor with a Arg/Args, the default no arg will NOT be created
IF you create a Constructor in SubClass with or without arg, a call to super() ( the default no arg constructor of the parent) IS IMPLICITLY called unless you explicitly call super(args).
When you explicitly call a Parents constructor in the childs constructor it has to be the first line of code.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Rajesh. One minor addition:
IF you create a Constructor in SubClass with or without arg, a call to super() ( the default no arg constructor of the parent) IS IMPLICITLY called unless you explicitly call super(args) or this(args). In the latter case, follow the this() to the other constructor to determine what sort of superclass constructor chaining eventually occurs..
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic