Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best constructor

Consider we have two classes Line and Point which class Line uses class Point as below:

public class Point {
   private int x;
   private int y;

   public Point(int x, int y) {
      this.x = x;
      this.y = y;
   }
}

and

public Class Line
{
   private Point start;
   private Point end;
   ...
}

I was wondering which of the constructors below are better for class Line due to OOP principles?

public Line(Point start, Point end)
{
   this.start = start;
   this.end = end;
}

or

public Line(int startX, int startY, int endX, int endY)
{
   start = new Point(startX, startY);
   end = new Point(endX, endY);
}
like image 404
user1776799 Avatar asked Oct 26 '12 10:10

user1776799


Video Answer


1 Answers

Definitely the first one. It is higher-level, the intent is clearer, it is more concise, and it is more maintainable with respect to changes to the Point class.

like image 117
jonvuri Avatar answered Oct 29 '22 18:10

jonvuri