Constructor overloading
Download
1 / 10

Constructor Overloading - PowerPoint PPT Presentation


  • 78 Views
  • Uploaded on

Constructor Overloading. One context in which you will regularly need to use overloading is when you write constructors for your classes. Constructors are methods that can be overloaded, just like any other method in a class.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Constructor Overloading' - meli


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Constructor overloading
Constructor Overloading

  • One context in which you will regularly need to use overloading is when you write constructors for your classes.

  • Constructors are methods that can be overloaded, just like any other method in a class.

  • In most situations, you will want to generate objects of a class from different sets of initial defining data:

tMyn


package TimoSoft;

public class MyClass

{

int x;

MyClass()

{

System.out.println("Inside MyClass() constructor.");

x=0;

}

MyClass(int i)

{

System.out.println("Inside MyClass(int) constructor.");

x=i;

}

tMyn


MyClass(double d)

{

System.out.println("Inside MyClass(double) constructor.");

x=(int)d;

}

void getXvalue()

{

System.out.println("The value of the instance variable \nof the object "

+this+" is "+x+".");

}

}

tMyn


package TimoSoft;

public class MyClassTest

{

public static void main(String[] args)

{

MyClass first=new MyClass();

MyClass second=new MyClass(52);

MyClass third=new MyClass(13.6);

first.getXvalue();

second.getXvalue();

third.getXvalue();

}

}

tMyn


run:

Inside MyClass() constructor.

Inside MyClass(int) constructor.

Inside MyClass(double) constructor.

The value of the instance variable

of the object [email protected] is 0.

The value of the instance variable

of the object [email protected] is 52.

The value of the instance variable

of the object [email protected] is 13.

BUILD SUCCESSFUL (total time: 1 second)

tMyn


tMyn


package TimoSoft; allow one object to initialize another.

public class MyClass

{

int x, y;

MyClass()

{

System.out.println("Inside MyClass() constructor.");

x=0;

y=0;

}

MyClass(int i, int j)

{

System.out.println("Inside MyClass(int) constructor.");

x=i;

y=j;

}

tMyn


MyClass(MyClass obj) allow one object to initialize another.

{

System.out.println("Inside MyClass(MyClass) constructor.");

x=obj.x;

y=obj.y;

}

void getXYvalues()

{

System.out.println("The value of the instance variables \nof the object "

+this+" are "+x+" and "+y+".");

}

}

tMyn


package TimoSoft; allow one object to initialize another.

public class MyClassTest

{

public static void main(String[] args)

{

MyClass first=new MyClass();

MyClass second=new MyClass(52, 18);

MyClass third=new MyClass(second);

first.getXYvalues();

second.getXYvalues();

third.getXYvalues();

}

}

tMyn


run: allow one object to initialize another.

Inside MyClass() constructor.

Inside MyClass(int) constructor.

Inside MyClass(MyClass) constructor.

The value of the instance variables

of the object [email protected] are 0 and 0.

The value of the instance variables

of the object [email protected] are 52 and 18.

The value of the instance variables

of the object [email protected] are 52 and 18.

BUILD SUCCESSFUL (total time: 1 second)

tMyn


ad