Default Method in Java 8
In this article, We will learn default method and static method in an interface in java 8 new features. There are many other features which are introduced in java 8 version but here we will focus only on default method and static method in java 8.
Now in java 8, we can declare default method in an interface.
When we use "default" keyword with a method is known as default method in java interface.
Java default method is a non-abstract method and we can declare default method with body.
We can override default method in java.
We can keep both abstract and default methods in an interface and static method also.
Syntax of default method:
class Demo
{
default void show()
{
//statements
}
}
Now in java 8, we can declare default method in an interface.
When we use "default" keyword with a method is known as default method in java interface.
Java default method is a non-abstract method and we can declare default method with body.
We can override default method in java.
We can keep both abstract and default methods in an interface and static method also.
Syntax of default method:
class Demo
{
default void show()
{
//statements
}
}
Java 8 Examples - default method in interface
In this java 8 interface default method example, We will use default method and abstract method in an interface.
interface MyInterface
{
void show();//abstract method
default void display()//default method
{
System.out.println("This is default method in java 8");
}
}
class Test implements MyInterface
{
public void show()//give specific implementation
{
System.out.println("This is show method");
}
public static void main(String args[])
{
Test t = new Test();
t.show();
t.display();
}
}
Output: This is show method
This is default method in java 8
Output: This is show method
This is default method in java 8
Java 8 Interface Static Method
In Java 8, We can declare a static method in an interface before java 8 version it is not possible. Let's understand with an example of static method in an interface where we will declare abstract, default and static methods in an interface.
interface MyInterface
{
//declaring abstract method
void show();
//declaring default method
default void display()
{
System.out.println("This is default method of java 8");
}
//declaring static method
static void watch()
{
System.out.println("This is static method in java 8");
}
}
class Test implements MyInterface
{
//give the implementation of abstract method show()
public void show()
{
System.out.println("This is show method");
}
public static void main(String args[])
{
Test t = new Test();
t.show();//calling show method
t.display();//calling default method
MyInterface.watch();//calling static method of interface
}
}
Output: This is show method
This is default method of java 8
This is static method in java 8
Now you can declare a static method in java interface.
Output: This is show method
This is default method of java 8
This is static method in java 8
Now you can declare a static method in java interface.
Java 8 - Abstract Class Vs Java 8 Interface
Abstract Class
- We can't achieve fully abstraction through abstract class.
- We can put the constructor in an abstract class.
Java 8 Interface
- We can achieve full abstract through java 8 interface.
- We cannot put the constructor in java 8 interface.
Java 8 Default Method with Multiple Inheritance
Here we will see default method with multiple inheritance through the interface. Here we will take two interfaces and define default method with the same name or same signature in both the interfaces and implement both interfaces and call default method.
Let's see what happens?
interface MyInterface1
{
void show();
default void method()
{
System.out.println("default from MyInterface1");
}
}
interface MyInterface2
{
void showAgain();
default void method()
{
System.out.println("default from MyInterface2");
}
}
class Test implements MyInterface1,MyInterface2
{
public void show()
{
System.out.println("show from MyInterface1");
}
public void showAgain()
{
System.out.println("showAgain from MyInterface2");
}
public static void main(String args[])
{
Test t =new Test();
t.method();
}
}
Output: compile time error
Now above example will throw compile time error because same name methods(default methods) in both interfaces and the compiler is not sure which method to be invoked.
Let's solve this error in given below example by implementing default method in the implementation class.
public void show()
{
System.out.println("show from MyInterface1");
}
public void showAgain()
{
System.out.println("showAgain from MyInterface2");
}
public static void main(String args[])
{
Test t =new Test();
t.method();
}
}
Output: compile time error
Now above example will throw compile time error because same name methods(default methods) in both interfaces and the compiler is not sure which method to be invoked.
Let's solve this error in given below example by implementing default method in the implementation class.
interface MyInterface1
{
void show();
default void method()
{
System.out.println("default from MyInterface1");
}
}
interface MyInterface2
{
void showAgain();
default void method()
{
System.out.println("default from MyInterface2");
}
}
class Test implements MyInterface1,MyInterface2
{
public void show()
{
System.out.println("show from MyInterface1");
}
public void showAgain()
{
System.out.println("showAgain from MyInterface2");
}
public void method()
{
System.out.println("default method run");
}
public static void main(String args[])
{
Test t =new Test();
t.method();
}
}
Output: default method
Read More About Java 8 Features, link given below
Java 8 Lambda Expressions.
Java 8 Date and Time API.
Java 8 StringJoiner class with Examples.
What is Functional Interface in Java 8 with Examples?
What is Interface in Java with Example?.
What is Java Abstract Class and How to Define it?.
I hope, This java 8 features -default and static method in interface will be useful to you.
public void show()
{
System.out.println("show from MyInterface1");
}
public void showAgain()
{
System.out.println("showAgain from MyInterface2");
}
public void method()
{
System.out.println("default method run");
}
public static void main(String args[])
{
Test t =new Test();
t.method();
}
}
Output: default method
Read More About Java 8 Features, link given below
Java 8 Lambda Expressions.
Java 8 Date and Time API.
Java 8 StringJoiner class with Examples.
What is Functional Interface in Java 8 with Examples?
What is Interface in Java with Example?.
What is Java Abstract Class and How to Define it?.
I hope, This java 8 features -default and static method in interface will be useful to you.
0 comments:
Post a Comment