answersLogoWhite

0


Best Answer

public class Test {

public Test() {

…

}

public Test(int i) {

…

}

public Test(String s) {

…

}

}

In the above example we have three constructors for the class Test. One of them has no arguments, one has an integer argument and one has a string argument. This is how multiple constructors are organized inside a class.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a sample of multiple constructor in a single class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Whati is Constructor overloading?

Constructor overloading is the feature by which we declare multiple constructors for a single class. 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 } }


Is there a sample program for constructor?

All Java programs would have a constructor... public class Test { public Test(){ ... } ..... } This is a constructor. Even if you dont code the constructor Java would automatically place a default constructor for compilation.


How do you call multiple constructors with single object?

You can have any number of constructors for a class. All we need to do is implement constructor overloading. 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 } }


Can you call a constructor from another if a class has multiple constructors?

Yes. All you need to do is to specify the correct number of arguments to invoke the correct constructor.


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 } }


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


Why base class constructor is first invoked in subclass constructor?

The base class constructor is invoked first when a subclass is instantiated, because the base class must be viable and consistent before the subclass constructor is fired.


How can you recognize a constructor in a class?

A class's constructor will have the same name of the class and no return type (not even void): class Example(){ Example() {printf("This is the constructor\n");} ~Example(){printf("This is the destructor\n");} };


How do you call an abstract class from main method?

An abstract class cannot have a constructor and hence you cannot invoke the constructor of the class - i.e., you can instantiate an abstract class and hence you cannot call the constructor of an abstract 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.


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


Which situation constructor is used?

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