Java | Overloading and Copy constructors

10 min read

Overloading Constructors:

What is Overloading?

The name of the method together with the type and sequence of parameters form the signature of the method.

Two or more methods within the same class can share the same name as long as their parameter declarations are different (ie., the signature is different. Those methods are said to be overloaded and the process is referred to as method overloading.

(eg) void method1(int x) {.....}  --->A

void method I(int x, int y) {.....} ---> B

void method l(float x, float v) {.....} ---> C

All the above 3 methods have the same name 'method1'.  However, they differ by the number of parameters, it receives (Statements A & B) or they differ at least by the type of the arguments (B &C have 2 arguments, 'B' has integer parameters and 'C' has float parameters). When an overloaded method is invoked, Java uses the type &/ number of arguments to determine which version of the overloaded method to actually call.

(eg) If there is a call like method1(2, 3);, method1 in statement 'B' will be invoked.

If there is a call like method1(5);, method1 in the statement, 'A' will be invoked.

method 1(2, 3.5); ---> method1 in statement 'C' will be invoked. Though the 1st argument is an integer, it will be type promoted to float, to match the method which can take the float. Even though the overloaded methods may have different return types, the return types alone are insufficient to distinguish 2 versions of a method.

(eg) void method1(int x, int y) {....}  ---> cannot be overloaded as they differ only by their return types.

int method1(int x, int y) {.....}  

A constructor can also be overloaded as we overload other methods provided their number of arguments or type of arguments differs.

Let us rewrite the previous program by overloading the constructor. Let us also try to overload the method — 'calculateSpeed( )'.

class Computer
{
private int RAM, hdisk;
private float freq;
private String make;
Computer() ---> will be invoked whenever an object is created as Computer nodel = new Computer( );
{
RAM = 2;
hdisk = 200;
freq = I.2e06;
make = "Intel";
}
Computer(int r, int h, float f, String s)
{
RAM r;
hdisk = h;
freq = f;
make = s;
} ---> will be invoked when object is created as Computer c = new Computer(3, 100, //2.4e06, "IBM");
Computer(int r, int h, float f)
{
RAM = r;
hdisk = h;
freq= f;
make = "AMD";
} ---> will be invoked when object is created as Computer c = new Computer(3, 100, 2.4e06); All objects created by invoking this constructor, assigns "AMD" for the 'make' instance variable.
void calculateSpeed( ) //will be invoked for a single core processor
{
System.out.println("The speed is:" + freq+ "cycles per second");
}
void calculateSpeed(Boolean ismulticore) //will be invoked for a multicore processors
{
System.out.println("Enter the number of cores:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt( );
System.out.println("The speed is: " + freq*n + " cycles per second");
}
void display( )
{
System.out.println(RAM+" "+hdisk+" "+freq+" "+make);
}
Class ComputerMain
{
public static void main(String args[ ])
{
Computer node1 = new Computer( ); ---> invokes default constructor
Computer node2 = new Computer(4,400,2,4e06,"Intel"); ---> calls constructor with 4 args
Computer node3 = new Computer(4, 400, 2.4e06); ---> calls constructor with 3 args
System.out.println("Is node2 a multicore processor?");
Scanner sc = new Scanner(System.in);
boolean ismulticore = sc.nextBoolean( );
if(ismulticore) ---> only if the user gives 'true' if block will be executed.
node2.calculateSpeed(true);         //invokes method that takes a boolean argument
else
node2.calculateSpeed( );         //invokes method that doesn't take any argument
node1 .display( );
node2.display( );
node3.display( );
}
}

 

Assigning one object reference to another:

When one object reference variable is assigned to another object reference object is not made instead only a copy of the reference variable is made.

(eg) Computer node1 = new Computer( ); 

Computer node2 = node 1;

As node2 is assigned node1's value, which is nothing but the address of the object referred to by node 1, node2 will also point to the same object. Thus we have not created 2 objects with identical values but we have created 2 reference variables for the same object.

To create a new object which is identical to the already existing one, the statement should be as follows:

Computer node2 = new Computer(node );

//This will create a new object referenced by node2 & its instance variables will take the values as that of node1.

The object's reference variable node 1 itself is passed as an argument.

So, a constructor which takes reference variable as its parameter should be defined to handle this. lf 'final' Computer C is given, 'C' cannot be modified within the constructor

Computer(Computer C)           
{
RAM = C.RAM;     
hdisk = C.hdisk;             
freq = C. freq;                   
make = C.make;
}              

Has the argument is a reference variable, the parameter receiving it should also be a reference variable of type  'Computer'. node 1's instance variable's values are copied to 'C' reference variable which is later assigned to the instance variables of the object (in our case, node2), used to invoke the constructor.

This is called Copy Constructor.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error