• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Is calling a default constructor mandatory

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()
		{
			System.out.println("B");
		}
		
		Building(String name)
		{
			this();
			System.out.println("BN...  "+name);
		}
	
}

//Code 2

public class Ch1 extends Building{
	static String name="Prasad";
	Ch1()
	{
		System.out.println("Ch1...");
	}
	
	Ch1(String name)
	{
		this();
		System.out.println("Ch1.."+name);
	}
	
	public static void main(String[] args)
	{
		new Ch1("XX ");
	}
}

//Output
B
Ch1...
Ch1..XX

Open in new window

0
prsubject
Asked:
prsubject
  • 3
1 Solution
 
for_yanCommented:
No it is not mandatory - you don't need to call default constructor
within teh constructor which takes the string as parameter
0
 
for_yanCommented:

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

or
super(String)

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


0
 
prsubjectAuthor Commented:
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
B
BN...  XX
Ch1..XX
0
 
for_yanCommented:
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
without:

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
this()
so that you would not need to repeats those five lines where you initialize
those first five parameters




0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now