A shortnote on Java8 and its default interfaces

As Java evolved over the years, the connections presented in the Java library require adding new performance. If you add new techniques in the interface without having standard techniques function, all the sessions that already implemented the connections should go through a modify. This results in changing thousands of lines of code. To avoid this, the Java 8 presented the standard technique function. That is, if you want to add any performance to the current interface, you can add it by using the standard technique function without affecting the implementations.

Let us see some examples to understand this better. For example, I am declaring “BookInterface” with start and look functionalities. The execution sessions of this interface should provide the and look technique implementations.
package org.smarttechie;
// The interface is intended to open and read. The implementors should implement the methods to open and read.
// @author Siva Prasad Rao Janapati
public interface BookInterface {
// This method opens the book
public void openTheBook();
// The method reads the book
public void readTheBook();
}
Now, we will offer the execution to the above interface.
package org.smarttechie;
// The JavaBookImpl is the implementation of BookInterface
// @author Siva Prasad Rao Janapati
public class JavaBookImpl implements BookInterface {
// This opens the book
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
// This reads the book
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
}
Now we want to provide near performance to the guide interface.
If you add near performance to the guide interface, the current execution class should go through a modify. With the standard technique function, we can add near performance to the guide interface. The standard technique will be available to all the implementations.

package org.smarttechie;
// The interface is intended to open and read. The implementors should implement the methods to open and read.
// @author Siva Prasad Rao Janapati
public interface BookInterface {
// The method opens the book
public void openTheBook();
// The method reads the book
public void readTheBook();
// The default method implementation
public default void closeTheBook() {
System.out.println(“Closting the book”);
}
}
package org.smarttechie;
// The JavaBookImpl is the implementation of BookInterface
// @author Siva Prasad Rao Janapati
public class JavaBookImpl implements BookInterface {
// This opens the book
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
// This reads the book
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
public static void main (String[] args) {
BookInterface bookInter = new JavaBookImpl();
//Call the default method declared in BookInterface
bookInter.closeTheBook();
JavaBookImpl book = new JavaBookImpl();
book.closeTheBook();
}
}

Don't be shellfish...Email this to someonePrint this pageTweet about this on TwitterShare on StumbleUponShare on TumblrShare on LinkedInShare on RedditDigg thisShare on Google+Pin on PinterestShare on FacebookBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>