I think it's important to understand proper OOP from a traditional sense, and what is necessary for data binding, etc. Java introduced the JavaBean (POJO with only getters and setters) to bind to the user interface. Most people are not building user interfaces in Java these days, but they still have their place. First, let's distinguish between OOP and traditional procedural programming:
Object Oriented Programming:
- Emphasizes data hiding (i.e. internal data is not accessible directly)
- Only exposes methods to manipulate the object
- SOLID principles
As an example, lets say we have a PlayerCharacter class for a game:
public class PlayerCharacter {
private int defense;
private int hitPoints;
public PlayerCharacter() {
defense = 5;
hitPoints = 15;
}
public PlayerState receiveHit(int attackStrength) {
int hitStrength = Math.Max(attackStrength - defense, 0);
hitPoints = Math.Max(hitPoints - hitStrength, 0);
return hitPoints > 0 ? PlayerState.Alive : PlayerState.Dead;
}
}
You'll notice that there is no way for any other class to know what the hitPoints
or defense
values are. The class is fully contained, making it opaque. The method receives all the external information it needs as parameters. This allows you to replace the very simplistic algorithm I have for calculating damage done with something more complex. This class represents OOP from the traditional interpretation.
Data Objects and Services (AKA Procedural with Objects)
The JavaBean approach is very much like a C struct
. You have all your object state available outside of the class, allowing other objects to modify it's state directly. It is entirely possible to write running applications that use this approach. In fact, it's actually quite common these days.
In writing code for the server, it is very common for developers to use Object-Relational Mapping (ORM) tools like hibernate. They bind database rows to POJOs, hiding the code to synchronize changes to the database. Again, the JavaBean approach is used for binding, but this time to the back end.
Services are then written to manipulate the data objects (i.e. the name spaces for functions).
This approach is not traditional OOP, but it works very well for a certain class of applications.
Your PHD Supervisor
Your professor is trying to teach you proper OOP. It's a valuable skill to have, and knowing the trade-offs will only help you build better systems in the future. There is a lot of strength in the traditional OOP approach, since it does lend itself better to reuse.
It's better to have more, but smaller self-contained objects with your professor. It also makes your code easier to spot errors in.
When you get in the real world, you'll have to make decisions based on how your team designed the code to work.