2

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.");
        }
    }
}

zaynv
  • 235
  • 2
  • 4
  • 15
  • Yep, it seems correct to me your attemps, do you have something wrong with your code? – nachokk Mar 24 '14 at 22:54
  • Oh no, I'm not saying I'm having any trouble with my following code. I'm just having problems with one of the bullets I have to do: "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." I'm not sure what do to for that. – zaynv Mar 24 '14 at 22:55
  • 1
    `instanceof` can be used with an interface type just as well as for `X`. – ajb Mar 24 '14 at 23:00
  • Copy paste your `instanceof` test and substitute `X` for each `interface`. What's the issue? – Boris the Spider Mar 24 '14 at 23:00
  • Ohh, I had no idea you can use instanceof to check instance types as well. Thanks very much! – zaynv Mar 24 '14 at 23:04
  • you can get an array of implemented interfaces by using `myObject,getClass().getInterfaces()` and then you can check which item of this array is your desired interface – Mahmoud_Mehri Sep 21 '18 at 15:27

2 Answers2

1

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.

sybear
  • 7,837
  • 1
  • 22
  • 38
  • Thanks very much, I just figured it out from another comment! Thanks for taking the time! – zaynv Mar 24 '14 at 23:04
  • FYI posting the full code answers to homework questions is frowned upon. The only reason I'm not downvoting is because the OP says that the answer was already evident _before_ you posted. – Boris the Spider Mar 25 '14 at 00:07
  • @BoristheSpider posting such an answer is not that big of a deal, it will not hurt anyone and I could possibly gain a small amount of rating. In addition, studying carefully is OP's own business. In this question I see some statements but no real problems or problem explanations, so I just posted the whole solution. For me answering to even relatively small / easy question is a challenge as I study myself by searching for info and testing my own code. – sybear Mar 25 '14 at 08:30
-2

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

Mati
  • 2,476
  • 2
  • 17
  • 24
  • 2
    `instanceof` needs a type name, not an object. You can't iterate over type names. – ajb Mar 24 '14 at 22:59
  • I didn't mean to use an `instanceof` for comparing, I should have stated this in my answer. You can use `interface1.isInstance(w)` (interface is Class object from Set). – Mati Mar 24 '14 at 23:14
  • The OP's assignment said he has to use `instanceof`. – ajb Mar 24 '14 at 23:27