Saturday, August 27, 2016

The default magic in JDK 8

In this post, we'll talk about making use of methods in interface using default in JDK8. I'll keep this short and begin with interface. Java has been known dauntingly to allow only declarations in interfaces. Sure, there might have been some obscure workarounds, but never before has there been a native addressing to this particular pet-peeve of many-a-Java developers around the world. JDK 8 now allows you to use a default method to do just that! Yes, now you can finally define your methods in the interface itself and have the child-class get that information inherently. How cool is that, huh?

Say, for this code:
public interface NewInterface {
    //Normally methods can be defined in interface like this
    public void aNormalMethod();
    //But wouldn't it be convenient if Java allows methods 
    //to be defined and not just declared?? say, like this:
    default void theMethodImTryingToUse(){
        /* Some Lines of Code */
    }
}

Previously on source 1.7 (Java Platform JDK 1.7), you could get an error similar to this while attempting it:

Now with, Java 8 we have this sweet option to roll out our code.

Okay, since all the instructional-crap is behind us now, lets begin with the formal introductions with code and see it in action: |Click here to skip the fun and jump directly to the output|

Make the following classes and interfaces in your package
As it is apparent, in this example there are:
*Note: Siddhant is the child class and also the class that we're going to run as it is our main class to observe inheritance by default.


SOURCE CODE:
Father.java
public interface Father {
    public void a();
    public void b();
    default void c(){
        System.out.println("running: Method-C of Father Interface");
    }
    default void d(){
        System.out.println("running: Method-D of Father Interface");
    }
}

Mother.java
public interface Mother {
    default void w(){
        System.out.println("running: Method-W of Mother Interface");
    }
    default void x(){
        System.out.println("running: Method-X of Mother Interface");
    }
    public void y();
    public void z();
}  

AbstractFather.java
abstract class AbstractFather implements Father{
    @Override
    public void a(){
        System.out.println("running: Method-A of AbstractFather");
    }
}

AbstractMother.java
abstract class AbstractMother implements Mother {
    @Override
    public void z(){
        System.out.println("running: Method-Z of AbstractMother");
    }
}

SiddhantExtendsFather.java
public class SiddhantExtendsFather extends AbstractFather{
    @Override
    public void b() {
        System.out.println("running: Method-B of Abstracted Siddhant");
    }
}

SiddhantExtendsMother.java
public class SiddhantExtendsMother extends AbstractMother{
    @Override
    public void y() {
        System.out.println("running: Method-Y of Abstracted Siddhant");
    }
}

Siddhant.java (Our Main Class that we run)
public class Siddhant implements Father,Mother {
    @Override
    public void a() {
        System.out.println("running: Method-A of Siddhant");
    }

    @Override
    public void b() {
        System.out.println("running: Method-B of Siddhant");
    }

    @Override
    public void y() {
        System.out.println("running: Method-Y of Siddhant");
    }

    @Override
    public void z() {
        System.out.println("running: Method-Z of Siddhant");
    }
    
    public static void main(String[] args) {
        Siddhant s = new Siddhant();
        SiddhantExtendsFather sef = new SiddhantExtendsFather();
        SiddhantExtendsMother sem = new SiddhantExtendsMother();
        System.out.println("Printing Siddant's values\n");
        s.a();
        s.b();
        s.c();
        s.d();
        s.w();
        s.x();
        s.y();
        s.z();
        System.out.println("\nPrinting Abstracted Siddhant's values\n");
        sef.a();
        sef.b();
        sef.c();
        sef.d();
        sem.w();
        sem.x();
        sem.y();
        sem.z();
    };
}


Output:

References:
1. Oracle Docs - dive deep into the default method
2. Tutorials Point - read a little more about this topic

0 comments

Post a Comment