Intro to Object-Oriented Programming
Designing with UML and PlantUML

1 The Four Pillars: Why We Use OOP
We adopt Object-Oriented Programming (OOP) not because it’s a requirement, but because it provides powerful advantages in building complex, maintainable software. These benefits are often categorized into the four core “pillars” of OOP:
1.1 Encapsulation (Bundling and Hiding)
Encapsulation is the practice of bundling data (attributes) and the methods (actions) that operate on that data into a single unit, the class.
A key concept here is Information Hiding. We control access to an object’s internal state by declaring its fields as private and providing public methods (getters and setters) to interact with that state. This prevents direct, uncontrolled modification of the object’s data.
For example, a BankAccount class can encapsulate the balance field. A deposit(amount) method can contain logic to ensure the amount is positive, and a withdraw(amount) method can check for sufficient funds. This prevents the balance from being set to an invalid state from outside the object.
1.2 Abstraction (Simplifying the Interface)
Abstraction means exposing only essential features of an object while hiding the complex implementation details. It simplifies interaction by providing a clean, high-level interface.
Consider a Database class. The user of this class only needs to know about methods like connect(), query(sql), and disconnect(). They don’t need to understand the underlying details of network sockets, data serialization, or transaction management. This separation of interface from implementation allows the internal workings of the Database class to change without affecting the code that uses it.
1.3 Inheritance (Code Reuse and Hierarchies)
Inheritance allows a new class (subclass or child class) to adopt the properties and methods of an existing class (superclass or parent class). This models an “is-a” relationship.
For instance, Car, Truck, and Motorcycle classes can all inherit from a common Vehicle superclass. The Vehicle class would define common attributes (like speed, color) and methods (like startEngine(), stopEngine()), which are then automatically available in the subclasses, promoting code reuse.
1.4 Polymorphism (Many Forms)
Polymorphism, meaning “many forms,” allows objects of different classes to be treated as objects of a common superclass. It lets us write flexible and decoupled code.
A common way to achieve this is through method overriding. Following the Vehicle example, each subclass (Car, Truck) might provide its own specific implementation of a calculateFuelConsumption() method. You could then have a list of Vehicle objects and call this method on each one, and the correct implementation for the actual object’s type (Car or Truck) would be executed.
2 Java’s Toolkit for Robust Design
Java is designed to enforce these OOP principles, providing us with the tools we need for robust application design:
| OOP Concept | Java Mechanism | Example |
|---|---|---|
| Encapsulation | private access modifier and public Getters/Setters. |
Protecting a balance field in a BankAccount class and enforcing validation rules within deposit() and withdraw() methods. |
| Abstraction | Interfaces and Abstract Classes. | Defining a Shape interface with an area() method, allowing different shapes (Circle, Square) to be handled uniformly without knowing the specific area calculation formula for each. |
| Inheritance | The extends keyword. |
Creating specialized exception types, like InvalidTransactionException, that inherit from a more general BusinessException class. |
| Polymorphism | Method Overloading and Method Overriding. | A Graphics object using a draw(Shape s) method that can correctly render any object that implements the Shape interface, whether it’s a Circle or a Rectangle. |
2.1 Designing with Interfaces and UML
Interfaces (using the implements keyword) are the cornerstone of true abstraction and polymorphism in Java. An interface is a blueprint for a class. It defines a contract of methods that a class must implement.
Unified Modeling Language (UML) is a standardized way to visualize the design of a system. A UML Class Diagram is perfect for showing the relationships between classes and interfaces.
PlantUML is a tool that lets you create UML diagrams from simple text descriptions.
Here is a UML diagram showing an interface and a class that implements it. This is a common pattern for creating flexible, decoupled designs.
You can generate this diagram yourself using the online PlantUML editor and the following code.
Any class that implements Printable guarantees that it has those two methods. This allows us to write code that can operate on any Printable object, regardless of its specific concrete type.