Created
April 27, 2025 08:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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