In the first example you say "I want
Car to assign a value of 10 to some Machine's (
Car isn't really connected with that
Machine, it could be "Gate", "Garage" or other classes that
Car can cooperate with
Car, but they are not in the relation "Car is a..." ) variable
In the second one you say "
Car is a machine and I want it to be able to change value of it's own
x variable to 10". (You could say "I want my car to start its own engine, for example).
I know that in the first example I'm creating kind of new object (am I right?) - it's not about creating new
Machine reference by passing it as an argument. You rather say that you want instance of
Car class to be able to change
x value of the one specific instance of Machine class. Not to all Machines, just to the one you pass as an argument to the
start() method. Example:
Car has a functionality to open the gate when you want it to, but Car
is not a Gate.
But I don't know when should I use first one and when the second one, which is better, and so on. In the second example you say Car is a Machine - so it is able to perform all actions that Machines are able to perform and it also is able to perform some actions specific only to Cars and not to all Machines. To use inheritance you should be sure that you can say Car is a Machine.