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...
-
4If this is homework or an interview question, please tag it as such. – user229044 Aug 17 '10 at 14:21
-
If it's in Java, you may need a Vehicule.ToyotaFactory or something. – Wadih M. Aug 17 '10 at 14:25
-
so..what's up with 9 answers in 2 minutes? Thank you guys. It was an interview question btw...I believe I answered it well at that time but not sure if you guys can come up better answers. +1 to all – FlyingCat Aug 17 '10 at 14:27
-
There is not that mush information here. What is the intention of your system? What do you put behind car, Toyota, Vehicle? – mathk Aug 17 '10 at 14:30
-
4Google will hate us for naming Toyota so much and screwing the searches for Toyota :P – Federico klez Culloca Aug 17 '10 at 14:31
15 Answers
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.
-
8
-
HAHAHAHAHA! ROFL. Oh man. The shamelessness brings me to tears! +20 for evilclown! – Chuck Burgess Aug 17 '10 at 14:37
-
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 Aug 17 '10 at 14:43
-
@Artefacto, yes, Toyota is a factory object that produces Vehicle objects, be it a Car object, Truck object, or other. – Aug 17 '10 at 14:48
-
Car, Truck and other derived classes inherit the brake property that always returns false by default. – bcosca Aug 17 '10 at 15:19
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.

- 96,375
- 17
- 202
- 225
-
-
-
@Alex He I'd prefer a Stand class that had a method buy that received a car object :p – Artefacto Aug 17 '10 at 14:23
-
And of course, `ToyotaCar` would have a buggy `accelerate` method :p – Artefacto Aug 17 '10 at 14:26
-
@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 Aug 17 '10 at 15:50
-
@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 Aug 17 '10 at 16:09
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"
.

- 131,333
- 52
- 229
- 284
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');

- 209,507
- 32
- 346
- 385
Did you mean:
class Vehicle{
}
class Car extends Vehicle{
}
class Toyota extends Car{
}

- 14,094
- 8
- 55
- 71
-
4I would disagree. Toyota is an instance of car, it has no new behaviours. – Matt Ellen Aug 17 '10 at 14:26
-
1@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 Aug 17 '10 at 14:28
-
@dave http://stackoverflow.com/questions/3503392/the-object-oriented-relationship/3503444#3503444 check the last line – Federico klez Culloca Aug 17 '10 at 14:29
-
1Toyota also makes trucks. Does that mean that Vehicle > Truck > Toyota? I'm not on board with this one. – Rob Hruska Aug 17 '10 at 14:29
-
Fair point, @dave. I'm not into cars, so I just imagine they're all the same ;) – Matt Ellen Aug 17 '10 at 14:38
"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.

- 84,970
- 20
- 145
- 172
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.

- 476,176
- 80
- 629
- 1,111
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.

- 11,600
- 5
- 41
- 74
-
1... 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 Aug 17 '10 at 15:52
Toyota is a car. Car is a Vehicle. So Toyota class would inherit from Car which in turn would inherit from Vehicle.

- 12,406
- 10
- 42
- 59
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.

- 232,980
- 40
- 330
- 338
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 ;-)

- 26,308
- 17
- 56
- 95
You can take different approaches.
Car
is a subclass ofVehicle
.Toyota
is a subclass ofCar
.Car
is a subclass ofVehicle
.Car
has a property calledmake
. You instantiate Car and set itsmake
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.

- 21,744
- 6
- 51
- 55
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.

- 2,637
- 6
- 25
- 28
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++)

- 1,260
- 11
- 29
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.

- 243,077
- 51
- 345
- 550