Beginning Java Programming: The Object-Oriented Approach Chapter 7 Compilation Error

This is the live code sandbox for Beginning Java Programming: The Object-Oriented Approach Chapter 7. This example demonstrates how you cannot access a private member of a class from another class. In the program, the class ProductProgram tries to access myWidget.productPrice on line 4. This leads to a compilation error because the string productPrice is a private member of the class Product. Use the project explorer tree to the left of the code window to explore the additional classes. Click “Run” to run the example in your browser or edit the code and then click “Run” to run it with your changes.
You can download the complete code for Beginning Java Programming: The Object-Oriented Approach as well to work with it in your own code editor.

This example rectifies the error in the previous program.
It does so by using the Product class’s public setter method setPrice() to set the price instead of trying to set its private member productPrice directly.

Return to the complete list of live code sandbox examples for Beginning Java Programming: The Object-Oriented Approach

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *