Java 8 New Features

Java 8

Java 8 is the biggest change to the language since its inception, bigger than Java 5. It introduces many new language features including lambda expressions and method references.

New Features
Default and static Interface methods
Lambda Refrences
Method references

New Libraries
Streams
date/time
Concurrency updates

JVM Changes

PermGen Disappears

Default Static Methods

Pre-Java 8 interfaces only contain
– contacts
– method signatures

Post-Java Interfaces can contain default methods
– methods marked by keyword default
– with method body

public interface A {
	void method1();

	default void method2() {
		System.out.println("Default Method");
	}
}

public class Myclass implements A {
	@Override
	public void method1() {

	}
}

A class can implement zero or more interfaces. It must provide the implementation for each method in those interfaces. This is fine except when the interface is updated but the class is not intended for update.
In this case, the class will not compile – as it does not provide an implementation for the new method.
This is where the use of default methods comes in

In the above interface, method 2 is marked as default hence during implementation user does not need to provide the implementation for the same.

Multiple Interfaces can have the same default methods

Java class can implement one or more interfaces. The same default method signature may be present in more than one interface. In such cases implementing class will not compile unless it overrides the duplicate method.

interface IntC {
	void method1();

	
	default void method2() {
		System.out.println("Default Method- Interface C");
	}

	interface IntB {
		void method1();

		default void method2() {
			System.out.println("Default Method- Interface B");
		}

		class ImplementationClass implements IntC, IntB {

			@Override
			public void method1() {

			}

			@Override
			public void method2() {
				System.out.println("Default Method- Class");
			}
		}

As shown in the above example Interface B and C both have method 1 as the default method. ImplemenatatonClass needs to provide override method 2 to compile.

Interface method can be called if Required

In some cases, it may be useful to call one or more of the default implementations of a default method – even when there are multiple versions in the inheritance hierarchy. This can be achieved by prefixing the call to super type with the interface providing the required implementation

public interface InterfaceB {
	void method1();

	default void method2() {
		System.out.println("Default Method- Interface B");
	}
}
public interface InterfaceC {
	void method1();


	default void method2() {
		System.out.println("Default Method- Interface C");
	}
}

public class Myclass implements InterfaceB,InterfaceC {


	@Override
	public void method1() {

	}
	@Override
	public void method2() {
		InterfaceB.super.method2();
	}
}

In the above example, my class implements two interfaces both of which have method 2 as the default method. in this case, the implementation of any interface can be called by adding a prefix as super.

Static Interface Methods

In Java 8 it is possible to define methods in the interface. These methods with the keyword static as part of their signature (and are by default public ).

Static Interface methods are like static class methods(here they belong to the Interface only)

A static method belongs only to an Interface type, it is therefore only possible to invoke static methods by reference to the interface in which they are defined; and not via any classes that implement that Interface.

It is also not possible to override the method in the implementing class (as it does not see the static method).

public interface StaticInterface {
	int method1();

	static int methodstatic() {
		return 10;
	}
	
}

Interface with Static method


Comments

8 responses to “Java 8 New Features”

  1. Hello there, just was alert to your blog via Google, and located that it’s truly informative. I am gonna watch out for brussels. I’ll be grateful in case you proceed this in future. A lot of other folks might be benefited from your writing. Cheers!

  2. You actually make it seem so easy together with your presentation but I to find this topic to be actually something which I feel I might never understand. It kind of feels too complex and very huge for me. I am taking a look ahead on your subsequent submit, I will attempt to get the hold of it!

  3. Some really nice stuff on this web site, I like it.

  4. you are really a good webmaster. The web site loading speed is incredible. It seems that you are doing any unique trick. In addition, The contents are masterwork. you have done a excellent job on this topic!

  5. Good – I should definitely pronounce, impressed with your web site. I had no trouble navigating through all tabs and related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your customer to communicate. Nice task..

  6. I’d must check with you here. Which isn’t one thing I often do! I get pleasure from reading a put up that may make individuals think. Additionally, thanks for allowing me to remark!

  7. Thank you for sharing excellent informations. Your site is very cool. I am impressed by the details that you¦ve on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched everywhere and simply could not come across. What a perfect web site.

  8. I truly enjoy reading through on this web site, it has got wonderful articles. “I have a new philosophy. I’m only going to dread one day at a time.” by Charles M. Schulz.

Leave a Reply to zoritoler imol Cancel reply

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