Search Logic Blocks

Thursday, April 22, 2021

Java: Static Methods in an Interface

In our last post - Default Methods in an Interface, we saw that how we can add a default method to an existing interface and how we can override the default method in the classes, which are implementing the interface. Like a regular class, we can also have a static method in an interface and can be accessed the same way. There is no need to implement or extend the interface to access the static method.

Let's create an interface which represents an array of integer numbers. Array can be series of any kind of numbers - Prime numbers, Fibonacci numbers, Even numbers, Odd numbers or any other pattern. The interface provides a default method to create a list of integers, methods to implement the interface and a static method to display the group data in a standard way.


Interface Group - (Group.java)
public interface Group {
public int getNextNo();

public int[] getList();

public String getName();

default public int[] createGroup(int n) {
int num[] = new int[n];
for(int i = 0; i < n; i++) {
num[i] = getNextNo();
}
return num;
}

static public void displayGroup(Group g) {
int num[] = g.getList();
System.out.println(g.getName());
int n = num.length;
for(int i = 0; i < n; i++) {
System.out.print(num[i] + " ");
}
System.out.println(" ");
}
}
Methods defined in the interface -
public int getNextNo();
getNextNo() method is used to generate the new number for the particular group. This method will be implemented different for every group of numbers - prime numbers, Fibonacci numbers, etc.
public int[] getList();
getList() method returns the array of integers numbers generated in the particular group - prime, fibonacci, etc.
public String getName();
getName() method returns the name of the particular list.
default public int[] createGroup(int n) {
int num[] = new int[n];
for(int i = 0; i < n; i++) {
num[i] = getNextNo();
}
return num;
}
createGroup() method is the default method to create the list of numbers. The implementing class does not need to implement the createGroup() method, but can override the method as needed.
static public void displayGroup(Group g) {
int num[] = g.getList();
System.out.println(g.getName());
int n = num.length;
for(int i = 0; i < n; i++) {
System.out.print(num[i] + " ");
}
System.out.println(" ");
}
displayGroup() method displays the list in a particular way. This method is static method, so can be accessed directly. This method can not be overridden. In the implementing class, if there is any method with the same name, it will be totally different method and there will be no relation to static method in the interface.

Class Prime (Prime.java) for prime numbers - getNextNo() method returns the next prime number in the list
public class Prime implements Group {
int lastPrime = 1;
int groupSize = 0;
int primeList[];

Prime(int n) {
groupSize = n;
primeList = createGroup(n);
}

public int getNextNo() {
if(lastPrime >= 3) {
boolean foundNext = false;
int temp = lastPrime;
while(!foundNext) {
temp = temp + 2;
if(isPrime(temp)) {
lastPrime = temp;
foundNext = true;
}
}
} else if (lastPrime == 1) lastPrime = 2;
else if (lastPrime == 2) lastPrime = 3;
return lastPrime;
}

public int[] getList() {
return primeList;
}

public String getName() {
return "Prime Numbers List: ";
}

public boolean isPrime(int n) {
boolean prime = true;
for(int j = 3; j < n; j = j + 2) {
if((n % j) == 0) {
prime = false;
break;
}
}
return prime;
}
}
Class Fibo (Fibo.java) for Fibonacci numbers - getNextNo() returns the next Fibonacci number in the list
public class Fibo implements Group {
int lastFibo1 = 0;
int lastFibo2 = 0;
int groupSize = 0;
int FiboList[];

Fibo(int n) {
groupSize = n;
FiboList = createGroup(n);
}

public int getNextNo() {
if(lastFibo1 >= 1 && lastFibo2 >= 1) {
int temp = lastFibo2;
lastFibo2 = lastFibo2 + lastFibo1;
lastFibo1 = temp;
} else if (lastFibo2 == 0) lastFibo2 = 1;
else if (lastFibo2 == 1) {
lastFibo1 = 1;
}
return lastFibo2;
}

public int[] getList() {
return FiboList;
}

public String getName() {
return "Fibonacci Numbers List: ";
}
}
Class GroupDemo (GroupDemo.java) - Demos how to access the interface Group, to access its default and static methods, and its implementing classes (Prime, Fibo, Even and Odd) -
(NOTE: Above, I have not included code for classes Even and Odd. But these classes can be accessed at Github.)
public class GroupDemo {
public static void main(String args[]) {
Group g1 = new Prime(10);
Group.displayGroup(g1);
Group g2 = new Fibo(10);
Group g3 = new Odd(10);
Group.displayGroup(g2);
Group.displayGroup(g3);
Group g4 = new Even(10);
Group.displayGroup(g4);
}
}
Output

Prime Numbers List: 
2 3 5 7 11 13 17 19 23 29  
Fibonacci Numbers List: 
1 1 2 3 5 8 13 21 34 55  
Odd Numbers List: 
3 5 7 9 11 13 15 17 19 21  
Even Numbers List: 
2 4 6 8 10 12 14 16 18 20  

All the code can be accessed at Github Link.

No comments: