Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A good Object-Oriented analogy [closed]

I'm looking for a good way to describe OO to beginners, though an analogy.

Currently I'm likening a Class to a shopping list, and a shopping trolley full of items to an object. But I feel it's a bit confusing.

Preferably the analogy would be reflected well in the code example (Ruby), currently I have this, and it feels klunky.

# First we create a class
class Shopping

    # The items method gives us a list of items in the Shopping
    def items
    ["apple", "cereal", "flour"]
    end
end

# Create a new Shopping, called basket
basket = Shopping.new

# ask the basket what items it has
basket.items        #=> ["apple", "cereal", "flour"]
like image 346
thomasfedb Avatar asked Jun 29 '10 15:06

thomasfedb


4 Answers

I've seen it described as Man is a class, and Steve is an object (instance of Man). Steve has blonde hair, is 6', weighs 180lbs, etc.

You can then do inheritance, so Man inherits Person, Person inherits Animal, and onward.

like image 63
Adam Shiemke Avatar answered Sep 20 '22 02:09

Adam Shiemke


I don't think you should be thinking in terms of an analogy, but rather an elucidating example. People already think in an object-oriented way; just tell them an object is a thing and a class is a kind of thing.

Show them that 2 is a Fixnum and "hello" is a String. Then show an example of how a class describes what that kind of thing is composed of and what it can do. Any of these simple examples, like Animals or Cars, will work fine.

like image 44
mckeed Avatar answered Sep 19 '22 02:09

mckeed


When I normally think about the distinction between class and object, I generally tend to think of the class as a blueprint. Perhaps you could compare a class to a blueprint and a object to a house?

class House
    def floor 
        @floor
    end
    def roof 
        @roof
    end
    //[etc]
end
like image 35
KLee1 Avatar answered Sep 22 '22 02:09

KLee1


I'm no mechanic, but I often use cars as a high level example for polymorphism and hierarchy.

We have Vehicles, things that can get us around and it'd have methods and properties like Move() or Park() or PassengerCount. Examples of Vehicles can be Sedans, Vans, Trucks, and Bikes. Sedan might have methods and properties like TurnOnLights(), GallonsOfGas, and such, but under Sedan might be PoliceCar. PoliceCar can have new methods and parameters and redefine some of Sedan's methods and properties, i.e. a PoliceCar's TurnOnLights() method can do much more than a Sedan can do.

As far as instances go, you can explain to them that your car is an instance of Sedan. It's blue, has 7 gallons of gas, and can turn on its lights. Just walk through a parking lot pointing out instances: "that's a van, there's a sedan, here's a bike." Don't treat them stupid, but show them how it's a definition and relate it to a definition they already know. On a side note, talk about how the parking lot is an array of Vehicle objects. The parking lot doesn't care what kind of Vehicle it is.

There are a lot of different ways to relate private and public methods to this as well. Such as if your car has no idea how much gas another car has, or something like that.

If they're catching on, you can describe Interfaces to them by saying every Sedan (let's it's automatic to simplify things for now) has the same interface. They all provide the same method for steering, accelerating, braking, displaying gas and mileage and so on. How these features actually work can differ from instance to instance, but the same methods are made available in all Sedans. The drive does not need to know how the engine works explicitly because of this interface.

like image 43
Corey Ogburn Avatar answered Sep 19 '22 02:09

Corey Ogburn