56.8k views
0 votes
Consider a Rational class designed to represent rational numbers as a pair of int's, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two int's), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private.

User Qjgods
by
4.6k points

1 Answer

2 votes

Answer:

There is not a reason for the two methods to be private unless you want to only allow objects of the class Rational to have access to these methods. Perhaps your professor has a particular reason for requiring it to be set up as such.

Step-by-step explanation:

The use of the private space simply restricts access to either variables or methods contained within the space to the class object that is associated with the variable or methods.

The use of the public space allows the method or variable to be accessed from anywhere within the program.

Note, if using class inheritance, classes that inherit from the Rational class will not be able to access the private functions.

User MarkusN
by
5.0k points