Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Default methods and static methods uses in Interface.

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 719
    Comment on it

    We know that interface provide methods without body or functionality but in abstract class we can provide default functionality of methods. So in interface we can't give method body and can't declare ant static method.

    But in Java 8 now we can declare static method and can declare some default methods using "default " keyword.

    So now there's virtually no difference between an interface and an abstract class other than multiple inheritance.

    We can change default method without changing any of its implementation.

    Here is the example:

    public class Example implements MyInterface {
    
        public static void main(String[] args) {
              // Your code here....
        }
    
        @Override
        public void calculate() {
            System.out.println("Calculation goin on...");
        }
    
    }
    
    interface MyInterface {
    
        public void calculate();
    
        default public void messages() {
            System.out.println("Hi what's goin on ?");
        }
    }

    Here it's not required to implement all methods of interface.

    we can also declare some static method in interface now

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: