Hey guys in this article, you will understand the defining the default in interface which in introduced in Java 8.
Read More:
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
- Check the Spring Boot JdbcTemplate Tutorials
Since Java 8, we can have method body in interface. But we need to make it default method.
interface Drawable {
void draw();
default void msg(){
System.out.println("default method");
}
}
class Rectangle implements Drawable{
public void draw(){
System.out.println("drawing rectangle");
}
}
public class TestInterfaceDefault{
public static void main(String args[]){
Drawable d = new Rectangle();
d.draw();
d.msg();
}
}
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.