In a number of languages, classes can expose fields, but interfaces cannot. In some cases, it may be useful to a class expose information via both its class type (in which case consumers could uses fields) and via an interface (in which case consumers must use methods). For
For example, a Location3d interface might define methods getX(), getY(), and getZ(), and a. A Point3d class might define fields X, Y, Z and also implement that interface. Other objects which have locations, but don't store the X, Y, and Z directly, might implement the interface to compute coordinates which are not stored inallow other entities to inquire about their position.
Continuing the example, a Point3dMonster (e.g.class in a game, a monster might expose an EnemyLocation whosewhich holds a reference back to the monster, and implements getX() etc. methods so they will continuously report the location of the monster's current enemy). If the EnemyLocation stored coordinates indirectly, or even if it held a reference to the current enemy's Point3d, the coordinates wouldn't get updated when a monster finds a new enemy to target. If, but ifhowever, its getX() etc. methods examine the monster to which it's attached monster, and read the present location of itsthat monster's present enemy, they can always report up-to-date data.