gudii9
asked on
attribute vs parameter and setter vs add method
Hi,
In below code
attribute vs parameter and setter vs add method.
When to use set method and when to use add method. Please advise
In below code
class Passenger
{
Flight m_Flight;
public void setFlight(Flight flight)
{
m_Flight = flight;
}
}
class Flight
{
List<Passenger> m_Passengers = new ArrayList<>();
public void addPassenger(Passenger passenger)
{
m_Passengers.add(passenger);
}
}
Passenger passenger = new Passenger();
Flight flight = new Flight();
flight.addPassenger(passenger);
passenger.setFlight(flight);
what is differences and similarities betweenattribute vs parameter and setter vs add method.
When to use set method and when to use add method. Please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A flight is a list of passengers. A passenger is a member of the list/flight.
When adding a passenger to that flight, you use a method where the passenger is the parameter of the method.
A passenger belongs to a certain flight. A passenger has a flight(number) as an attribute.
If you set a passenger's flight(number) you pass the flight(number) as parameter to the setter method.
In fact by calling that setter, you set the flight(number) attribute of that passenger.
When adding a passenger to that flight, you use a method where the passenger is the parameter of the method.
A passenger belongs to a certain flight. A passenger has a flight(number) as an attribute.
If you set a passenger's flight(number) you pass the flight(number) as parameter to the setter method.
In fact by calling that setter, you set the flight(number) attribute of that passenger.
ASKER
A flight is a list of passengers.
based on the code you mentioned it?
or based on common knowledge?
I see
class Flight
{
List<Passenger> m_Passengers = new ArrayList<>();
public void addPassenger(Passenger passenger)
{
m_Passengers.add(passenger
}
}
Flight class has ArrayList of Passenger type,That is what you mean when you say
flight is a list of passengers
but flight could be more than list of passengers right.
Here only one additional method called
addPassenger
aprart from list of passengers
ASKER
A passenger is a member of the list/flight.
List is Passenger ArrayList you are talking about right?
But flight is separate object right
ASKER
A flight is a list of passengers.
above you mean like below??
A flight has a list of passengers.
A passenger is a member of the list/flight.above you mean like below??
A passenger is a member of the list
or you mean like below ??
A passenger is a non static member instance variable belonging to flight object
ASKER
When adding a passenger to that flight, you use a method where the passenger is the parameter of the method.
Do you mean Based on below code i thought we are adding passenger to ArrayList which in turn is under flight class. Is my understanding correct?
class Flight
{
List<Passenger> m_Passengers = new ArrayList<>();
public void addPassenger(Passenger passenger)
{
m_Passengers.add(passenger
ASKER
http://stackoverflow.com/questions/10115588/what-is-the-difference-between-field-variable-attribute-and-property-in-java
looks like attribute is vague term not defined by oracle
Which of the questions do you want me to answer? ;-)
I normally use the work 'attribute' to refer to instance variables of a class when drawing object models.
ASKER
Which of the questions do you want me to answer? ;-)all of them if you can please
based on the code you mentioned it?Both
or based on common knowledge?
Flight class has ArrayList of Passenger type,That is what you mean when you sayIndeed.
flight is a list of passengers
but flight could be more than list of passengers right.Correct
A flight is a list of passengers.Yes.
above you mean like below??
A flight has a list of passengers.
Do you mean Based on below codeYes.
i thought we are adding passenger to ArrayList which in turn is under flight class. Is my understanding correct?That's the internal details of the Flight class. But as a user of the Flight class you're not interested whether that's an ArrayList or something else. You just (want to) add a passenger to a flight
I think @zzynx has summed it up.
@gudii9 have you understood?
@gudii9 have you understood?
ASKER
A Flight has Passengers as members. You add members to a list of members.why we have passenger as member in Flight whereas flight as attibute in Passenger.
A passenger has a Flight as attribute. You set an attribute on an 'attribute owner' (being a Passenger here)
Why cannot we have both as member or both as attribute
Two reactions:
"Member" and "attribute" are just words, terminology.
In fact - as I already indicated - I would expect a passenger to have a flight number as atttribute.
Eg. A ball has attributes 'color' and 'diameter'. A passenger has the attribute 'flight number'. That attribute expresses what flight (s)he is/will be/was on.Why cannot we have both as memberWe can. I can think of a scenario where that would make sense: Flight could be a member of a Passenger's list of "Taken Flights Ever". (The list of "Taken Flights Ever" could be considered as an attribute of the Passenger)
ASKER
"Member" and "attribute" are just words, terminology.
do they mean same thing or different thing?
ASKER
can you please elaborate. I was not able to folow completely.
I did not understand difference between member vs attribute vs parameter now.