Java / Constructor
Copy-Constructor.
Java supports copy constructor like C++, however Java doesn't create a default copy constructor if the developer does not provide its implementation.
public class Apple { String color; String size; Apple(String color, String size) { this.color = color; this.size = size; } Apple(Apple appleObj) { this.color = appleObj.color; this.size = appleObj.size; } @Override public String toString() { return "Apple of (" + color + " , " + size + ")"; } public static void main(String[] str) { Apple greenApple = new Apple("Green", "small"); Apple redApple = new Apple("Red", "large"); Apple anotherGreenApple = new Apple(greenApple); System.out.println(greenApple); System.out.println(redApple); System.out.println(anotherGreenApple); } }
More Related questions...