QuoteOriginally posted by: billyx524hi,i was recently asked during an interview if a circle class can inherit from an ellipse class. my thinking was that since a circle is a special case of an ellipse, then it should be allowed. but the interviewer said no. I find this to be subjective. Is there a right/wrong answer to this question?In a previous life I worked in CAD and my company developed C++ and C# CADObject library that we used for all kinds of stuff. So we went through this whole discussion when developing the library.Where to star? First, some general issues:A. People confuse ISA (gen/spec) relationship with something elseB. ISA refers to base class B and derived class D. C. ISA is usually (incorrectly) done as base class B and object/instance of B that some people 'promote' to the status of a class (call it D).D. The "driver" B->D should be behavior, not structure.You may be making an error of judgment in one of A...C.Now for ellipse/circle; there's a lot of high school maths that we would like to use in CAD. In this regards Circle has much more functionality that Ellipse (intersection with circles, lines etc.). Here are the choices:I. Circle derived from Ellipse ("structure drives specialization" ==> wrong). Interviewer was correct.III. Create an Ellipse that uses a Circle; change coordinate to transform Ellipse to Circle, do the stuff on Circle and transform back (this is Composition).II. No class Circle, just class Ellipse (an object with axes a == b is just a square object and not a class).IV. Separate classes Ellipse and Circle (no relationship in principle).These are model. Which one is correct depends on your requirements! Maybe interviewer wanted to discuss the different scenarios.(Some) developers jump to conclusions => Use ISA based on inherited/specialized structure (WRONG), Use ISA based on inherited behaviour (GOOD).Languages that support interfaces don't have this problem. C++ does not support interfaces! //A good counterexample to case D above is Rectangle (width, height) and derived Square (width == height). QuizClasses Option, CallOption, PutOption, ShoutOption??
Last edited by Cuchulainn
on February 14th, 2016, 11:00 pm, edited 1 time in total.