home / study / engineering / computer science / computer science questions and a
ID: 3702898 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / stringy why are the fields private? why is tostring() tagged @override? why does stringy use ...
Question: Stringy Why are the fields private? Why is toString() tagged @Override? Why does Stringy us...
What's What? * What names are in Stringy's public interface? . What names in Stringy are not in the public inter- face? * Notice how step-by-step equals ) is. » What's that 00verrides? A Stringy object contains operations (methods) and state (variables) Is the StringBuilder sb part of the object's state? » Is the stringCount part of the object's state?Explanation / Answer
In case of an query, please leave a comment. I will resolve your issue.
Ques. Why are the fields private?
Ans. The fields are private so that they are not visible or accessible outside the class and no one can alter the attributes. It is a security measure to prevent the attributes being affected by other classes. This property is called encapsulation.
Ques. Why is toString() tagged @Override?
Ans. toString() method is a method defined in the Object class. This methods returns a String representation of the object. If we implement toString() method in our class, we have to override theoriginal method as each class inherits the Object class. So, we write the annotation
which reflects that the mathod is overridden.
Ques. Why does Stringy use a foreach in toString(), but not in equals() or in its constructor ?
Ans. Using for each loop , we can only access the elements, but can't change them. So, we didn't use for each loop in the constructor as there we were altering the values of chars.
We didn't use for each loop in equals() method as we were accessing the elements of chars at the same index in both the current object and the other String. As in for each loop, we don't get any index, so we used a normal loop to access elements using index.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.