answersLogoWhite

0


Best Answer

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this:

public class NoConstructorClass{

//no constructor goes here

}

Then you get something like this:

public class NoConstructorClass{

public NoConstructorClass(){ // Default constructor that you didn't write

super();

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

if we write a code in java then we don't mansion a default constructor in it then compiler by default provide a default constructor and the main impotent role of default constructor in our program is that it initialize the member of the class then it is reason compiler provide a default constructor in our java program code .

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A default constructor is generated for a class if we did not specify any constructor for it. A default constructor is a public constructor that takes no arguments.

For example:

public class Test {

}

The compiler will turn this into:

public class Test {

public Test() {

...

}

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

You are free to override the default constructor whenever you like. In fact, if you want to keep the default constructor when you also need a parameterized constructor, you have to override the default constructor in order to use it. Once you've written the parameterized constructor, the default constructor goes away.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A default constructor is a constructor that takes no arguments.

Here's a sample:

class c

{

int a,b;

c() //Constructor 1

{

a=2;

b=1;

}

c(int x, int y) //Constructor 2

{

a=x;

b=y;

}

};

Here, Constructor 1 is called the default constructor, while Constructor 2 is a parameterized constructor.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor)

Determine Whether a Default Constructor Will Be Created

The following example shows a Lamborghini class with two constructors:

class Lamborghini {

Lamborghini() { }

Lamborghini(String name) { }

}

Will the compiler put in a default constructor for the class above? "No!" What about for the following variation of the class?

class Lamborghini {

Lamborghini(String name) { }

}

Now will the compiler insert a default constructor? "No Again!" What about this class?

class Lamborghini { }

Now we're talking. The compiler will generate a default constructor for the preceding class, because the class doesn't have any constructors defined. OK, what about this one below?

class Lamborghini {

void Lamborghini() { }

}

You might be tempted to say that, the compiler won't create one, since there already is a constructor in the Lamborghini class? Take another look at the Lamborghini class.

What's wrong with the Lamborghini() constructor? It isn't a constructor at all! It's simply a method that happens to have the same name as the class. Remember, the return type is a dead straight way to tell us that we're looking at a method, and not a constructor. So, here again the compiler will put the default no-arg constructor in the class.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Default constructors are useful in those situations where the object need not be initilaised before it is used. The instance variables may be set later on using get and set methods.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Default constructor in Java initialises all instance variables with default values.

class program {

int a; // default constructor initialises a to 0

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Default constructors is a term having to do with computer programming. A default constructor refers to a constructor that is automatically generated when an explicit constructor is missing.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you get a default constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


How is the default constructor equivalent to a constructor having default arguments?

Any constructor that can be invoked without explicitly passing any arguments is a default constructor. Note that there can be only one default constructor so there can only be one constructor where all arguments have default values or one constructor that has no arguments, but not both. A constructor where all arguments have default values is a useful means of combining two or more constructors into a single default constructor, thus reducing verbosity and code duplication.


When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


What is an empty constructor?

An empty constructor takes no arguments and calls the default constructor


What is the difference between default constructor and parameterized constructor?

A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.

Related questions

Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


What is a default parameterized constructor?

There is no such thing as a default parameterized constructor. The default constructor is always the 'no-arg' constructor and does not take any parameters or arguments as input


How is the default constructor equivalent to a constructor having default arguments?

Any constructor that can be invoked without explicitly passing any arguments is a default constructor. Note that there can be only one default constructor so there can only be one constructor where all arguments have default values or one constructor that has no arguments, but not both. A constructor where all arguments have default values is a useful means of combining two or more constructors into a single default constructor, thus reducing verbosity and code duplication.


When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


What is another name of default constructor?

No-Arg Constructor


What is an empty constructor?

An empty constructor takes no arguments and calls the default constructor


What is the difference between default constructor and parameterized constructor?

A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.


Why Java always provides a default constructor to class when you use another constructor default constructor remove?

Classes in Java inherit constructors from their parent classes. If you don't explicitly define a parent class, then Object is used, which has only the default empty constructor. That "default" constructor is only there when defined by the parent class, so classes which do not have a no-argument constructor will not allow subclasses to automatically use it. This is implemented this way because of the special nature of constructors. Java could not always provide a default constructor because it could not guarantee that all class members would be properly created or initialized.


What is first call in struts2 default constructor or validate method?

Default Constructor will be called first . If you override Validate method , then validate method will be called .


Do you have to declare a constructor every time you create a class?

Yes, If you don't a default constructor will be created for you.


What is the general syntax of a default constructor?

There is no such thing as 'the general syntax of a default constructor'. A default constructor is one that is generated by the compiler if you don't specify any constructors at all. Semantically speaking, it is public and takes no arguments, but this is not 'syntax'.


Using default constructor in inheritance and in what reason it throws error?

The compiler places a default no-arg constructor in any java class that does not have an explicit constructor coded into it. for ex: public class Car { ... ... //lots of code but no constructor } In the above case, the compiler will place the below constructor into the code: public Car() { super(); } But, if you have a constructor in your class that takes arguments then the compiler will not put the default constructor. Ex: public class Car { public Car(String name){ ... } ... //lots of code } Above, we have a Car constructor that takes a string name as argument. so, the compiler wont put the default constructor in the code. now, if you try to do: Car obj = new Car(); you will get an error because this constructor is not defined.