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