Skip to content

Instantly share code, notes, and snippets.

@Sedose
Created April 27, 2025 08:35
Show Gist options
  • Select an option

  • Save Sedose/2c2aa56d9e77c8a5dcd213459189ad73 to your computer and use it in GitHub Desktop.

Select an option

Save Sedose/2c2aa56d9e77c8a5dcd213459189ad73 to your computer and use it in GitHub Desktop.
Default methods in Java interfaces give a slightly similar feeling to Kotlin extension functions, but they are not the same thing
public class Main {
public static void main(String[] args) {
User user = new User("Dora");
System.out.println(user.getGreeting());
}
}
interface UserOps {
default String getGreeting() {
return "Hello, " + getName() + "!";
}
String getName();
}
class User implements UserOps {
private final String name;
public User(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment