Create multiple getter methods for private fields
Create a class named Book
with two private fields: title
of type String and author
of type String. Write getter methods getTitle
and getAuthor
to retrieve the values of title
and author
respectively.
Harper Lee
Harper Lee
Harper Lee
George Orwell
George Orwell
George Orwell
F. Scott Fitzgerald
F. Scott Fitzgerald
F. Scott Fitzgerald
Herman Melville
Herman Melville
Herman Melville
Instructor solution
Think you've got it?
How many getter methods should a class have?
- A.
None, private fields should not be accessed.
- B.
Only one, which returns all the private fields.
- C.
One for each private field that needs to be accessed.
- D.
As many as there are public fields.
You may exit out of this review and return later without penalty.