How do you connect two classes without using inheritance.
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.
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)
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.
Basically what you are doing is putting a piece of information into that method. In this case your putting a
Person object into the
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
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 3 Years Ago by JamesCherrill
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...