Overloaded constructor : Overloading « Class « Java






Overloaded constructor


public class Point {
  int x, y;

  Point(int x, int y) // Overloaded constructor
  {
    this.x = x;
    this.y = y;
  }

  Point(Point p) // Overloaded constructor
  {
    this(p.x, p.y);
  } // Calls the first constructor

  void move(int dx, int dy) {
    x += dx;
    y += dy;
  }

  public String toString() {
    return "(" + x + ", " + y + ")";
  }
}
           
       








Related examples in the same category

1.Demonstration of both constructor and ordinary method overloadingDemonstration of both constructor and ordinary method overloading
2.Overloaded methodOverloaded method
3.Demonstration of overriding fieldsDemonstration of overriding fields
4.Overloading based on the order of the argumentsOverloading based on the order of the arguments
5.Promotion of primitives and overloadingPromotion of primitives and overloading
6.Overloading a base-class method name in a derived class does not hide the base-class versionsOverloading a base-class method name in a derived class does not hide the base-class versions
7.Demotion of primitives and overloading