-6

Given three classes A, B, and C, where B is a subclass of A, and C is a subclass of B.

(a) (o instanceof B) && (!(o instanceof A))
(b) (o instanceof B) && (!(o instanceof C))
(c) !((o instanceof A) || (o instanceof B))
(d) (o instanceof B)
(e) (o instanceof B) && !((o instanceof A) || (o instanceof C))

Question: Which option is true only when an object denoted by reference o has actually been instantiated from class B?

Note: I am unable to understand the question. Even though the object is instantiated from B, we can instantiate objects from any of the classes A, B or C.

What is the question exactly trying to state?

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
paidedly
  • 1,413
  • 1
  • 13
  • 22
  • 3
    You should ask whoever gave you the assignment. Stack Overflow is not for helping you understand your assignments. We can help you *solve* it if you run into problems, but to understand what is expected of you you should talk to whoever asked the question. – Raniz Jun 12 '15 at 09:10
  • Read : http://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for Please at least search for answers first – Gyan Jun 12 '15 at 09:11
  • It's a question from a java certification book – paidedly Jun 12 '15 at 09:11

2 Answers2

0

Since this is clearly a homework exercise, it is not appropriate for me to provide an answer directly. Instead, I have written a little program for you to demonstrate what the question means, and running it will provide an answer.

You can try this out for yourself:

public class Main {

  class A {}
  class B extends A {}
  class C extends B {}

  public static void main(String[] args) {
    new Main().experiment();
  }

  private void experiment() {
    Object o = new B();

    boolean a = (o instanceof B) && (!(o instanceof A));
    boolean b = (o instanceof B) && (!(o instanceof C));
    boolean c = !((o instanceof A) || (o instanceof B));
    boolean d = (o instanceof B);
    boolean e = (o instanceof B) && !((o instanceof A) || (o instanceof C));

    System.out.println("a = "+a);
    System.out.println("b = "+b);
    System.out.println("c = "+c);
    System.out.println("d = "+d);
    System.out.println("e = "+e);
  }

}
NickJ
  • 9,380
  • 9
  • 51
  • 74
0

If you do not understand instanceof, read this.

The example provided explains what you are asking quite clearly.

  • Is a superclass an instanceof a subclass? No
  • Is a subclass an instanceof a superclass? Yes

From the question, you know the relationships between A, B, C.
Since o 's actual class is B, you should be able to answer those questions with the given information.

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41