1

I'm using the scanner to take user input and assign it to an object. I know string is an extension of object class so it should be a simple assignment. Here is what I've tried.

string name = s.nextLine();// or s.next();
object obj = name;// or JOSN.parse(name);

Here is my code. I've put one arraylist instead another arrraylist.

        trans = new List<Transition>(nos);

        for (i = 0; i < 5; i++) {
            List<Transition>innerList = trans.get(i);
            for (j = 0; j < 5; j++){
                String state = s.next();
                Transition reg = innerList.get(j).add(state);
         }}

The error i get is 'type mismatch cant convert type string to Transition', but Transition is just an object.

Will
  • 89
  • 1
  • 9

2 Answers2

1
         OBJECT
       /        \
  Transition   String

This is how the hierarchy looks like.

Transition is a subclass of Object, just like how String is a subclass of Object.

If two classes are in the same hierarchy chain, then an instance of the subclass can be safely cast to an instance of the superclass. In this case, Transition and String are not in the same chain.

refer here for more information. I asked question for you only basically and got downvote. :)

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

When ever we are doing casting it must have IS-A Relation. Means, String IS-A Object and Transition IS-A Object.

But In this when we try to make a relationship on String and Transition we See, Transition NOT(IS-A) String.

This will give to you casting problem, we must remember whenever we are trying any Casting it must be in hierarchy, Having IS-A relation with the other.

RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24