Saturday, January 20, 2018

Interfaces in Java


  In the java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

interface advanceCalculation {
    public int divisor_sum(int n) ;
}
 class Myclass implements advanceCalculation{

    @Override
    public int divisor_sum(int n) {
    int sum=0;
        if(n>1) {
        for(int i=1;i<=n;i++) {
            if(n%i==0) sum+=i;
        }
    }
        return sum;
    }
}
public class Mycalculation {
    public static void main(String arg[]) {
    Myclass obj=new Myclass();
    int s=obj.divisor_sum(6);
   
    System.out.println("The sum of divisor = "+s);
         
}
}


No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...