Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1092107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:37:59+00:00 2026-05-16T23:37:59+00:00

public class Elevator () { Button firstFloorbutton = ButtonFactory.getButtonInstance(this, 1); Button secondFloorbutton = ButtonFactory.getButtonInstance(this,

  • 0

public class Elevator ()
{
Button firstFloorbutton = ButtonFactory.getButtonInstance(this, 1);
Button secondFloorbutton = ButtonFactory.getButtonInstance(this, 2);
Button thirdFloorbutton = ButtonFactory.getButtonInstance(this, 3);
Button fourthFloorbutton = ButtonFactory.getButtonInstance(this, 4);

 Fan fan1 = FanFactory.getFanInstance(this);

 Light light1 = LightFactory.getInstance(this);

 private void goUp()
 {
    .....
 }

 private void goDown()
 {
   ......
 }

   .............   

 }

        ============================

 public class Button()
 {

   Elevator E;
   int floor;

   public button (Elevator E, int floor )
   {
    this.E = E;
    this.floor = floor;
   }

   public void buttonPressed()
   {

   //logic to determine which floor the elevator is currently at and determine whether to go up or down to reach "this.floor"

   E.goUp();  // if need to go up

   else

   E.goDown()   // if need to go down

   }
} 


    ==========================


    public class ButtonFactory()
    {

      public Button getButtonInstance(Elevator E, int floor)
      {
         Button b =new Button(E, floor);
         return b;
      }
    }

     ==================

    public class FanFactory(){ .................}

     =====================

    public class LightFactory() { ........... }


     ==========================

What kind of relationship exist between the Elevator and Button class?

According to Kathy and Bert (SCJP) page 92 : HAS-A relationship are based on usage rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B.

In my example Elevator class code have a reference to a instance of Button and Button have a reference to instance of Elevator class.

Can anyone please clarify on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:38:00+00:00Added an answer on May 16, 2026 at 11:38 pm

    The Elevator has a button. Actually, it has four, but with each of those buttons, there’s a has-a relationship.

    Has-a is a somewhat informal term used to refer to two more formal kinds of relationship: association and aggregation. In both cases, one party in the relationship has a pointer to the other, but they’re distinguished by semantics: in an association relationship, the first party knows about the other, but doesn’t completely dominate it (think you and a colleague, or a boss, or a subordinate), whereas in an aggregation relationship, the latter party is part of the former party, and has no independent existence (think you and your liver). In this case, i’d say the Button is more specifically on the subordinate end of an aggregation relationship with the Elevator, not merely an association relationship.

    Other examples of association might be a Customer and a Salesman, or a Department and an Employee. Of aggregation, an Order and and OrderLine, or a Structure and a Component. Interesting corner cases are a Category and a Product, and an Order and an Invoice.

    One practical consequence of the kind of relationship is object lifetime: in an association, if the first object dies, the second may live, but in an aggregation, it will die. Think about your Elevator: if you deleted one (or removed it from your live data structures and let it be garbage collected, at least), would you want the Buttons to survive?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class TextBoxDerived : System.Web.UI.WebControls.TextBox { protected override void OnLoad(EventArgs e) { this.Controls.Add(new LiteralControl(Hello));
public class PhotoList : ObservableCollection<ImageFile> { public PhotoList() { } **//this is the line
public class Test { //Car c = null; // Is this better and is
public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Test1<Type> { public Type getCompositeMessage(Type... strings) { Type val = (Type) ;
public class Knowing { static final long tooth = 343L; static long doIT(long tooth)
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.