-3

Say, if in our object design, there is already a Car class, and now there are some cars objects that convertibles.

We can define another class Convertible and subclass Car, but then let's say, we later on create a class that's FourWheelDrive that also subclasses Car, and later on, if we have a FourWheelDrive that is also a Convertible, then how can we handle it?

How is the design above compared with the other design, which is a isConvertible boolean in the Car class, and a isFourWheelDrive boolean also in the Car class, just as flags or properties of the Car class. So we won't define extra classes in this case.

Update: in a real life example, in our project, there was a Credential class that stores a user's info: user_id, encrypted_password, email_address, etc. When we allow logging in through Facebook, Gmail, Yahoo, MySpace (using JanRain), a coworker proposed adding FacebookCredential, GmailCredential, YahooCredential, all these classes that subclass Credential. I was a bit overwhelmed that there are so many classes, and when you see a method, you have to look at whether the subclass overrides it or is it the base class's method. I would have done it just using a code to tell which provider of the credential it is (Facebook, Gmail, etc), and use this provider code to do the appropriate things. (for example, some providers have verified email address, some don't). So I just don't know if my coworker's method is more appropriate or complicated.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 7
    I never understand these "objects representing real world constructs" discussions. The answer always depends on the ACTUAL problem you are trying to solve with your code. – Andrew Shepherd Apr 22 '12 at 23:29
  • maybe at least we know what the options are, it can be helpful – nonopolarity Apr 22 '12 at 23:42
  • "You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page." – sunnyrjuneja Apr 22 '12 at 23:51
  • @Sunny this is not a practical question? You haven't seen a racing game with cars in it before huh, have you? – nonopolarity Apr 23 '12 at 00:26
  • There is no need to get defensive. Every answer below is an abstraction of your problem. There is no right answer to your problem. It matters on the language you use, the conventions you adhere to and your personal preference. Perhaps in asking future questions you'll consider what a suitable answer would look like because now there is no clear end for this question. – sunnyrjuneja Apr 23 '12 at 00:41
  • @Sunny many people who ask questions have no idea what the answer would look like. Do you know why people ask questions? It is because they don't know the answer – nonopolarity Apr 23 '12 at 00:58
  • 1
    Please stop asking rhetorical questions. I'm suggesting that a better place to find your solution might be a reading book or taking a course on object oriented programming and programming more. Learning how to design classes is not something you will learn on SO and your tools will effect those design decisions. – sunnyrjuneja Apr 23 '12 at 02:03
  • programming nowadays has a lot to do with object design. Some object design questions have a clear answer, and some don't. I don't like that you play God and decide what object question can be asked, and what cannot be. – nonopolarity Apr 23 '12 at 02:12
  • He's not "playing god", he attempting to politely explain how SO works, and the best way for you to help yourself. Please see: the [FAQ](http://www.stackoverflow.com/faq) and also perhaps [Stack Overflow is not attacking you personally](http://meta.stackexchange.com/a/128589/172496) and [Stack Overflow is not what you assume it is](http://meta.stackexchange.com/a/128554/172496) – Brian Roach Apr 24 '12 at 17:02

4 Answers4

2

You might want to take a different approach by using the http://en.wikipedia.org/wiki/Strategy_pattern You can define different behaviours for diffrent types of cars, so you don't have lots of subclasses.

Ozzie
  • 11,613
  • 4
  • 21
  • 24
1

You imply that you're using a language which doesn't support multiple inheritance. Does the language support interfaces? You could have a base abstract class Car. (Abstract because one never builds a "car" but instead builds a specific implementation of a car.) Then instances of classes which inherit the abstract base class can implement common interfaces:

  • IConvertible
  • IFourWheelDrive
  • IHybrid
  • and so on...

The idea is that the abstract base class defines what something is at its simplest. The interfaces define what type of that thing it is, and there can certainly be overlap. Those interfaces would contain the operations and properties which are specific to that type. (An IConvertible would have properties and methods that non-convertibles wouldn't have, same with an IHybrid, etc.) Then the specific implementations could add their own unique flair to the whole thing.

It's a bit of a contrived example, as you know. So it's all conjecture. But for this particular theoretical implementation I'd go with interfaces.

David
  • 208,112
  • 36
  • 198
  • 279
1

In addition to your concern about four wheel drive cars which are also convertibles, consider the following:

  • Are all Convertables cars?
  • Are all vehicles with four wheel drive cars?

To me, these sorts of questions point at these being attributes of car which do not define a type. In a sense, they're no different than color or the number of doors, etc.

Jason Braucht
  • 2,358
  • 19
  • 31
  • but... in the example of `Animal` class, if we have rabbits, we might define a `Rabbit` class and subclass `Animal`, even when all rabbits are animals – nonopolarity Apr 22 '12 at 23:40
  • It may be that all convertibles are in fact cars (I can't think of a counter-example). However, not all four wheel drive vehicles are cars. For example ATVs are sometimes four wheel drive but they are arguably not considered cars. – Jason Braucht Apr 23 '12 at 00:03
  • I'm not sure there is a definitive answer to these questions. While the example of creating a subclass `Rabbit` from `Animal` might be useful in some contexts, in others one might argue that `Rabbit` is simply a name for a particular instance of `Mammalia`. Or in the context of story books, `Rabbit` might be a `LeadCharacter` (eg [Peter Rabit](http://en.wikipedia.org/wiki/Peter_Rabbit)). [Ontology](http://en.wikipedia.org/wiki/Ontology) is not a simple subject. – Jason Braucht Apr 23 '12 at 00:07
0

It basically comes down to a question whether you need/want to change the functionality of the class based on whether it's a convertible and or four wheel-drive. For example, if you need/want to have a raise_top and lower_top for a convertible, and lock_hubs and unlock_hubs for a four wheel-drive, then you pretty much need to use inheritance to add classes that have those. In such a case, then yes, there's a pretty fair chance that an open-top 4WD vehicle will inherit from both the convertible and 4 wheel-drive classes. As such, if you're doing that in C++, those classes should probably inherit virtually from automobile (or whatever you name your base class), and in something like Java, they'll pretty much need to be interfaces rather than classes.

On the other hand, if you just need to be able to find out whether a particular vehicle is or isn't convertible and/or 4 wheel-drive, then a simple Boolean (or enumerated) field in the class should be perfectly adequate.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111