Copy Constructor
Copy constructors are called when you want to create a new object from another object that is the same type. In a constructor, you would pass another previously created object with the same type. For example, if we want to create a new rectangle from an old rectangle, we write the copy constructor.
TEXT/X-JAVA
1// Rectangle.java
2public class Rectangle extends Shape {
3 protected int height;
4 protected int width;
5
6 // Overriding default constructor
7 public Rectangle() {
8 this.height = 1;
9 this.width = 1;
10 }
11
12 // Copy constructor
13 public Rectangle(Rectangle other) {
14 this.height = other.getHeight();
15 this.width = other.getWidth();
16 }
17
18 // Custom Constructor
19 public Rectangle(int height, int width) {
20 this.height = height;
21 this.width = width;
22 }
23
24
25 // Rest of the methods and Attributes
26}