fresh_dev
fresh_dev

Reputation: 6774

How to represent a Zoo in Object Oriented?

i got this interview question about how to represent a Zoo in OOP and my answer was that the Zoo will have property set animals, where Animal is an abstract class and all the animals in the zoo will extend it, it will contain the species,city properties. and Zoo will extend the abstract class Agricultural_land, which contains the space,place properties.

and i got a question about if i have different types of birds, how to represent them in OOP and i said i will make another class for birds and different types of birds will extend it.

please give me your ideas.

Upvotes: 2

Views: 9257

Answers (4)

Radek
Radek

Reputation: 1054

Main reason for asking open questions during an interview is a need for information on your skills. So, there is no one good way to answer such kind of question. Instead, you can consider which skills would like to demonstrate.

A few hints bellow:

  • basic object analysis (how you create classes, methods or attributes)
  • building of classes hierarchy (where inheritance is needed - when not)
  • using interfaces
  • Inversion of control
  • design patterns (eg. does observer pattern is suitable for vets and animals health control?)
  • your approach for building unit tests

Upvotes: 6

Soteric
Soteric

Reputation: 3120

Any object is a combination of state (field values) and behaviour (methods logic). I think if you have to represent something as an object you better specify the context first. If you develop some land trading application then your Zoo should have fields like cost, owner, lastSoldDate and methods like sell, changeOwner, calculateTax. If you work on game then your Zoo will have animals, trees and so on. Clear context allows you to decide what is important for your object and what's not.

As for birds I suggest you to read first chapter of Head First Design Patterns book. There is very fun and clear explanation of how to design ducks hierarchy and how to avoid common pitfalls. They explain the composition over inheritance principal which become kind of enlightenment for me. Now I'm trying to avoid inheritance and design my objects as a set of components responsible for behaviour. And now no over 9000 classes for different birds, no complex classes hierarchy where you scratch your head if you need to add a new bird. That's really nice book :)

Upvotes: 5

Ali Raza
Ali Raza

Reputation: 1215

Etymologically, the word "animal" derives from the Latin meaning a "living being, a being which breathes".Birds breathe and are members of the Animal Kingdom.

So Bird class will extend Animal class

Upvotes: 1

CAA
CAA

Reputation: 970

you could start way more higher in the extension tree.... you have a class "livingt hings" that gets extended by birds, which gets extended...

and a Zoo has more then only animals. It has roads, workers, visiters, trees, garbage cans...

Upvotes: 0

Related Questions