answersLogoWhite

0

Constructor overloading in java

User Avatar

Wiki User

โˆ™ 2011-02-17 12:29:35

Best Answer

WHAT IS A CONSTRUCTOR

"It is a special type of method same name as class name that determines how an object is initialized when it's created".

what is constructor in java

Like other methods, we can also define constructor Method in our java program but unlike other methods, we cannot call a constructor directly; Java called constructor automatically when an object has created. When we use new keyword to create an object of a class, java does three thing;

Allocates memory for the object.

Initialize that object instance variable, with their initial value or to a default.

Call the constructor Method of the class.

If a class doesn't have defined any constructor method, we will still create an object of that class but we have to set instance variable or call other methods that object needs to initialize itself to that object afterward.

By defining constructor method in our own classes, we can set initial values of instance variable, call method based on those variable or call methods on other objects, or calculate initial properties of our object. We can also overload constructor, as we would regular methods, to create an object that has specific properties based on the argument we give to new.

BASIC CONSTRUCTOR

by defining a constructor looks like a regular method, with 2 basic difference.

Constructor and class name are always same.

It doesn't have any return type

For example, in the below table a simple class person, with a constructor that initializes it's instance variable based on the argument to new. The class also includes a method for the object to introduce itself, and a main() method to test each of these class.

class Person

{

String name;

int age;

Person (String n, int a)

{

name = n;

age = a;

}

void printPerson ()

{

System.out.print("Hi, I am " +name);

System.out.println(" I am "+ age + " years old.");

}

public static void main(String args[])

{

Person p;

p = new Person ("Ajab", 20);

p.printPerson();

p = new Person ("Rizwan", 30);

p.printPerson();

The output of the program is given below:

Hi, I am Ajab. I am 20 years old.

Hi, I am Rizwan. I am 30 years old

CONSTRUCTOR OVERLOADING

like other methods, constructor can also take different number and types of parameters, enabling us to create our objects with exactly the properties you want it to have, or for it to be able to calculate properties from different kinds of input.

constructor overloading in Java

For example, the MyRectone class in the given table creates a MyRectone Constructor and passing different parameter instead of creating different methods for the given arguments.

class MyRectone

{

int x1 = 0;

int y1 = 0;

int x2 = 0;

int y2 = 0;

MyRectone ( int x1, int x2, int x2, int y2)

{

this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

}

MyRectone (Point topLeft, Point bottomRight)

{

x1 = topLeft.x;

y1 = topLeft.y;

x2 = bottomRight.x;

y2 = bottomRight.y;

}

MyRectone ( Point topLeft, int w, int h)

{

x1 = topLeft.x;

y1 = top left.y;

x2 = ( x1 + w);

y2 = (y1 + h);

}

void printRect ()

{

System.out.print ("MyRectone: ");

}

public static void main (String args [] )

{

MyRectone rect;

System.out.println ("Calling MyRectone with coordinates 35,35 70,70");

rect = new MyRectone (35,35,70,70);

rect.printRect();

System.out.println ("Calling MyRectone with coordinates (15,15) (30,30)");

rect = new MyRectone (15,15,30,30);

rect.printRect();

System.out.print (" Calling buildRect w/1 point (10,10),");

System.out.println ("width (50) and height (50)");

rect = new MyRectone ( new Point (10,10), 50, 50);

rect.printRect();

Output

Calling MyRectone with coordinates 35,35 70,70:

MyRectone: 

Calling buildRect w/1 points (15,15), (30,30):

MyRectone: 

Calling buildRect w/1 point (10,10), width (50) and height (50):

MyRectone:

CALLING ANOTHER CONSTRUCTOR

Some constructor may be a superset of another constructor defined in your class; that is, they might have the same behavior plus a little bit more. Rather than duplicating identical behavior in multiple constructor Methods in our class, it makes sense to be able to just call that first constructor from inside the body of the second constructor. Java provides a special syntax for doing this. To call a constructor defined on the current class, use this form:

this (arg1, arg2, arg3 โ€ฆ..);

The arguments to this are, of course, the arguments to the constructor.

User Avatar

ajabservices

Lvl 2
โˆ™ 2021-07-15 05:45:41
This answer is:
User Avatar
User Avatar

Amie Smitham

Lvl 1
โˆ™ 2021-07-17 16:05:37
great answer, thx
Study guides

Mixture

1 card

Class

โžก๏ธ
See all cards
4.35
โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…
23 Reviews
More answers
User Avatar

Wiki User

โˆ™ 2011-02-17 12:29:35

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Constructor overloading 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.


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.


Does Java support copy constructor?

No. Java does not support copy constructor


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


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


What is an operator overloading in java?

Operator overloading refers to redefining what the mathematical operators (+-*/) do when applied to different objects. Java does not support operator overloading.

People also asked