3
Contributors
4
Replies
13
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

public boolean hasDriver(Person drive)
I dont understand what drive in the above method represents. Person is a class while the method is under Car class.

0
  1. By each having a reference to the other. Eg your Car class is connected to your Person class via the driver variable. Inheritance is also called an "is a" relationship (Cat is a Mammal). You can also have a "has a" relationship (Car has a Driver)

  2. When the hasDriver method is called you have to pass an instance of Person as a parameter. Inside the method that instance will be accessed by the name "drive", regardless of what the variable name may be in the calling method.

0

Basically what you are doing is putting a piece of information into that method. In this case your putting a Person object into the hasDriver() method.

Now drive is how you reference the Person object. so if Person has a method in it called getName(), you would say `drive.getName()

A simple way to look at it for now is if you think of variables. e.g.

int number = 0 Here we create an 'Integer' which we call 'number' and we assign it a value of '0'

The same is true of Objects
Person drive = new Person(); says We want to go to the 'Person' class and we are going to call it 'Drive' and we want to make this into a 'new Person' object

Hope this clarifies your problem. If not then feel free to send me a message of reply right here on the thread

0

ObSys
That misses a very important fundamental point about Java. In your examples the "number" variable contains the value 0, but the"drive" variable is not a Person object, and does not contain a Person object. It is a reference variable that contains a reference (pointer) to a Person object. Failure to make this distinction is behind many posts we get here when people don't understand what happens when you have two variables and one object.

ps I just saw another of your recent posts in which you tried to test an int != null. That's exactly the kind of mistake I had in mind!

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.