java – Inner Class inside an Interface implementing same Interface, what we are achieving by this?-ThrowExceptions

Exception or error:

My Question:

I was looking at the source code of TextWatcher and I didn’t get the concept here.
What was the point of extending to NoCopySpan?

TextWatcher.java:

public interface TextWatcher extends NoCopySpan {
     public void beforeTextChanged(CharSequence s, int start, int count, int after);
     public void onTextChanged(CharSequence s, int start, int before, int count);
     public void afterTextChanged(Editable s);
}

NoCopySpan.java:

package android.text;

/**
 * This interface should be added to a span object that should not be copied into a new Spanned when performing a slice or copy operation on the original Spanned it was placed in.
 */
public interface NoCopySpan {
    /**
     * Convenience equivalent for when you would just want a new Object() for
     * a span but want it to be no-copy.  Use this instead.
     */
    public class Concrete implements NoCopySpan {}
}
How to solve:

NoCopySpan is just a marker interface. According to javadoc it is used to modify behavior of copy routine of Spanned objects (it relies on type of components). For example android.text.SpannableStringBuilder uses such inheritance information to skip spans copying upon construction.

This approach has some drawbacks but still pretty common. The reason of Concrete class existence is to provide convenient way to construct on-op dummy (or default) realization of NoCopySpan interface.

###

As for the specification of the interface NoCopyScan, the inner class implementing the same interface can be used as a default implementation/representation of the interface anywhere outside.

An inner class in an interface is treated as a static inner class implicitly. We can instantiate the interface’s inner class just like a static inner class and put NoCopyScan to use. Please find a simple example depicting the usage of default implementation of the interface with the help of the inner class below:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
interface NoCopy{

public void doStuff();

public class Conc {             //Inner Class
    public int retStuff(){
        return 2;
    }
    }




//  public void doStuff(){
//      System.out.println("Overriding in inner class");
//  }
}


 class ConcOut {

    public int returnStuff(){
        return 5;
    }


    public void doStuff(){
        NoCopy.Conc innerObj = new NoCopy.Conc();    //Instantiating inner class
        //NoCopy.Conc innerObj = (new ConcOut()).new Conc();

        System.out.println("overriding in outside class ConcOut "+ innerObj.retStuff());                              // calling the method of inner class
    }
}

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        ConcOut conObj = new ConcOut();
        conObj.doStuff();
        //ConcOut.Conc concInner = conObj.new Conc();

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *