Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

attribute vs parameter and setter vs add method

Hi,

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);
   }
}

Open in new window



Passenger passenger = new Passenger();
Flight flight = new Flight();

flight.addPassenger(passenger);
passenger.setFlight(flight);

Open in new window

what is differences and similarities between
attribute vs parameter and setter vs add method.

When to use set method and when to use add method. Please advise
SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

A Flight has Passengers as members. You add members to a list of members.
A passenger has a Flight as attribute. You set an attribute on an 'attribute owner' (being a Passenger here)

can you please elaborate. I was not able to folow completely.

I did not understand difference between member vs attribute vs parameter now.
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.
Avatar of gudii9

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
Avatar of gudii9

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
Avatar of gudii9

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
Avatar of gudii9

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);
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.
Avatar of gudii9

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?
or based on common knowledge?
Both

Flight class has ArrayList of Passenger type,That is what you mean when you say
flight is a list of passengers
Indeed.

but flight could be more than list of passengers right.
Correct

A flight is a list of passengers.
above you  mean like below??
A flight has a list of passengers.
Yes.

Do you mean Based on below code
Yes.

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?
Avatar of gudii9

ASKER

A Flight has Passengers as members. You add members to a list of members.
A passenger has a Flight as attribute. You set an attribute on an 'attribute owner' (being a Passenger here)
why we have passenger as member in Flight whereas flight as attibute in Passenger.

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 member
We 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)
Avatar of gudii9

ASKER

"Member" and "attribute" are just words, terminology.

do they mean same thing or different thing?