Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me with the code for building copy constructor in Java? I have tried with the code but was unable to write the function definition:

public class Point1

{

    private int _x ,  _y;    

    public Point1 (Point1 other)

    {

        ...

        ...

    }

//other more constructors here...

}

1 Answer

0 votes
by (26.7k points)

You can use the below function definition :

public Point1(Point1 other)

{

    _x = other._x ;

    _y = other._y;

}

Also, you can try the below code to pass the arguments:

public class Point2 extends Point1    

{

    private int _z;

    public Point2(Point2 other)

    {

        super(other);

        this._z = other._z;

    }

}

I hope this will help.

Want to become a Java Expert? join Java Training now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Programming:

Related questions

0 votes
1 answer
asked Mar 1, 2021 in Java by rahulnayar01123 (6.1k points)
0 votes
1 answer
asked Apr 3, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 20, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 15, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...