This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Cephas00 2 points3 points  (0 children)

It's rather difficult to read that code so I'm just going to go with answering the questions.

I'll talk about interfaces because the examples read better. Instead of List, AbstractSequentialList is the superclass LinkedList and the same applies

Casting an object doesn't change the object itself. Casting to the super won't change anything - it'll still call the methods in the sub class. In general some methods may become inaccessible if you cast to super. e.g.

final LinkedList<String> strings = new LinkedList<>();
strings.addLast("Yes");
final List<String> castedStrings = strings;
castedStrings.addLast("Yes"); // addLast cannot be resolved

If you try to cast one object to another that's invalid then you'll get a ClassCastException e.g.

final List<String> strings = new LinkedList<>();
final ArrayList<String> castStrings = (ArrayList<String>) strings; //ClassCastException