posted 17 years ago
Yes, having an abstract class does save you from having to code a name variable and getter/setter in each class. That's a point in favor of an abstract class. The trade-off, though, is that the class becomes less flexible. You stated at the beginning of your first post that the assignment was to have each class calculate the area. The interface accomplishes that beautifully without an abstract class. The name attribute was something you added as an afterthought, and is peripheral to the real point of the class. I'd say it's not worth making the class less flexible.
This is obviously a simple application, so it doesn't really matter, but in the real world, it's a good idea to keep classes free of dependencies on a static hierarchy whenever possible.
Here's an example of what I'm talking about: Suppose now you want to draw each of these shapes on a screen. You now have a whole lot of other things to take into consideration, such as coordinates, background color, translucence, etc. Since all you used was a interface to calculate the area, you're free to have these classes implement as many other interfaces as you want (e.g. drawable, expandable, movable, etc.). Now suppose you have a whole lot of default behavior you want each of these shapes to inherit that has nothing to do with area, and it would be really convenient just to have the shape classes inherit from a class that embodies all that default behavior. If the shape classes already inherit from an abstract class, you're just out of luck, because a Java class can only inherit from one superclass. I think you can see why you don't want to play the abstract class card until you really need it.
You just have to weigh the benefits on either side and make your decision. If, for example, there were ten properties shared by all classes, that would probably swing my decision over to using an abstract class. With only one property, though, I think I'd prefer to keep the class more flexible and just do the extra work of coding the property in each class.