Created
October 22, 2020 18:30
-
-
Save viveknaskar/4e748227d77587c42e60a8a5a6c3a43b to your computer and use it in GitHub Desktop.
Static Method Example in Interface (Java 8)
This file contains 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
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