Question

I've been having a bit of trouble with this problem. The question:

Write a complete Java program that does the following:

  • declares interfaces I1 and I2, both with empty bodies
  • declare interface I3 with an empty body, which extends both of the above interfaces
  • declare interface I4 with an empty body
  • class X implements I3 with an empty body
  • class W with an empty body implements I4 and extends X
  • create a class InstanceofTest that does the following in the main():
    • Create an object w of class W.
    • Use the instanceof operator to test if the object w implements each of the interfaces and is of type X, and display the appropriate message.

So this is what I've come up with so far:


public interface I1
{

}

public interface I2
{

}

public interface I3 extends I1, I2
{

}

public interface I4
{

}

public class W extends X implements I4
{

}

I'm having a bit of confusion with the InstanceofTest method. I know that the instanceof operator would tell you if a certain object is an instance of a certain type, like when you do it like this:


public class InstanceofTest
{

    public static void main(String[] args)
    {
        W w = new W();
        if (w instanceof X)
        {
            System.out.println("w is an instance of X.");
        }
    }
}

The problem I'm having is using instanceof to see if w implements each of the interfaces. I have no idea how I would go about doing that. Any help would be greatly appreciated!


Edit: So, should I do it like this?


public class InstanceofTest
{
    public static void main(String[] args)
    {
        W w = new W();
        if (w instanceof X)
        {
            System.out.println("w is an instance of X.");
        }

        if (w instanceof I1)
        {
            System.out.println("w implements I1.");
        }

        if (w instanceof I2)
        {
            System.out.println("w implements I2.");
        }

        if (w instanceof I3)
        {
            System.out.println("w implements I3.");
        }

        if (w instanceof I4)
        {
            System.out.println("w implements I4.");
        }
    }
}

Was it helpful?

Solution

Well, here is the full solution for you:

public interface I1 {}

public interface I2 {}

public interface I3 extends I1, I2 {}

public interface I4 {}

public class X implements I3 {}

public class W extends X implements I4 {}

public class InstanceofTest {
  public static void main(String[] args){
      W w = new W();

      if (w instanceof I1)
        System.out.println("W implements I1");

      if (w instanceof I2)
        System.out.println("W implements I2");

      if (w instanceof I3)
        System.out.println("W implements I3");

      if (w instanceof I4)
        System.out.println("W implements I4");

      if (w instanceof X)
        System.out.println("W extends X");
    }
}

And the result will be that W implements every interface and extends X.

OTHER TIPS

Store those interfaces in Collection like Set<Class> classes, then iterate over them and compare with your object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top