Java examples for Object Oriented Design:Constructor
Overloading Constructors
import java.awt.Point; public class Main { int x1 = 0;/*from w w w . j a va2s. c o m*/ int y1 = 0; int x2 = 0; int y2 = 0; Main(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } Main(Point topLeft, Point bottomRight) { this(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y); } Main(Point topLeft, int w, int h) { this(topLeft.x, topLeft.y, topLeft.x + w, topLeft.y + h); } void printBox() { System.out.print("Box: <" + x1 + ", " + y1); System.out.println(", " + x2 + ", " + y2 + ">"); } public static void main(String[] arguments) { Main rect; System.out.println("Calling Box2 with coordinates " + "(25,25) and (50,50):"); rect = new Main(25, 25, 50, 50); rect.printBox(); System.out.println("\nCalling Box2 with points " + "(10,10) and (20,20):"); rect= new Main(new Point(10, 10), new Point(20, 20)); rect.printBox(); System.out.println("\nCalling Box2 with 1 point " + "(10,10), width 50 and height 50:"); rect = new Main(new Point(10, 10), 50, 50); rect.printBox(); } }