-2

So I wanted to be a good friend and help someone I know with their Java classes, since I have had experience writing a few networking apps. (Java Not Being My First Language) So he handed me a bonus assignment he wanted help on. Everything else we seemed to nail down just fine, but the bonus assignment handed to my friend pretty much went against everything I was thought in any programming language. Obviously if you extend a class in any programming language. C++, Java, PHP, you can only access the sub class variables if they are protected OR public. Obviously if you are trying to access ANY thing that is private or trying to break the rules, you need to take a step back. ... Right?

Apparently the assignment was like this: You need to modify this program called the "InvoicePrinter" So that you can print the invoice in HTML and YOU CANNOT modify any of the classes ECXEPT for InvoiceFormatter

Here is how the program summed up.

//Address.java
class Address
{
   private String m_name;
   private String m_street;

  Address(String name, String street)
  {
     m_name = name;
     m_street = street;
  }

}

//Invoice.java
class Invoice
{
     private Address m_location;
     private Products m_products;

     Invoice(Address addr)
     {
       m_location = addr;
     }

     public void format()
     {
          //Print data here...
     }
}

//InvoicePrinter
class InvoicePrinter
{
     int main(//...)
     {
        Invoice myInvoice = new Invoice(new Address("My company", "12345 n Street Way"));
        myInvoice.addProduct(new Product("box", 1.20));
        myInvoice.addProduct(new Product("glasses", 5.00));


        //Print Invoice on screen...
        myInvoice.format();


       //Add your invoice printer here here...
     }

}

//InvoiceFormater.java
class InvoiceFormater
{
     private Invoice myInv;

     InvoiceFormater(Invoice invoice)
     {
           myInv = invoice;
     }


    public static void format()
    {
         //Print your HTML formatter here.

    }

}

So unless I'm slowly turning into a Java beginner or something, but the only thing I see how to be able to extract data from Invoice is extending "InvoiceFormatter" and using Super, but you still would need access to "Invoice" data, such as Address and etc... So how would one go about doing this without changing any of the code except for int main or InvoiceFormatter?

... I'll admit, this drove me a bit crazy just thinking if how it would work, since it sounds like a thing from Inception or a paradox... lol

ajm113
  • 936
  • 1
  • 8
  • 18
  • 2
    The assignment seems self contradictory; you said that `InvoicePrinter` is the only class you can modify, but the `Invoice.format` and `InvoiceFormatter.format` methods are asking you to implement them. – Colonel Thirty Two Dec 15 '14 at 15:56
  • Google java Reflection. – javaHunter Dec 15 '14 at 16:20
  • 1
    Your instinct is correct that this assignment seems nonsensical. The `Address` class, for example, is completely useless as written. There only sensible way forward is to modify the existing classes. There is also a lot of crap in this code, like `int main` which isn't Java, and `(//...)` which isn't a proper Java (or C or C++) comment. – David Conrad Dec 15 '14 at 16:27

2 Answers2

0

You cannot, by definition, access the private fields of another class (except inner or enclosing classes, but this is not the case here).

Let me elaborate: there seem to be no accessors in the Invoice class for the fields m_location and m_products, so you have really three options:

  • try to parse back those fields from the output of format() (this is like cheating)
  • change accessibility of the private fields run-time using the reflection API (this is even more like cheating)
  • convince the assigner, that the correct way is to modify the Invoice class
P.Péter
  • 1,527
  • 16
  • 39
0

You are able to obtain private class information using 'Reflection'. Reflection allows you obtain information about classes, like available methods and fields. But it also allows you the get the value of fields, or to call the method after you found out they exists.

For more information on reflection and for some tutorial you can visit the Reflection tutorials of Oracle.

On so there the following question and accepted answer might help you out with a specfific example of your problem: How do I read a private field in Java?

More information on reflection is also provided by the acceptedanswer of What is reflection and why is it useful?


Note, that I doubt that this is the intended solution of your teacher... From a teaching point of view, it seems to make more sense that you are also allowed to modify Invoice, as using reflection for these simple tasks (adding setters ad getters) is a huge overkill!

Community
  • 1
  • 1
Veger
  • 37,240
  • 11
  • 105
  • 116
  • 1
    I agree with you Veger, it's a bit insane, specially for Java 101. I'll refere my friend this info encase what the the teacher is telling him is true, or ether my friend is getting he's instructions mixed up, even though I asked him a number of times. – ajm113 Dec 15 '14 at 16:15