Search Logic Blocks

Monday, March 29, 2021

Java: Nested Interface in an Interface

We had talked about interfaces and their fully / partial implementations. We also talked about Nested Interface in a Class (declaring an interface in a class) and Nested Class in an Interface (declaring a class in am interface). Let's declare an interface inside an interface (Nested Interface in an Interface) -

The inner interface class instance can be used three ways -
  • instance methods can be called directly
  • instance is passed to interface variable by reference and then methods are called through that variable
  • instance is passed to the outer interface method which takes a parameter interface variable

All three examples are shown in the code given below -
// Outer Interface
public interface OuterInterface {
public void display();
public void showInner(InnerInterface i);

// Inner Interface
public interface InnerInterface {
public void display();
}
}
// Implementation of Outer Interface
public class OuterInterfaceClass implements OuterInterface {
public void display() {
System.out.println("Display method from OuterInterfaceClass");
}

public void showInner(InnerInterface i) {
System.out.println("Show method from OuterInterfaceClass");
i.display();
}
}
// Implementation of Inner Interface
public class InnerInterfaceClass implements OuterInterface.InnerInterface {
public void display() {
System.out.println("Display method from InnerInterfaceClass");
}
}
// Demo class showing accessing inner interface in all three ways
public class Test {
public static void main (String args[]) {
// Creates a an object of class implementing OuterInterface
OuterInterfaceClass oic = new OuterInterfaceClass();
oic.display();
System.
out.println("___________________________________________________");

// Creates a an object of class implementing InnerInterface
InnerInterfaceClass iic = new InnerInterfaceClass();
iic.display();
System.
out.println("___________________________________________________");

// Creates a a variable of inner interface and assign the class variable By Reference
OuterInterface.InnerInterface oiii;
oiii = iic;
oiii.display();
System.
out.println("___________________________________________________");
// Calls the outer interface method which accesses inner interface
oic.showInner(iic);
}
}
Let's explore all three options -

InnerInterfaceClass is created by implementing inner interface InnerInterface directly.
// Implementation of Inner Interface
public class InnerInterfaceClass implements OuterInterface.InnerInterface {
public void display() {
System.out.println("Display method from InnerInterfaceClass");
}
}
1. instance methods can be called directly -

Create an instance of class implementing inner interface and then call interface methods:
// Creates a an object of class implementing InnerInterface
InnerInterfaceClass iic = new InnerInterfaceClass();
iic.display();
2. instance is passed to an interface variable by reference and then methods are called through that variable -

Declare a variable of inner interface and assign the inner class variable (Using class reference), call the method through interface variable:
// Creates a a variable of inner interface and assign the class variable By Reference
OuterInterface.InnerInterface oiii;
oiii = iic;
oiii.display();
3. instance is passed to the outer interface method which takes a parameter interface variable -

Creates an instance of class implementing outer interface and then call interface methods passing inner interface as a parameter:
// Creates a an object of class implementing OuterInterface
OuterInterfaceClass oic = new OuterInterfaceClass();
// Calls the outer interface method which accesses inner interface
oic.showInner(iic);
Here is the output of the above code -

Display method from OuterInterfaceClass
___________________________________________________
Display method from InnerInterfaceClass
___________________________________________________
Display method from InnerInterfaceClass
___________________________________________________
Show method from OuterInterfaceClass
Display method from InnerInterfaceClass

Code can be accessed on Github Link.

Tuesday, March 23, 2021

Java: Nested Class in an Interface

We already discussed about interfaces, partial interface and nested interface in a class. In this post, we will learn, how we can declare a class inside an interface. The outer interface can access the inner class in its own methods. Suppose there is an interface named as Magazine, and it has inner class named as Article. A class MagazineDemo implements the interface Magazine and creates an instance of inner class Article -

// Interface Magazine - prototype of a magazine
public interface Magazine {

// Prints the article
void postArticle(Article a);

// Gets the number of characters in an Article
int getArticleLen(Article a);

// Gets the number of words in an Article
int getArticleWords(Article a);

// Class Article in the interface Magazine
class Article {
String title;
String author;
String content;

// Article class constructor
Article(String title, String author, String content) {
this.title = title;
this.author = author;
this.content = content;
}
}
}
The interface Magazine is implemented by a demo class MagazineDemo. The main() method creates an instance of inner class Article and calls the implemented methods to print the Article details -
import java.util.Scanner;

// Implements the interface Magazine
public class MagazineDemo implements Magazine {
// Prints the Article
public void postArticle(Article a) {
System.out.println("title: " + a.title);
System.out.println("author: " + a.author);
System.out.println("content: " + a.content);
}

// Implements the interface method - getArticleLen(Article a)
public int getArticleLen(Article a) {
int len = a.content.length();
System.out.println("Article has " + len + " characters.");
return len;
}

// Implements the interface method - getArticleWords(Article a)
public int getArticleWords(Article a) {
String words[] = a.content.split(" ");
int wordCount = words.length;
System.out.println("Article has " + wordCount + " words.");
return wordCount;
}

public static void main(String args[]) {
System.out.print("Enter title:");
Scanner sc = new Scanner(System.in);
String title, author, content;
title = sc.nextLine();

System.out.print("Enter author name:");
author = sc.nextLine();

System.out.print("Enter content:");
content = sc.nextLine();
System.out.println("----------------");

MagazineDemo d = new MagazineDemo();
Article a = new Article(title, author, content);
d.getArticleLen(a);
d.getArticleWords(a);
d.postArticle(a);
}
}
Here is the example of output -

Enter title:Implementing Interfaces
Enter author name:Herbert Schildt
Enter content:Once an interface has been defined, one or more classes can implement that interface.
----------------
Article has 85 characters.
Article has 14 words.
title: Implementing Interfaces
author: Herbert Schildt
content: Once an interface has been defined, one or more classes can implement that interface.

The code can be found at Github Link.