answersLogoWhite

0


Best Answer

Constructor overloading is similar to method overloading. You can overload constructors by changing the parameter list in the class definition.

Here is an example

public class Box{

private int height;

private int width;

private int breadth;

// First constructor with no parameters

Box(){

height = 0;

width = 0;

breadth = 0;

}

// This is the second overloaded constructor

Box(int h,int w,int b){

height = h;

width = w;

breadth = h;

}

public void display(){

System.out.println("Height: "+height+" Width: "+width+" Breadth: "+breadth);

}

public static void main(String args[]){

Box obj = new Box(1,2,3);

obj.display();

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to demonstrate constructor overloading?
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(); } }


Is can every class has at least one constructor?

As many as you care to write.


What is the difference between implicit and explicit Java programming?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor. Explicit is opposite to this , ie. programmer has to write .


What is overloading constructor in java?

This is when you overload the constructor so that there isn't just one way you can initialize the class objectAssume you have a class called Foo and you want two different constructorsFoo(int a, int b);Foo(float a, float b);using a method like this can make your classes more dynamic and you don't need to to write a new class to handle different types. Sorry for the lack of Java code, i program in C++ but it you can overload functions and constructors in C++ too.


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

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


Write a program in c plus plus with constructor?

// constructor program to add two number's // program written by SuNiL kUmAr #include<iostream.h> #include<conio.h> class constructor { private: int a,b; public: constructor(int m,int n); int sum(); }; constructor::constructor(int m,int n) { a=m; b=n; } int constructor::sum() { int s; s=a+b; return (s); } int main() { int x,y; clrscr(); cout<<"enter two number's to add \n"; cin>>x>>y; class constructor k (x,y); cout<<"sum of two number's is = "<<k.sum(); getch(); return (0); }


Write java program for addition of two objects?

May be link might help -> http://www.allinterview.com/viewpost/408298.html There it is implemented through...... Operator Overloading!


Can you write own constructor in c plus plus?

Yes.


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


Write a C program called MonthDays to find the number of days in a given month Test your code with different input values to demonstrate that your function is robust?

Write a C program called MonthDays to find the number of days in a given month Test your code with different input values to demonstrate that your function is robust?


C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


What is the diffferent between method and constructor?

Both are functions, i.e., places where you can write code. A constructor is simply a special method that is invoked automatically when an object is created.


Is can every class has at least one constructor?

As many as you care to write.


What is the difference between implicit and explicit Java programming?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor. Explicit is opposite to this , ie. programmer has to write .


What is overloading constructor in java?

This is when you overload the constructor so that there isn't just one way you can initialize the class objectAssume you have a class called Foo and you want two different constructorsFoo(int a, int b);Foo(float a, float b);using a method like this can make your classes more dynamic and you don't need to to write a new class to handle different types. Sorry for the lack of Java code, i program in C++ but it you can overload functions and constructors in C++ too.


Why do you use constructors?

A constructor lets you write code that will be executed when the class is instantiated with the "new" keyword.