Java 8 Concepts and Understanding..1

@FunctionalInterface
public interface MyFunctionalInterface{
void onlyOneAbstractMtd();
default void method1(){
System.out.println(“My Mtd 1”);
}
default void method2(){
System.out.println(“My Mtd 2”);
}
}
class Test{
public static void testImpl(){
System.out.println("Demo of method Referenceing");
}
class MethodRefDemo{
public static void main(String arg[]){
MyFunctionalInterface fI =Test :: testImpl;
MyFunctionalInterface.onlyOneAbstractMtd();
}
}
class MethodRefDemo{
public static void main(String arg[]){
MyFunctionalInterface fI = () -> System.out.println("FunctionalInterface implementation");
fI.onlyOneAbstractMtd();

}
}
Ex: public interface MyDefaultInterface{
default void myPrint(){
System.out.println("Default Intefrace");
}
}
public class UseDefaultInterface implements MyDefaultInterface{
public static void main(String arg[]){
UseDefaultInterface useme=new UseDefaultInterface();
useme.myPrint();
}
public class OverrideDefaultMtd implements MyDefaultInterface{
public void myPrint(){ <-------note the access modifier default is public in class.
System.out.println("Override Default Method");
}
public static void main(String arg[]){
OverrideDefaultMtd o=new OverrideDefaultMtd();
o.myPrint();
}
ex: public interface MyStaticInterface{
static void myStaticMtd(){
System.out.println("My Static Method");
}
}
public class Utility implements MyStaticInterface{
public static void main(String arg[]){
MyStaticInterface.myStaticMtd(); --> no object creation.
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Swapnil Watkar

Swapnil Watkar

Hi ! I am a Software Engineer, experienced in Mobile and Web applications Also AWS Certified Solutions Architect — Associate