Java 8 and 9 Enhancement in Interface Part -1

Hello Friends,

In this post, we will take a look at Java 8 and Java 9 Enhancement with respect to Interface.

As we already know in the interface every method is always public and abstract whether we are declaring or not. We can't put the normal method and this is the limitation of interface prior to Java 8 version.

Java 8 version onwards give you the ability to put default method and static method as well.

Now the question comes to mind why we need default method and static method in Interface.
To give you an answer to this question lets take an example.

Step :

Assume we have an interface with the name School and that has one method syllabus.

public interface School {
public void syllabus();
}

Now assume we have ten different class that implement School interface and provide his own implementation for syllabus method.

public class Class1 implements School {

@Override
public void syllabus() {
System.out.println("Class First");
}
}


public class Class2 implements School {

@Override
public void syllabus() {
System.out.println("Class Second");
}
}

and up to class10.

Assume our programming requirement is we have to extend the functionality of this
interface by adding a new method ExtraSub.

public interface School {
public void syllabus();
        public void extraSub(); 
}


Once we added new method extraSub to the interface then all the implementation classes will be
affected and won't be compiled, because every implementation class should implement all
methods of interface.

Hence to overcome this problem in Java 8 default method concept came. which are also known as Defender methods or Virtual Extension Methods.


How to Declare Default Methods inside interfaces :

public interface School {

        default void extraSub(){
System.out.println("Additional subject");
}
public void syllabus();
}

So now Interface default methods are by default available to all implementation classes. Based
on requirement implementation class can ignore these methods or use these default methods directly or can override.

So we can say a default method gives you the ability to add an additional feature without affecting implementation class.

Now assume we have added multiple default method in the interface and some have common functionality so there may be duplicate code to overcome this type of problem Java 9 has private method concept in the interface.

Example

public interface School {

         default void extraSub() {
System.out.println("Only for Class 9 and 10");
common();
}

default void extraSub1() {
System.out.println("Only for Class 9 and 10");
common();
}

default void extraSub2() {
System.out.println("Only for Class 9 and 10");
common();
}

default void extraSub3() {
System.out.println("Only for Class 9 and 10");
common();
}

private void common(){
System.out.println("Common code for all the default method");
}
public void syllabus();
}

So in the above example, we have added separately the common code into a private method and call that private method from each default method which required that functionality.



Share this

Related Posts

Previous
Next Post »

1 comments:

Write comments