creating an object from another class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: creating an object from another class

  1. #1
    Join Date
    Mar 2003
    Posts
    14

    creating an object from another class

    Could someone look at the following code:

    "import java.text.*;

    public class BookOrder{
    private Book book;
    private int quantity;
    private double total;

    private static int orderObjectCount = 0;

    public BookOrder(String bookCode, int bookQuantity){
    book = new Book(bookCode);
    quantity = bookQuantity;
    setTotal();
    orderObjectCount++;
    }

    public void setTotal(){
    total = quantity * book.getPrice();
    }

    public double getTotal(){ return total; }

    public Book getBook(){ return book; }

    public int getQuantity(){ return quantity; }

    public String toString(){
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    String orderString = "Code: " + book.getCode() + "\n"
    + "Title: " + book.getTitle() + "\n"
    + "Price: " + currency.format(book.getPrice()) + "\n"
    + "Quantity: " + quantity + "\n"
    + "Total: " + currency.format(total) + "\n";
    return orderString;
    }
    public static int getOrderObjectCount(){
    return orderObjectCount;
    }

    }"
    And then look at this code:
    "import java.text.*;
    public class DiscountBookOrder extends BookOrder{
    private String discountCode;
    private double subtotal, percentOff, total;

    public DiscountBookOrder(String bookCode, int bookQuantity, String keyCode){
    super(bookCode, bookQuantity);
    discountCode = keyCode;
    setPercentOff();
    setTotal();
    }

    public void setPercentOff(){
    if (discountCode.equalsIgnoreCase("a10"))
    percentOff = 0.1;
    else
    percentOff = 0.0;
    }

    public void setTotal(){
    subtotal = super.getQuantity() * super.getBook().getPrice();
    total = subtotal - (subtotal * percentOff);
    }

    public double getSubtotal(){ return subtotal; }
    public double getPercentOff(){ return percentOff; }
    public double getTotal(){ return total; }

    }"

    What I don't quite understand is how the DiscountBookOrder class does not need to name a BookOrder object as an instance variable even though it does create a BookOrder object, but the BooKOrder class needs to name a Book object as an instance variable. Is it because the DiscountBookOrder class inherits all of the BooKOrder classes public variables and methods? Also, do you always need to name another class's object as an instance variable when you want to use it's variables in another class or create an object from another class?

    Could someone help me to understand this?

    thanks

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    The DiscountBookOrder class does not need to name a BookOrder object as an instance variable because the DiscountBookOrder class inherits all of the BooKOrder class' public and protected variables and methods.

    I'm not quite sure what you are asking in the 2nd question.

    ArchAngel.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Mar 2003
    Posts
    14

    more clarity

    In the BookOrder class, a Book object is named as an instance variable. I think this is so a new Book object is created everytime a BookOrder class is created. My question was do you always need to name an object in another class as an instance variable when you want to create that object from another class?

    thanks

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    No. You only need to store a reference (sounds a bit better than "name an object") in an instance variable if you're going to use it later. In the case of 'Book', you do:

    Code:
      public void setTotal(){
        total = quantity * book.getPrice();
      }
    You actually already obey this rule in the toString() method:

    Code:
      String orderString = "Code: " + book.getCode() + ...
      return orderString;
    Here, you don't store 'orderString' as an instance variable - you just use a local variable. This variable will be garbage collected when this method is completed.

    ArchAngel.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Mar 2003
    Posts
    14

    another question

    You do need to reference an object from another class as an instance variable if you are going to create that object everytime another object is created, right?

    In the code above, everytime a BookOrder object was created, a Book object was created. The Book object was listed as a private instance variable for the BookOrder class. Does this always need to happen like this? I mean, in order to create a Book object everytime you create a BookOrder object, do you need to list the Book object as an instance variable?

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    Yes. Otherwise the object will become eligable for garbage collection. For example, if you had written:
    Code:
    public BookOrder(String bookCode, int bookQuantity){
      Book myLocalBook = new Book(bookCode);
      quantity = bookQuantity;
      setTotal();
      orderObjectCount++;
    }
    Here you don't store the reference to the newly created book object in the BookOrder class. This means that at the end of this method you will NEVER be able to get a reference to that particular object EVER again.

    Actually, in this case, it'll give you another problem because setTotal() NEEDS a reference to the Book in order to get its price, so you're screwed.

    ArchAngel.
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Mar 2003
    Posts
    14

    you have help me alot!

    Now, so in order for the Book object's variables to be used after you create a book object, the book object has to be listed as an instance variable that way the reference to the newly created Book object won't be destroyed after the constructor method creates it. The Book object's new values are stored as a variable.

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    Yes.
    ArchAngel.
    O:-)

  9. #9
    Join Date
    Mar 2003
    Posts
    14

    one more question

    In the DiscountBookOrder class, the BookOrder object does not need to be listed as an instance variable because it automatically imports all of the BookOrder's variables. The variables are imported before the newly created BookOrder object can be destroyed. Am I right?

  10. #10
    Join Date
    Mar 2003
    Posts
    834
    No. Not quite right...

    "because it automatically imports all of the BookOrder's variables"

    BookOrder's variables are "imported". DiscountBookOrder IS a BookOrder. It therefore INHERITS all of the methods and instance variables of the BookOrder's class.

    "The variables are imported before the newly created BookOrder object can be destroyed."

    I don't understand what you mean here. No BookOrder object is ever created for DiscountBookOrder.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center