Reputation: 1045
Please have a look at the snippet of the code below which I stumbled upon while reading this java tutorial on creating objects.
// Declare and create a point object and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
The rectOne
object is created by passing an object of Point
class i.e. originOne
and width
and height
of the rectangle. If you have a look at the documentation of Rectangle
class, you come to know that there is no such constructor in the documentations which is accepting three parameters (i.e. A point, width and height). However there are seperate constructors, one of them taking a point of class Point
as parameter
Rectangle(Point p)
and the other one taking width and height of the rectangle as parameter
Rectangle(int width, int height)
I was wondering can you combine constructors, as done by the snippet of the code I shared above from the tutorial?
Upvotes: 0
Views: 781
Reputation: 69440
In the example you do not use the java.awt.Rectangle
. In this tutorial they use an own implementation of the Rectangle class which have such a constructor.
Upvotes: 1
Reputation: 723
Rectangle offers 7 constructors with different arguments of types int
, Point
and Dimension
.
Unfortunalely there is no constructor mixing int
with Point
or Dimension
as you asked for.
Instead you have the following options:
pass only ints (by getting them from the point)
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne.x, originOne.y, 100, 200);
pass a dimension as the second argument
Point originOne = new Point(23, 94);
Dimension sizeOne = new Dimension(100, 200);
Rectangle rectOne = new Rectangle(originOne, sizeOne);
For the rectangle without origin you can use
Dimension sizeTwo = new Dimension(50, 100);
Rectangle rectTwo = new Rectangle(sizeTwo);
This way you can even easily reuse the origin or the size for different rectangles.
Upvotes: 0
Reputation: 2591
No, you can't. You can only use the constructors that have already been defined as they are. The best approach is to use the constructor that is most suitable for your use case and use setter methods to assign your additional properties.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne);
rectOne.setSize(100, 200);
or
Rectangle rectOne = new Rectangle(originOne.getX(), originOne.getY(), 100, 200);
or
Dimension dimension = new Dimension(100, 200);
Rectangle rectOne = new Rectangle(originOne, dimension);
It depends on you :)
Upvotes: 0
Reputation: 393781
No, you can't combine two constructors the way you suggest. Rectangle does have a 3 parameters constructor :
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
The Rectangle class you referred to in your documentation link in not related to the one used in the tutorial.
What you could do is call one constructor from another constructor. For example, a constructor with 3 parameters can first call a constructor that only has the first two parameters, and then initialize the 3rd parameter.
For example, the Rectangle constructor can be re-written as :
public Rectangle(Point p, int w, int h) {
this (p);
width = w;
height = h;
}
or
public Rectangle(Point p, int w, int h) {
this (w,h);
origin = p;
}
Upvotes: 1
Reputation: 35547
Combining constructors? No such thing in Java
.You can convert
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
To
Rectangle rectOne = new Rectangle(new Point(23, 94), 100, 200);//not a Combining.
Rectangle already has a constructor that accepts Point.
Upvotes: 1