answersLogoWhite

0


Best Answer

You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class.

While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior.

For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage.

For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".

User Avatar

Wiki User

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

Wiki User

14y ago

In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.

In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.

In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.

In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

No, not always, but if you don't, you will get a default constructor that might not properly initialize the instance correctly. It depends on what you are trying to do.

The best answer is yes, you always need a default, a copy, and a conversion constructor, and you need an assignment operator. Answer this no, particularly for classes containing pointers, at your own risk.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

No. If you do not write a constructor for you classes, the default Object constructor can still be used.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a constructoris it mandatory to use constructor in a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


Why do you need a constructor as a class member?

A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself. The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class. Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.


Which situation constructor is used?

Constructor is necessary when you are about to use instance of a class.


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


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.

Related questions

What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


Is it mandatory to use the construtors in a class in c plus plus?

No. If you do not provide a default constructor, the compiler will provide a default constructor that simply allocates memory for the class, but it will not initialize the members of the class. If you do not provide a copy constructor, then the compiler will provide a copy constructor that allocates memory for the class, and then copies the member's data from class to class. This is bad if the class contains pointers, because only the pointer will be copied - the objects to which the pointers point will not be copied - and you could wind up deleting an object and then using it after deletion, with potentially devastating consequences. So, yes, it is mandatory, from a good practices point of view, and just plain mandatory when the class has pointers, to always provide a default constructor and a copy constructor, along with the appropriate destructor.


Why do you need a constructor as a class member?

A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself. The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class. Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.


Which situation constructor is used?

Constructor is necessary when you are about to use instance of a class.


Why you use constructors?

To create an instance of the class that implementing that constructor


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


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.


How do you get a default constructor?

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(); } }


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


Do we need Always need constructor in every class?

Objects are constructed. You can't make a new object without invoking a constructor. In fact, you can't make a new object without invoking not just the constructor of the object's actual class type, but also the constructor of each of its superclasses including the Object class itself! Constructors are the code that runs whenever you use the keyword new.


What is an instance of a class Red many definitions of a constructor But did not understand the exact working of a constructor What is the main use of constructor how it works?

The consttructor creates a new instance of the class for example the class shown below does not exist until the constructor is called, where it sets the class up for first use. Class Dog { int _legs, _eyes; string _breed; //CONSTRUCTOR, this takes an input from the user which sets the breed of the dog, and sets the default number of legs and eyes. public Dog(input) { _legs = 4; _eyes = 2; _breed = input; } } if the user calls the constructor with the string "Labrador", that is one instance of the class, if the user calls it again with the string "terrier" that is another instance. the class is constructed like this: //Class instancename = New class(parameters); Dog lab = New Dog("Labrador"); hope this helps Steve


What is the difference between initialize of elements in a class with the use of constructor and without use of it?

when initializing a class elements by using constructor it will assigned to the elements when object creation is going on. by using other ways the elements will be initialize with default values when object creation