I see no reason to make a separate Dogs
class, unless there are other operations to be performed on Dogs
as a group besides the standard collections operations of adding, removing, and accessing members. For example, if there was a releaseTheHounds()
method or a callEveryoneForDinner()
method, etc.
HOWEVER, it may be that what you were told was that you are supposed to use a List
type to declare listOfDogs
, rather than ArrayList
. That is, this:
List listOfDogs = new ArrayList<Dog>();
Instead of this:
ArrayList listOfDogs = new ArrayList<Dog>();
You are doing this already and it is a good practice. Here, the "encapsulation" is the hiding of ArrayList
as the implementor of the List
interface. This makes it easy to switch to another implementation easily (e.g. TreeList
) without having to change any code that accesses listOfDogs
.
Last but not least, you really should declare it this way:
List<Dog> listOfDogs = new ArrayList<Dog>();
Then you are ensuring that the List
holds Dog
instances and nothing else.