1

I created a class called "Object" and two classes "Item" and "Scenery" that implement the class Object ( every item is an object and every scenery is an object)

During my implementation I created an Array of type Object (let's call it ObjectArray)- (that can store both items and sceneries).

In another part of the code I need to catch a specific element from ObjectArray, and I need to check if it's an Item.

I found a solution but it's too long ( add every item I add ObjectArray to another array of only items and check if the element I want to catch exists in both arrays)

But I guess their might be another better way!?

M--
  • 25,431
  • 8
  • 61
  • 93
  • 2
    Please share the code of your tries, that would easier to understand. And don't call a class `Object` , that one already exists and is the superclass of all ones – azro Aug 31 '20 at 17:28
  • 2
    Does [`instanceof`](https://en.wikibooks.org/wiki/Java_Programming/Keywords/instanceof) help? – Kulfy Aug 31 '20 at 17:39
  • @azro it's a lot of code behind this explanation. – Wassim Jaoui Aug 31 '20 at 17:46
  • 1
    @Kulfy your comment answers my qst. thanks ! – Wassim Jaoui Aug 31 '20 at 17:47
  • 4
    Does this answer your question? [How to determine an object's class?](https://stackoverflow.com/questions/541749/how-to-determine-an-objects-class) – Kulfy Aug 31 '20 at 17:49
  • 1
    Your job to share a minimal example that is enough to understand the real problem, don't share all code – azro Aug 31 '20 at 17:49
  • One suggestion: If you find yourself needing to determine if the elements in an array are one of two types of objects, you need look at your design. – NomadMaker Aug 31 '20 at 18:56

1 Answers1

2

I created a class called "Object" and two classes "Item" and "Scenery" that implement the class Object ( every item is an object and every scenery is an object )

In Java, every object is an instance of Object, so you don't need explicitly to make Item and Scenery subtypes. Also, Object it's a class so we use to say implement with abstract types like Interfaces and extends for concrete elements.

To determinate what type is the object, you can use instanceof

Object[] myObj = {new Item(),new Item(), new Scenary()};

for (Object s : myObj)
     if (s instanceof Item)
        System.out.println(s.getClass() + " It's an item!");

Pay attention that instanceof return true also with subtypes. So let's assume in future you will extend the Item class with another class let's call it SubItem. If the array of objects contains some SubItem instances, the instanceof call on SubItem will return true. So if you want to determinate precisely if it's the right class object you need a more restricted check just like s.getClass() == Item.class.

Object[] myObj = {new Item(), new Scenary(), new SubItem(),new SubItem()};

for (Object s : myObj)
     if (s.getClass() == Item.class)
        System.out.println(s.getClass() + " It's an Item!"); 

In this latest scenario, this check will return true only if s is effectively a Item and not a SubItem. As the latest point don't use Object, always use abstract class or interfaces to aggregate more types. If you will continue to study you will understand why. So what I'm saying is to create an interface for example Shop and let both Item Scenery and eventually SubItem implement it. Take notes that with this method both the 3 types are subtypes of the type Shop so a instanceof for each Item Scenery and SubItem compared with Shop will return true. Summering: instanceof will be useful for runtime checks when you have an abstract type and you can't determinate statically the real nature of an object. (Imagine to assign an instantiated Item to it's Shop for example Shop obj = new Item();. You will notice that this assignment is legit.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Virgula
  • 318
  • 4
  • 12