Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Last active October 22, 2020 18:22
Show Gist options
  • Save viveknaskar/4c0e5d25d75c841548cb4c14ba83aeeb to your computer and use it in GitHub Desktop.
Save viveknaskar/4c0e5d25d75c841548cb4c14ba83aeeb to your computer and use it in GitHub Desktop.
Default Method in Interface Example in Java 8
package com.viveknaskar;
// Class implementing the interface
public class DefaultMethodExample implements DefaultMethodInterface {
@Override
public void abstractMethod() {
System.out.println("Hello from abstract method");
}
public static void main(String[] args) {
DefaultMethodExample dm = new DefaultMethodExample();
dm.abstractMethod();
dm.defaultMethod();
}
}
interface DefaultMethodInterface {
// Abstract method
void abstractMethod();
// Default method having method body
default void defaultMethod() {
System.out.println("Hello, I am coming from default method in Interface");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment