Is calling a default constructor mandatory

Posted on 2011-04-22
Last Modified: 2012-08-13
I have two classes. One class extends the other. In the subclass, I have two constructors. One which is the default constructor and the other which takes a String parameter . When I instantiate the class with a string parameter, it is calling both the constructors. I am pasting my code
//Code 1
public class Building {
		Building(String name)
			System.out.println("BN...  "+name);

//Code 2

public class Ch1 extends Building{
	static String name="Prasad";
	Ch1(String name)
	public static void main(String[] args)
		new Ch1("XX ");


Open in new window

Question by:prsubject
    LVL 47

    Expert Comment

    No it is not mandatory - you don't need to call default constructor
    within teh constructor which takes the string as parameter
    LVL 47

    Expert Comment


    Within the subclass you can call
    the constructor of your superclass using operator


    as you superclass has bot constructors,

    but youll cal only one of the two

    and super opertaort shoudl always be
    the very first operator in the
    constructor of the subclass


    Author Comment

    As You said I replaced the this() method in the subclass with super() method. Two Situations have arisen

    Situation 1:

    When I placed super() --> It was calling the default constructor of the super class --> Fine

    Situation 2:
    When I placed the first line with a parameterized constructor, super(name),  It was calling both the default and the parameterized constructor of the super class.

    //Output for second situation
    BN...  XX
    LVL 47

    Accepted Solution

    Correct, this because in you Buliding(String) constructor
    you called this().

    In fact this is not necessary.

    Say in normal case if say I have one instance variable,
    say String in the class and i amy have two constructors
    in this class one would be with parameter andother would be

    public class MyClass {

    String myString;

    public MyClass(){}

    public MyClass(String s){
    myString = s;


    In this case there is no need within constructor with
    parameter to call default constructor.
    It actually does not add anything.

    If you contructors are defined in such a way,
    that default constructor without any
    parameters instializes say five strings

    and constructor with parameters intializes three more parameters,
    then you may call form the
    second constructor the fiorst one using
    so that you would not need to repeats those five lines where you initialize
    those first five parameters


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now