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...
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.
Did you mean:
class Vehicle{
}
class Car extends Vehicle{
}
class Toyota extends Car{
}
Toyota is a car. Car is a Vehicle. So Toyota class would inherit from Car which in turn would inherit from Vehicle.
"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.
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.
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"
.
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 ;-)
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.
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.
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.
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');
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.
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.
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++)
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.