The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java 8 Interface Static and Default Methods

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java 8 Interface Static and Default Methods Posted: Feb 22, 2015 5:28 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java 8 Interface Static and Default Methods
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Java 8 introduced two new methods to declare they are
    1.default methods
    2.static methods
  • By this interfaces and abstract class are same but still having differences like abstract class can have a constructor etc will discuss more before that we need to know about these Java 8 features of interfaces.
  • Defaults methods are also called as defender methods can be implemented inside the interface
  • Like normal class now with java 8 we can declare static methods in side a interface.
  • Lets jump deep into Java 8 default and static methods 

  

1.Interface Default Methods in Java 8

  • Before Java 8 in interfaces we can and able to declare only abstract methods only.
  • If we declare a method without abstract that will be treated as abstract by default.
  • As we know all methods in interfaces are by default abstract methods.
  • These methods wont have body means implementaions
  • The class which is implementing this interface need to provide body / implementation for this abstract methods.
  • Now with java 8 default methods we can add methods to interface without disturbing existing functionality.
  • So instead of overriding now we can inherit these default methods from interfaces


  1. package com.instanceofjava;
  2. interface Java8Interface{
  3.  
  4. abstract void show();
  5.   
  6. default void display(){
  7.  
  8. System.out.println("default method of interface");
  9.  
  10. }
  11.  
  12. }

 

  1. package com.instanceofjava;
  2. class Sample implements Java8Interface {
  3.  
  4. void show(){
  5. System.out.print("overridden method ")
  6.  }
  7. public static void main(String[] args){
  8.   
  9. Sample obj= new Sample();
  10.  
  11. obj.show(); // calling implemented method
  12. obj.display(); // calling inherited method
  13. Java8Interface.display(); calling using interface name
  14.  
  15. }
  16.  
  17. }

Output:


  1. overridden method
  2. default method of interface
  3. default method of interface

How to call default methods:

  • We can all these default methods by using interface name and also by using object of the class which is implementing.
  • From above example
  • obj.show(); // calling implemented method
  • obj.display(); // calling inherited method
  • Java8Interface.display(); calling using interface name

Can we override java 8 default method

  • As we discussed above default methods in interfaces are implemented methods with bodies
  • Yes we can override same method in class which is implementing this interface.
  • Lets see one sample program how to override and what happens if we override


  1. package com.instanceofjava;
  2. interface InterfaceWithDefault{
  3.  
  4. default void defMethod(){
  5.  
  6. System.out.println("default method of interface");
  7.  
  8. }
  9.  
  10. }


  1. package com.instanceofjava;
  2. class Demo implements InterfaceWithDefault{
  3.  
  4. void defMethod(){
  5.  
  6. System.out.print("overridden method in class Demo ") 
  7.  
  8.  }
  9. public static void main(String[] args){
  10.   
  11. Demo obj= new Demo();
  12.  
  13. obj.defMethod(); // calling overridden method
  14. Java8Interface.defMethod(); calling using interface name : interface defMethod will be called
  15.  
  16. }
  17.  
  18. }

Output:


  1. overridden method in class Demo
  2. default method of interface

What happens if we implement two interfaces having same default methods

  • Now lets see if a class implementing two interfaces which are having same default methods
  • Whatever the implementation in the two interfaces defined if we implementing two interfaces which are having a default method in both then compilation error will come if two methods have same signature. works fine if two methods have same name with different arguments.
  • Check the below example programs to understand more.


  1. package com.instanceofjava;
  2. interface A{
  3.  
  4. default void defMethod(){
  5.  
  6. System.out.println("default method of interface: A");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. interface B{
  3.  
  4. default void defMethod(){
  5.  
  6. System.out.println("default method of interface: B");
  7.  
  8. }
  9.  
  10. }



  1. package com.instanceofjava;
  2. class Demo implements A, B{ // compilation error will come
  3.  
  4. public static void main(String[] args){
  5.   
  6. Demo obj= new Demo();
  7.  
  8.  
  9. }
  10.  
  11. }


  • If we implement two interfaces which are having same method with same parameters then compilation error will occur.
  • Duplicate default methods named "defMethod" with the parameters () and () are inherited from the types A and B.
  • If we define two methods with different type of parameters then we can work with both interfaces.



  1. package com.instanceofjava;
  2. interface A{
  3.  
  4. default void defMethod(){
  5.  
  6. System.out.println("Default method of interface: A");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. interface B{
  3.  
  4. default void defMethod(String str){
  5.  
  6. System.out.println("Default method of interface: B");
  7. System.out.println(str);
  8.  
  9.  
  10. }
  11.  
  12. }



  1. package com.instanceofjava;
  2. class Demo implements A, B{ // compilation error will come
  3.  
  4. public static void main(String[] args){
  5.   
  6. Demo obj= new Demo();
  7. obj.defMethod();
  8. obj.defMethod("Java 8")
  9.  
  10.  
  11. }
  12.  
  13. }

Output:

  1. Default method of interface: A
  2. Default method of interface: B 
  3. Java 8











Read: Java 8 Interface Static and Default Methods

Topic: JBoss Forge NetBeans integration – Getting Started Previous Topic   Next Topic Topic: Playing with Weld-Probe – See All of your CDI Aspects in One Place

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use