Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Created October 22, 2020 18:30
Show Gist options
  • Save viveknaskar/4e748227d77587c42e60a8a5a6c3a43b to your computer and use it in GitHub Desktop.
Save viveknaskar/4e748227d77587c42e60a8a5a6c3a43b to your computer and use it in GitHub Desktop.
Static Method Example in Interface (Java 8)
package com.viveknaskar;
public class StaticMethodExample implements StaticMethodInterface {
// implementing abstract method
public void existingMethod(String str) {
System.out.println("String is: "+str);
}
public static void main(String[] args) {
StaticMethodExample obj = new StaticMethodExample();
// Calling the default method of interface
obj.defaultMethod();
// Calling the static method of interface
StaticMethodInterface.staticMethod();
// Calling the abstract method of interface
obj.existingMethod("Java 8 has added static methods and default methods");
}
}
interface StaticMethodInterface {
// This is a default method so we need not
default void defaultMethod() {
System.out.println("Hello from default method.");
}
/* This is a static method. Static method in interface is
* similar to default method except that we cannot override
* them in the implementation classes.
*/
static void staticMethod() {
System.out.println("Hello from static method");
}
void existingMethod(String str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment