answersLogoWhite

0


Best Answer

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples:

class Car {

Car() { }

Car(String s) { }

}

The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example.

Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name.

User Avatar

Wiki User

βˆ™ 2011-12-20 06:41:49
This answer is:
User Avatar
Study guides

Mixture

1 card

Class

➑️
See all cards
4.36
β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…
22 Reviews

Add your answer:

Earn +20 pts
Q: Why constructor overloading is useful in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the Differences between constructor overloading and method overloading in java?

really there is no difference between constructor overloading and metho overloading


How many constructor can be defined in a classin java p rogramming?

how many constructer can be defined in class in overloading of java programming


What is the function of this in java?

this in java is a keyword that refers to the current object of the class. It is also used in constructor overloading when you want to invoke one constructor from another within the same class.


Is constructor overloading a polymorphism concept?

I think constructor overloading is the concept of polymorphism.


Why you use constructor overloading?

When we are initializing our object with different internal state then we can use the constructor overloading.


Why opreator overloading is not included in Java Programming Language?

Operator overloading was a feature of C++ that the Java language designers thought was too complicated and not useful enough to include.


Is constructor overloading same as method overloading.yes or no. explain?

yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.


Difference between method overloading and constructor overloading?

There is no difference between them.


Operator overloading is possible in java or not?

Operator overloading is not possible in Java.


What is constructor overloading with example in java?

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments


Does Java support copy constructor?

No. Java does not support copy constructor


What is the method of constructor overloading?

The first thing to note about constructor overloading is that Java creates a no argument constructor for you if and only if you have not typed a constructor yourself. Every class has a constructor even abstract ones (default no argument constructor). Abstract constructors are always executed. To overload a constructor you can do the following: class Test { String name; Test(String n) { name = n; System.out.println("Constructing Test Object named: " + name); } } In the case above we are overloading the default no argument constructor with a constructor that takes a String parameter. You can write you own no argument constructor as follows: class Test { Test() { System.out.println("Constructing Test Object"); } } To override our own no argument constructor we do this: class Test { Test() { // our no argument constructor System.out.println("Constructing Test Object"); } String name; Test(String n) { // overloading our no argument constructor with this // constructor that take a String parameter name = n; System.out.println("Constructing Test Object named: " + name); } }

People also asked