What is default method in java 8?

Before Java 8 if we implement any interface in any class then they are forcing to implement all method in that class.but from default method in java8 they are not forcing to implement that method in any class.here we are create two method in interface Test. one is calculate method without body and second is squaret method with body.

				
					interface Test {
	double calculate(int a);

	default double squaret(int a) {
		return Math.sqrt(a);
	}
}

public class DefaultMethodInterface {
	public static void main(String[] args) {
		Test f = new Test() {
			public double calculate(int a) {
				return squaret(a);
			}
		};
		System.out.println("calculate value is:- " + f.calculate(100));
		System.out.println("Square value is:- " + f.squaret(16));
	}
}

				
			

Output :-
calculate value is:- 10.0
Square value is:- 4.0

1 thought on “What is default method in java 8?”

Leave a Comment

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