Quote:
Originally Posted by pewpewpew
by default constructor you mean the first constructor?
A default constructor is one that takes no parameters. The 20 gallon tank constructor should not take any parameters because it doesn't need to be passed any information.
Quote:
Originally Posted by Anais
our instructor told us that classes have data and methods, and to keep the data private.
Quote:
Originally Posted by pewpewpew
regarding the functions being private, our instructor told us to keep the data in classes private as a general rule, but maybe i misinterpreted what he meant.
Just saying that and not explaining why doesn't seem like great teaching.
What should be kept private is
implementation. The first car I owned didn't have power steering. When I upgraded to one that did, what happened behind the scenes when I turned the wheel was very different, but I didn't have to relearn anything. I still just turned the wheel and the car changed direction. This is an example of the implementation being hidden from me.
The reason data should be kept private is that data is always an implementation detail. For instance, your gastank class is keeping track of how much fuel is in it by storing a number, but there's no reason that would have to be the case for a gas tank. It could, for instance, calculate how much fuel is left from the weight of the tank. If you expose that stored number directly, then if you want to start calculating it with a method instead, it will break code that is using your class, which expects to be accessing an integer directly and not calling a method. Instead you should make the number itself private and expose it with a public method (or property, depending on the language). That way, changing how that number is provided just involves changing the code inside the method, and any code calling it can stay blissfully unaware that the implementation has changed.
"On request, I will tell you how much gas is left in me" is an abstraction. "I do this by internally keeping track of a number" is implementation. Abstractions public, implementations private.