I have learnt the principles of object oriented programming well and I think have a good understanding of the concepts - polymorphism, interfaces, abstract classes, inheritance, encapsulation etc. But when I am asked a question in an interview, like how to design "Cards" or a "Zoo", I am not able to come up with answers. When I start thinking about a design, for example Zoo, I just think Animal class, Lion class etc. One or two of the classes might make sense but I know I am not really understanding the problem and coming up with the best solution. I just think in terms of classes because that's the only thing that comes to mind.
How do I start getting better at this? I don't have a job where I can learn this, but I really want to learn this.
OO analysis and design is not about taking something like a Zoo and just going off and creating "objects" that mimic the real-world objects you'd find in a Zoo. OO is about organizing responsibilities and the data that help fulfill those responsibilities. So when someone asks "How would you use OO for a Zoo?", the first thing I would ask is "What is it about a Zoo that you'd like to encode in a computer program?"
Each organization is going to do thing differently, but the main thing would be to concentrate on the interfaces with the systems that class needs to interface with. The bottom line is getting the job done and everyone is going to have a different approach to doing that. For example, if you are asked to create a Zoo class, that may really be the wrong way to approach the interview. The primary question would be what does the required application need to do and what systems are already in place to do it. The new class may simply be a wrapper to encapsulate functionality that already exists. In any case, you would first need to do the analysis and come up the requirements.
I guess the short answer is that a class won't stand alone, it will be part of an object model that is meant to implement an application. So asking you to describe a single Zoo class may be a loaded question.
Does that help?
Joined: Sep 05, 2010
Thank you for your response. I just took Zoo as an example; what I meant to say was that I am totally lost trying think of designs like this one. I don't know where or how to start, how many classes, interfaces, and why so many classes - questions like these.
Joined: May 01, 2013
OK - have you read many books or Internet articles on design patterns. This may be a good place to start if you are interested in how to implement object-oriented designs - at least from the practical standpoint. There are also some good books on the topic. This one was written in 1998, but it has always been one of my favorites.