Java Java Data Structures Efficiency! Custom Serialization

Evgeniia Vakarina
Evgeniia Vakarina
3,317 Points

getTitle() vs protected

Why is it better to make song's mTitle protected instead of just using getTitle() method?

1 Answer

Wout Ceulemans
Wout Ceulemans
17,601 Points

I think Craig made the fields protected to either (or both):

  • Show that there is a protected access modifier and show a potential use case
  • Personal code style

There is no real performance difference between using a field directly (like Craig did in the video) or using a getter. In the end, it comes down to personal choice. In general, getters are recommended by convention over direct field access (encapsulation). However, I think this is one of these special cases where you could use either.