answersLogoWhite

0


Best Answer

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

}

}

User Avatar

Wiki User

โˆ™ 2012-04-07 19:21:35
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: What is the method of constructor overloading?
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


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.


Differene betwee constructor overloading same as method overloading?

A constructor is just a special form of a method. You can overload constructors in the exact same way as you can overload any other method.


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.


Difference between overloading and overwriting a constructor?

Overloading constructor means when you have multiple constructors in a class but with different number of arguments. We cannot override a constructor, but we can override a method having same arguments and same return type. This means when I subclass a particular class then I can call the superclass's class method and override that with subclass's own logic.


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


Is method overloading is polymorphism?

Yes. Method Overloading is a form of Polymorphism


What is method overloading and constructor overloading explain with?

method overloading means function having same name but different prototype .. e.g class demo { int i; public void display() { System.out.println("hello"); } public void display(int x) //method overloading { i=x; System.out.println("i="+i); } } class mainDemo { public static void main(String s[]) { demo ob=new demo();//creating object ob.display(); ob.display(5); } }


Discrbe breifly about Copy constructor overloading?

A copy constructor usually refers to a constructor which takes an object, and returns a copy of that object. I can think of no way to overload the constructor without changing its functionality.


What is constructor overloading?

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.

People also asked