Our first program demonstrates methods defined on struct types.
To run the program, compile and execute it using the javac and java commands:
In Java, methods are always defined within a class. The concept of receiver types doesn’t exist in Java as it does in some other languages. Instead, methods are automatically associated with the class they’re defined in.
Java uses object-oriented programming principles, where methods are typically defined within classes and operate on the class’s instance variables. This is similar to the struct methods in the original example, but with a more classical object-oriented approach.
Next, we’ll look at Java’s mechanism for defining abstract types and behaviors: interfaces.