0

Can Someone tell me with an example why an class should be defined inside an interface. The below is the simple code i was trying.

interface Watsapp
{
class A
{
    public void Validate()
    {

    }
};
abstract public  void SendText();
public void SendPic();
};

1 Answers1

0

its totally depends on logic requirements.
whenever we declare inner class, it treats as a data member so here also you can treat this class as a data member just assume scenario some one needs object of A inside Interface and there is no class right now. see eg.

public interface Watsapp
 {

   class A
    {
      public void Validate()
       {

       }
      public String iDoSomething()
      {
       return "i did";
      }

    };
   public A objOfA = new A();
   abstract public  void SendText();
   public void SendPic();
};

And main Class is bellow:

  public class TestMain {

   public static void main(String[] str){

      System.out.println( Watsapp.objOfA.iDoSomething());
    }

   } 

mostly people create anonymous class for one time use, but here You created a class with name.

see:

 public interface Watsapp
 {

    /*class A
    {
      public void Validate()
       {

       }
      public String iDoSomething()
      {
       return "i did";
      }

    };*/
   Thread t = new Thread() 
    {
    public void run() {
          // something ...  
         }
};
   abstract public  void SendText();
   public void SendPic();
};

Thank you.

v-jay
  • 63
  • 1
  • 1
  • 3