answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you have more than 1 constructor in a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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


Characteristics of constructor?

Contains an access modifier followed by the name of the class and some parameters. More specifically: public class MyClass { //Constructor public MyClass() { } }


What are the three parts of simple empty class?

Three parts of a simple empty java class are; A simple empty class of java supports legacy, as it automatically acquires object class, therefore can produce the functionality described in the object hierarchy. It automatically generates a fault constructor inside the class with a blank description. It also accompanies polymorphism, as can be practiced in any scheme by the extensive class.


Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.


1 Explain the concepts of constructor and destructor Do you have to declare a constructor every time you create a class?

You only need a constructor if the default constructor will not suffice. Often times, it is useful to have a constructor that takes common parameters so that you do not have to write additional code. For example, a Point class might have a constructor for Point(int x, int y), which would be a shortcut for assigning x and y independently. Other classes may not need any default values assigned, and for this, it is acceptable to just use the default constructor. Finally, some classes are virtual, static, or abstract, and so may not need a constructor because the constructor is unnecessary (static), or may be defined elsewhere (virtual, abstract).

Related questions

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


Characteristics of constructor?

Contains an access modifier followed by the name of the class and some parameters. More specifically: public class MyClass { //Constructor public MyClass() { } }


MA of a first class lever?

Class-III Lever . . . MA always less than 1. Class-II Lever. . . . MA always more than 1. Class-I Lever . . . . MA can be 1, more than 1, or less than 1.


What are the three parts of simple empty class?

Three parts of a simple empty java class are; A simple empty class of java supports legacy, as it automatically acquires object class, therefore can produce the functionality described in the object hierarchy. It automatically generates a fault constructor inside the class with a blank description. It also accompanies polymorphism, as can be practiced in any scheme by the extensive class.


Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.


How do we invoke a constructor?

In Java, objects are constructed. Every time you make a new object, at least one constructor is invoked. Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you. Ex: class Test { public Test() { } // this is Test's constructor public void Test() { } // this is a badly named, // but legal, method } If you see the example above, you would have realized that the constructor looks a lot like methods. Below are the main distinguishing factors between the constructor and normal methods: 1. The Constructor's name is exactly the same as the name of the class 2. They do not have a return type (Please remember this. A Constructor cannot have a return type as part of the code) 3. Constructors cannot be static, abstract or final


1 Explain the concepts of constructor and destructor Do you have to declare a constructor every time you create a class?

You only need a constructor if the default constructor will not suffice. Often times, it is useful to have a constructor that takes common parameters so that you do not have to write additional code. For example, a Point class might have a constructor for Point(int x, int y), which would be a shortcut for assigning x and y independently. Other classes may not need any default values assigned, and for this, it is acceptable to just use the default constructor. Finally, some classes are virtual, static, or abstract, and so may not need a constructor because the constructor is unnecessary (static), or may be defined elsewhere (virtual, abstract).


Can you override the constructor?

A constructor should be overloaded when there is a parameter with default value that is commonly used, or if it is possible that not all the information for creating the instance will be gathered all at once. As an example, say there is a class that describes bank accounts. Assuming I can choose to deposit or not deposit money under my name upon creating an account, there is a chance for a meaningful overload. One constructor could take in the name of the account owner and the initial balance as parameters; the overloaded constructor could just take the name as a parameter while setting the initial balance to zero.


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }


Can a lever be more than one class?

Yes, a hammer can be both a class-1 and a class-3 lever. Although, not all levers can be more than one class only some. For example, a hammer and a clothespin.


In what order are the class constructors called when a derived class object is created?

We know that constructors are invoked at runtime when you say new on some class type as follows: Lamborghini h = new Lamborghini(); But what really happens when you say new Lamborghini() ? (Assume Lamborghini extends Car and Car extends Object.) 1. Lamborghini constructor is invoked. Every constructor invokes the constructor of its superclass with an (implicit) call to super(), 2. Car constructor is invoked (Car is the superclass of Lamborghini). 3. Object constructor is invoked (Object is the ultimate superclass of all classes, so class Car extends Object even though you don't actually type "extends Object" into the Car class declaration. It's implicit.) At this point we're on the top of the hierarchy. 4. Object instance variables are given their explicit values. By explicit values, we mean values that are assigned at the time the variables are declared, like "int x = 27", where "27" is the explicit value (as opposed to the default value) of the instance variable. 5. Object constructor completes. 6. Car instance variables are given their explicit values (if any). 7. Car constructor completes. 8. Lamborghini instance variables are given their explicit values (if any). 9. Lamborghini constructor completes.


What are the rules on how to construct java constructor?

1. The constructor has to have the same name as the classthat it is in.2. It does not have a return type. If it has a return type, then it is a method (even though it is legal, it's not ideal to have name a method the same name as the class).3. It can use any access modifier (this includes private).4. The default constructor does not take arguments.5. The first statement in a constructor has to have a super() type or this() type. If this is not written, by default, it's super(). It's illegal to have it in any other line other than the first line.6. Constructors can only access static variables.7. Only constructors have access to another constructor.Remember that interfaces do not have a constructor.