Java doesn’t have a direct equivalent to Go’s struct embedding. Instead, we use composition to achieve similar functionality. Here’s how the concepts translate:
We define a Base class that corresponds to the base struct in Go.
Instead of embedding, we create a Container class that has a Base instance as a field.
We implement methods in Container that delegate to the Base instance. This allows us to access Base methods through Container, similar to how embedding works in Go.
In the main method, we create a Container instance and demonstrate how to access its properties and methods.
Java doesn’t have implicit interface implementation through composition. Instead, we use a functional interface (Supplier) to demonstrate a similar concept.
When you run this program, you’ll see output similar to the Go version:
This example shows how to achieve functionality similar to Go’s struct embedding in Java using composition and delegation. While the syntax and approach are different, the end result is quite similar in terms of behavior and structure.