answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: Can constructor have one or more parameters?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you overload the constructor in java true or false?

Yes, you can have more than one constructor with a different set of parameters.


How many kinds of constructor in java?

Java has 2 types of constructors based on parameters passed:Default or parameter-less constructor: A constructor which does not accept any arguments.Parametrized constructor: A constructor which accepts one or more arguments.Similarly based on Access modifier also we have:Public constructor - Class can be instantiated by anyonePrivate constructor - Class cannot be instantiated by anyoneProtected constructor - Class can be instantiated only by sub classes


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.


A default constructor has how many parameters?

None, zero. Example: new MyClass(); //and public classMyClass{} no constructor defined inside of MyClass


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).

Related questions

Can you overload the constructor in java true or false?

Yes, you can have more than one constructor with a different set of parameters.


How many kinds of constructor in java?

Java has 2 types of constructors based on parameters passed:Default or parameter-less constructor: A constructor which does not accept any arguments.Parametrized constructor: A constructor which accepts one or more arguments.Similarly based on Access modifier also we have:Public constructor - Class can be instantiated by anyonePrivate constructor - Class cannot be instantiated by anyoneProtected constructor - Class can be instantiated only by sub classes


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


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.


A default constructor has how many parameters?

None, zero. Example: new MyClass(); //and public classMyClass{} no constructor defined inside of MyClass


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).


What is a constructoris it mandatory to use constructor in a class?

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".


What is the need of parameterized constructor?

Parametrized constructors are used to increase the flexibility of a class, allowing objects to be instantiated and initialised in more ways than is provided by the default and copy constructors alone. If you define any parametrized constructor, including a copy constructor, you will lose the default constructor generated by the compiler and must declare your own if you need one. The default constructor can also be parametrized, but each parameter must include a default value in the declaration, so that it can be called without any parameters.


Can you have more than 1 constructor in a class?

Yes. At least in Java, that's possible, as long as the constructors have a different number, or different types of, parameters.


How will you overload a constructor?

The same way you overload any function -- you provide a new signature for it. The signature is determined by the number and type of parameters it accepts, and whether they are const or not. Every class has a public default constructor if no other constructor is declared . Also every class has a public copy constructor whether you declare one or not. So every constructor you do declare is, in fact, an overloaded constructor. Example: class myClass { public: myClass():myInt(0){} // default ctor (no parameters) myClass(const myClass & copy){myint = copy.myInt;} // copy ctor myClass(int newInt):myInt(newInt){} // overloaded ctor private: int myint; };


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.


What is parameterised constructor?

A parameterized constructor is one that takes multiple arguments/parameters as input. Ex: let us say we want to create multiple constructor for a class Test Public class Test { Public Test() { //code } Public Test(int vals) { //code } Public Test(String val) { //code } }