java help

Arachno

Veteran XV
norty's stuff was simple. This gets a bit more involved.

Ok, so i have an arraylist that contains references to two different types of object. Both of these objects extend from the same superclass.
My question is, if i use casting while traversing the arraylist, can i cast each reference as the superclass or do i have to find a way to identify which object each reference is and cast in that specific object to find the value of an instance variable that's inherited from the superclass anyway?

PS. I'm aware that casting is pretty pointless since java 5, but i need do it this way for this work. Don't ask why.
 
[You have a relationship where X and Y both derive from S. Your ArrayList contains items of type X and/or items of type Y.]

In this case, you can safely cast each ArrayList item to S. Whether or not it would make sense to do this instead of casting to the more specific type depends on whether or not S functionality is sufficient here.
 
Last edited:
somespareid said:
[You have a relationship where X and Y both derive from S. Your ArrayList contains items of type X and/or items of type Y.]

In this case, you can safely cast each ArrayList item to S. Whether or not it would make sense to do this instead of casting to the more specific type depends on whether or not S functionality is sufficient here.

Thanks.
Yeah, all the classes are encapsulated and all the return methods are inherited from S.

So something like:
Code:
            //PRE arrayList exists with references to X and Y
            //PRE int newNum is read from user input

            int end = arrayList.size();
            int pos = 0;
                
            while(pos < end){
                Object o = arrayList.get(pos); 
                S s = (S) o; 
                if (s.getNum() == newNum){
                    System.out.println("Number already exists. Please try again.");
                    
                }
                pos++;                
            }

Will work?
 
Last edited:
I believe that if you cast the instances of a subclass to their superclass, you can only utilize the methods defined in the superclass. If the subclasses only override methods in the superclass, then you should be fine.

However, since you've only got two different kinds of objects in your arraylist, you might be better off using the instanceof operator and casting them back into their original class.

Code:
//PRE arrayList exists with references to X and Y
            //PRE int newNum is read from user input

            int end = arrayList.size();
            int pos = 0;
                
            while(pos < end){
                Object o = arrayList.get(pos);
 
                if( o instanceof A )
                   o = (A) o;
                else if( o instanceof B )
                   o = (B) o; 

                if (o.getNum() == newNum){
                    System.out.println("Number already exists. Please try again.");
                    
                }
                pos++;                
            }

Might be excessive, but it's another way to handle things
 
Razer said:
I believe that if you cast the instances of a subclass to their superclass, you can only utilize the methods defined in the superclass. If the subclasses only override methods in the superclass, then you should be fine.

Yeah, all my instance variables and methods are declared in the superclass and nothing is overridden in the sub classes. It's just the sub classes that i'm making multiple instances of and setting the instance variables in.

Your solution looks like a much better way of doing things though. I wasn't aware of that particular operator, so thanks.
 
Arachno said:
So something like:

[…]

Will work?
Assuming that S has an accessible method called getNum, that will work; and from the looks of it S functionality is sufficient here, suggesting that casting to S is sufficient here.
 
Razer said:
Code:
//PRE arrayList exists with references to X and Y
            //PRE int newNum is read from user input

            int end = arrayList.size();
            int pos = 0;
                
            while(pos < end){
                Object o = arrayList.get(pos);
 
                if( o instanceof A )
                   o = (A) o;
                else if( o instanceof B )
                   o = (B) o; 

                if (o.getNum() == newNum){
                    System.out.println("Number already exists. Please try again.");
                    
                }
                pos++;                
            }
That won't work, because Object doesn't have a getNum method. Nor would it achieve anything not already being achieved if it did work.
 
Back
Top