• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OO, Patters, Polygon (Quadrilateral and rectangle)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that all quadrilaterals (which I believe to be poligons of 4 sides) are poligons. so I would say that quadrilateral should be a subclass of polygon.
A rectangle is a particular quadrilateral, so as rectangle are quadrilaterals then the rectangle class should be a subclass of quadrilateral.
So you would have that rectangle is a subclass of quadrilateral and quadrilateral is a subclass of polygon.

My questions are:
What problems could arise by making Quadrilateral and Rectangle subclasses of Polygon? What alternatives are possible? What are the advantages and disadvantages of each alternative?

Thanks.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mikie. Welcome to the Ranch!

You're getting close to a classic example of the limitations of single inheritance.

A parallelogram is a quadtilateral, so we can have Parallelogram extend Quadrilateral.
A trapezium is a quadtilateral, so we can have Trapezium extend Quadrilateral.

But a rectangle is a parallelogram and a trapezium, so what do we do now?

I don't think there's an absolute answer to this, so the best approach is going to depend on what you're trying to achieve. Which means I don't think you can necessarily design a perfect hierarchy in isolation.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To put it another way, when you're doing object-oriented analysis and design, you need to balance the "academic" part of figuring out IS-A, HAS-A, etc. relationships with "practical" considerations of what you want to do. The end goal is not really to arrive at a perfect Object-Oriented model that follows ALL the object-oriented rules. Rather, your goal needs to be to come up with a design that is loosely coupled and has responsibilities assigned appropriately to objects that can carry out those responsibilities coherently.

In my experience, it's far more useful to develop your design along with tests and code. Design is an idea, a hypothesis of how the system should work. Tests and code help you verify your idea. if you don't see your design form up as working code, it's very difficult to go through the trial and error process that is needed to refine a design (the idea) into something concrete (working code).

That's why I like Test-Driven Development because it's as much about design as it is about testing and coding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic