views:

367

answers:

15

Hi guys. I was asked to describe the relationship between vehicle, car, toyota in object oriented programming term (let's say in php environment). I was stumped. Can someone help me about it? Thanks...

+3  A: 

A car is a vehicle and a toyota car is a car.

The most straightforward statement is that car could be a subclass of vehicle and a toyota car could be a subclass of car.

Of course, Toyota could also refer to the company, in which case things would be a little different. I'm pretty sure the paragraph above is what they expected to hear, though.

Artefacto
And buy_toyota is a method of the class Toyota_car.. )
Alexander.Plutov
surely buy() is a method of car? or even vehicle? :)
Basiclife
@Alex He I'd prefer a Stand class that had a method buy that received a car object :p
Artefacto
And of course, `ToyotaCar` would have a buggy `accelerate` method :p
Artefacto
buy() would, of course, be a method of Person :p
Matt Ellen
@Alexander: so I need to have a car from Toyota in order to buy a car from Toyota? That's a pretty awful business model
jalf
@jalf Yes, you do, except you are confusing the meaning of "have". You do not have to "possess" a car from Toyota, but there has to be an instance so that you can buy it. However, I don't think a `buy` method would make sense in a `ToyotaCar`.
Artefacto
+2  A: 

Did you mean:

class Vehicle{
}

class Car extends Vehicle{
}

class Toyota extends Car{
}
fabrik
I would disagree. Toyota is an instance of car, it has no new behaviours.
Matt Ellen
@Matt Ellen I'd have to disagree with that. You could easily imagine features that are unique to a specific brand of car (OnStar, etc.)
dave
@dave http://stackoverflow.com/questions/3503392/the-object-oriented-relationship/3503444#3503444 check the last line
klez
Toyota also makes trucks. Does that mean that Vehicle > Truck > Toyota? I'm not on board with this one.
Rob Hruska
Fair point, @dave. I'm not into cars, so I just imagine they're all the same ;)
Matt Ellen
+1  A: 

Toyota is a car. Car is a Vehicle. So Toyota class would inherit from Car which in turn would inherit from Vehicle.

dave
+2  A: 

"Vehicle" would be a base class, "Car" would be a class that extends, inherits, or is derived from "Vehicle" (depending on the language used), and ideally "Toyota" should be the value of a "brand" or "make" property of one of those two classes.

cHao
+1  A: 

Vehicle is a class. Car is a sub-class of Vehicle. Toyota could be a (fairly generic) instance of the Car class, or a sub-class adding Toyota-specific functionality.

meagar
+4  A: 

Car is a type of Vehicle - so you could think of Vehicle as a base class of Car.

Toyota manufactures cars. Instead of making this an inheritance relationship, I would be inclined to add a manufacturer member variable to Vehicle. Then for instances of Car that are made by Toyota, set car.manufacturer = "Toyota".

Justin Ethier
+1  A: 

A Car is a Vehicle, a Toyota is a Car.

So, Toyota could be a subclass of Car and Car could be a subclass of Vehicle.

class Vehicle {

}

class Car extends Vehicle {

}

class Toyota extends Car {

}

For example a Car may have one member var to indicate the number of wheels and a function to turn on the engine (not all vehicles have an engine). While a Toyota may have the same vars as a Car, plus a string containing the model's name and a method to ask for warranty on pedals ;-)

klez
+2  A: 

Vehicle is a base class. Car is a derived class. Toyota is (probably) a value that can be assigned to the "brand" member of the Vehicle class. In theory, you could derive Toyota from vehicle (or something on that order), but IMO it's generally a poor idea. Derivation should generally reflect function, and there's little in the way of function that's attached to a brand.

Jerry Coffin
+1  A: 

You can take different approaches.

  1. Car is a subclass of Vehicle. Toyota is a subclass of Car.

  2. Car is a subclass of Vehicle. Car has a property called make. You instantiate Car and set its make to "Toyota".

There are more ways to do it, but those two are the most obvious that come to mind that fit into the OOP concept.

The key is to ask yourself what is the most general term there? It's vehicle. You can have many kinds of vehicles such as trucks, SUVs, bicycles, hovercrafts and cars. So, in OOP, your more specific class inherits from the more general class.

Jeff
+23  A: 

I think Toyota refers to the company.

I would say:

Vehicle is an object, and a Car is a type of Vehicle. Toyota is a particular implementor of Vehicle objects with the brake method overloaded to return false.

I can't bring myself to click the up arrow, but LOL!
cHao
HAHAHAHAHA! ROFL. Oh man. The shamelessness brings me to tears! +20 for evilclown!
cdburgess
What do you mean? Toyota is a factory (in the sense of the factory pattern)? "particular implementor of Vehicle" doesn't have any meaning in OOP
Artefacto
@Artefacto, yes, Toyota is a factory object that produces Vehicle objects, be it a Car object, Truck object, or other.
Car, Truck and other derived classes inherit the brake property that always returns false by default.
stillstanding
+2  A: 

Just to be awkward: car is a subclass of vehicle, as is truck. Toyota is a manufacturer who make both cars and trucks; so Toyota should be one of several potential values for the manufacturer attribute of vehicle.

class Vehicle{ 

   protected $_model;

   protected $_manufacturer;

   public __construct($manufacturer) {
       $this->_manufacturer = $manufacturer;
   }
} 

class Car extends Vehicle{ 
   public __construct($model,$manufacturer) {
       parent::__construct($manufacturer);
   }
   $this->_model = $model;
} 

class Truck extends Vehicle{ 
   public __construct($model,$manufacturer) {
       parent::__construct($manufacturer);
   }
   $this->_model = $model;
} 

$a = new Car('Avensis','Toyota');
$b = new Truck('Land Cruiser','Toyota');
Mark Baker
+2  A: 

The most basic way to think about it is in the logical hierarchy of what you are working with. And this would apply to anything in the world practically. What everyone has said here is spot on, but it may not help you understand what they are saying, if you don't understand OOP. SO allow me to elaborate.

Vehicle: A vehicle could be any sort of transportation. Keep in mind, it could be a plane, train, automobile, bike, hot air ballon, etc. A vehicle is something used to get you from one location to another. So the base class being vehicle tells you it's going to be a transportation device.

Car: A car is a form of vehicle. Therefore, it EXTENDS vehicle. A car can be described as a vehicle with 4 wheels, 2-n doors (depending on the car), an engine, a steering wheel, wipers, headlights, blinkers, brakes, accelerator, etc. So car further describes what vehicle you are referring to.

Toyota: A toyota is a form of car. Therefore, it EXTENDS car. (It could also extend truck if you were working with a Truck class). There are certain elements of a toyota that may not apply to every car. So to help further define the type of car, toyota could designate gas mileage, number of doors, color, size of engine, etc.

Hope that helps.

cdburgess
I do like your elaboration....:D
Jerry
... and a blue car is a form of car. Therefore it EXTENDS car. And a blue car with a scratched window is a form of blue car, therefore it EXTENDS blue car. None of which gives you a *useful* object hierarchy. There are a few other requirements for derivation, than simply "is a form of". It also has to have some kind of unique attributes **that are actually relevant to the domain**, for example
jalf
+1  A: 

In object oriented term Vehicle will be the base class car will be the subclass of vehicle

and Toyota will be brand name as an attribute.

Vipul
+1  A: 

My take: "toyota" should not be derived from "car", because Toyota is a company, not a specific car. A "toyota_car" could be derived from "car". "toyota" should be derived from the "manufacturer" class, which is derived from the "company" class. The "vehicle" class should have a class member of type pointer to class "manufacturer". In the case of a car manufactured by Toyota, the instantiated object of class "car" should have that inherited class member pointing to the Singleton object of type class "toyota". (C++)

Robert Drehmel
+1  A: 

Of course, this isn't the answer your interviewer wanted to hear, but the goal of object-oriented programming is not to model the entire world, but simply to provide abstract data types which model specific concepts in the domain of your application.

Hence, the relationship between the three depends entirely on what application you're making. It is perfectly possible that vehicle and car should be the same class. A lot of applications may not care what type of vehicle it is. They also might not care what brand it is, allowing you to throw away the Toyota class as well.

OOP could have been a useful tool, if it hadn't been taken hostage by idiots who thought the goal of programming is to produce a UML diagram of the entire universe.

jalf