Java superclass variable and subclass copy constructor issue

shadowfox

I know that in Java due to inheritance, a superclass type variable can hold a reference to a subclass object. Example, say ClassA is the superclass to ClassB. You could set an element of a ClassA[] to reference a ClassB object. I'm running into a problem doing this while using a copy constructor of the subclass.

public Items copyFromMasterInventory(String str){
        Items itemToReturn = null;
        int length = masterInventory.length;
        int iterator = 0;
        while (iterator < length){
            String name = masterInventory[iterator].getName();
            if (name.equals(str)){
                if (name.getClass().equals(Collectible.class)){
                    itemToReturn = new Collectible(masterInventory[iterator]); 
                 }
                if (name.getClass().equals(Props.class)){
                    itemToReturn = new Props(masterInventory[iterator]);
                }
            }
        }
        return itemToReturn;
    }

In this case, there are three class. Items, Props, and Collectible. Props and Collectible are subclasses of Items. masterInventory is an array of Items storing both Collectible and Props objects. The purpose of this method is to create and return a copy of an item in the masterInventory array. To avoid .clone(), I've created copy constructors for Collectible and Props. But the way I have it now shows an incompatible types error, that Items cannot be converted to Collectible or Props, even though the object stored in that element is a Collectible or Props object. If been reading and searching for hours but can't seem to come up with a solution. Does anyone know a way around this issue? All help is appreciated.

rsp

You could add an abstract method Item getCopy() to the Item class, implement it in both Props and Collectible, and call it in de while loop:

itemToReturn = masterInventory[iterator].getCopy();

the benefit here is that you do not need the condition on class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java:Method called from subclass in superclass constructor

From Java

Passing superclass object as parameter to subclass constructor (java)

From Java

Calling superclass from a subclass constructor in Java

From Dev

Java polymorphism - How to determine whether superclass vs subclass method will be called and superclass variable vs subclass variable?

From Dev

Superclass overriding Subclass with default values from constructor in Java

From Dev

Subclass Reference by Superclass variable?

From Dev

Passing SuperClass constructor parameters to a SubClass?

From Dev

Java - how do I call copy constructor for subclass of subclass?

From Dev

New to Java: SuperClass and SubClass

From Dev

Calling constructor of subclass from constructor of superclass

From Dev

Inheritance :hidden variable of superclass in subclass

From Java

Variable with same name in subclass and superclass

From Dev

Superclass method obtain subclass variable

From Dev

How can a constructor from the subclass access a private field variable from the superclass?

From Dev

In Moose, how to specify constructor arguments for the superclass in a subclass?

From Dev

Constructor method of subclass for a superclass type object?

From Java

Using generics and/or objects of Constructor to return superclass OR subclass

From Java

Initialize superclass variables (needed in constructor) in a subclass

From Dev

Passing an ArrayList of subclass to a constructor that takes ArrayList of superclass?

From Dev

Java superclass catch and subclass catch

From Java

java, initialize SubClass from SuperClass

From Dev

Java subclass initialize with arguments of superclass

From Dev

Subclass Initialization Issue with Data not from Superclass

From Java

Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called?

From Java

does "this" keyword on subclass constructor requires superclass default constructor to be implicitly defined?

From Dev

Dart - How would I call the factory constructor of the superclass in the constructor of the subclass?

From Java

Why is an instance variable of the superclass not overridden by a subclass?

From Dev

How to modify superclass variable from subclass?

From Dev

Alter superclass' variable from subclass on Python

Related Related

  1. 1

    Java:Method called from subclass in superclass constructor

  2. 2

    Passing superclass object as parameter to subclass constructor (java)

  3. 3

    Calling superclass from a subclass constructor in Java

  4. 4

    Java polymorphism - How to determine whether superclass vs subclass method will be called and superclass variable vs subclass variable?

  5. 5

    Superclass overriding Subclass with default values from constructor in Java

  6. 6

    Subclass Reference by Superclass variable?

  7. 7

    Passing SuperClass constructor parameters to a SubClass?

  8. 8

    Java - how do I call copy constructor for subclass of subclass?

  9. 9

    New to Java: SuperClass and SubClass

  10. 10

    Calling constructor of subclass from constructor of superclass

  11. 11

    Inheritance :hidden variable of superclass in subclass

  12. 12

    Variable with same name in subclass and superclass

  13. 13

    Superclass method obtain subclass variable

  14. 14

    How can a constructor from the subclass access a private field variable from the superclass?

  15. 15

    In Moose, how to specify constructor arguments for the superclass in a subclass?

  16. 16

    Constructor method of subclass for a superclass type object?

  17. 17

    Using generics and/or objects of Constructor to return superclass OR subclass

  18. 18

    Initialize superclass variables (needed in constructor) in a subclass

  19. 19

    Passing an ArrayList of subclass to a constructor that takes ArrayList of superclass?

  20. 20

    Java superclass catch and subclass catch

  21. 21

    java, initialize SubClass from SuperClass

  22. 22

    Java subclass initialize with arguments of superclass

  23. 23

    Subclass Initialization Issue with Data not from Superclass

  24. 24

    Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called?

  25. 25

    does "this" keyword on subclass constructor requires superclass default constructor to be implicitly defined?

  26. 26

    Dart - How would I call the factory constructor of the superclass in the constructor of the subclass?

  27. 27

    Why is an instance variable of the superclass not overridden by a subclass?

  28. 28

    How to modify superclass variable from subclass?

  29. 29

    Alter superclass' variable from subclass on Python

HotTag

Archive