1

I know it's generally a design flaw to have an empty class, yet I'm wondering whether there are some cases where it isn't to have an empty (abstract) class to group objects. Especially when polymorphism is needed.

For example I have a group location. They can consist of an area, a line (a road for example) or a specific point (such as a house). In the data structure it only requires to contain the coordinates.

I know they all have at least a single point in common, yet it's really weird property wise when I would have a coordinate at my location class.

At the moment I was thinking of the following solution:

public abstract class Location {

}

public class Point: Location 
{
    public double X {get;set;}
    public double Y {get;set;}
}

public class Line : Location
{
    public double StartX {get;set;}
    public double StartY {get;set;}
    public double EndX {get;set;}
    public double EndY {get;set;}
}

I know it's not a perfect example as there is another more correct solution to this case and there are probably objects for this solution found within the .Net libraries.

But how are things like this solved overall?

  • 1
    The class is useless. You create an abstraction so you can use the API the abstraction provides without being concerned about implementation details. If your class provides none of that, it has no meaning. Just create a Location folder and place your locations there. – Andy Jun 23 '16 at 7:50
  • 2
    You say "polymorphism is needed", but for what? There is no operation common to both lines and points that you can perform via the base class. What is the compelling benefit of having, say, a collection of locations, if all you can do is ask each location "are you a point?" wouldn't it be better in that case to have a collection of points and a collection of lines, and a common base class "object" ? – Eric Lippert Jun 23 '16 at 19:39
  • Inheriting from nothing never makes any sense. Instead contain the point and the line in the Location class. That way either the point or line could be null whenever they need to be. They should never both be null because, the object wouldn't be needed in that case. – John Peters Jun 24 '16 at 4:44
  • It would be correct only if the parent class might one day hold additional data. Otherwise yes useless. You might also consider making it an interface. – Andrew Apr 24 at 13:51
5

Polymorphism with no shared interface will ONLY let you store objects in the same (strongly typed) container, not actually DO anything. You can't disptach method calls or anything like that.

In your specific example, I think composition would be more appropriate, no? You already have an abstraction for a specific point in 2d space, so why woudn't you reuse it in your abstraction of all points between two points?

public class Point
{
    public double X { get; }
    public double Y { get; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

public class Line
{
    public Point Start { get; }
    public Point End { get; }

    public Line(Point start, Point end)
    {
        Start = start;
        End = end;
    }
}

EDIT: I'd also mention, having a Line inherit from Location is just misleading. A line in the mathematical (euclidean) sense is a set of points defined by two points. Of course, if what you're interested in are JUST the points BETWEEN the start/end points, then what you have isn't a line, but a line segment.

2

The two subclasses does not have any interface in common, so why you do you want to have them inherit from a common class in the first place? The Location class does not provide you any benefit as far as I can tell.

To me it would seem to more logical to have the Line contain two Point's.

  • I know it could be solved differently, for example just having one object named Location which contains a list of points (which can be used for areas as well then). This was just an example. – Echarnus Jun 23 '16 at 7:55
  • @Echarnus: OK, but the answer in the general case is that you should not include useless classes. – JacquesB Jun 23 '16 at 7:56
  • @Echarnus that's the problem with simplified examples, you get superficial answers. – RubberDuck Jun 23 '16 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.