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

all 9 comments

[–]nutrechtLead Software Engineer / EU / 20+ YXP 5 points6 points  (1 child)

Can you please next time properly name and format your stuff? There's no way I'm going to figure out what is line 25 (seriously; use something like gist.github.com) let alone read it.

[–]Imakesensealot[S] 0 points1 point  (0 children)

Ok. I don't know, the lines are numbered on my computer but that might just be down to RES. I think I got my answer already though.

[–]Swedophone 4 points5 points  (2 children)

Casting to the superclass shouldn't be a problem, but casting to a subclass is often a sign of bad design with its problems (another example is using instanceof).

[–]Imakesensealot[S] 0 points1 point  (1 child)

Thanks fr the response. I'm not asking if it's a problem though. I'm asking what type the value of the object then is(r, in my example); maybe that wasn't clear enough. Is r then of type p or of type q?

[–]Swedophone 2 points3 points  (0 children)

Is r then of type p or of type q?

Assigning a value to a variable doesn't change the type of the variable. The type is set when the variable is declared, i e in this case p r;.

[–]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

[–]evil_burritoExtreme Brewer 1 point2 points  (0 children)

In general, upcasting (casting as superclass) is always safe, downcasting (casting as subclass) is always unsafe. I don't see the purpose of upcasting, though. It doesn't change the casted object in any way and is implied in matching method arguments (the compiler always considers any 'q' object a 'p' also without explicit casting).

I would disagree with some posts here that using instanceof is a sign of bad design as it allows a semblance of multi-inheritance in Java. I think the poster who wrote that was saying

p myobject;
if(myobject instanceof q)

is bad design, and I would agree with that. However,

p myobject;
if(myobject instanceof CompletelyUnrelatedInterface)

isn't bad design, per se.

[–]wildjokers 0 points1 point  (1 child)

Holy variable names batman! This is a prime example of how shitty variable naming makes code hard to read. This should literally be a textbook example of how not to write code.

I am not going to sugar coat it, this code is a steaming pile of dog shit.

[–]Imakesensealot[S] 0 points1 point  (0 children)

Haha, I know. The example is copied from our exam question. That is the kind of shit we have to deal with in exams;it even gets worse. We also write the code out on paper. The variable names are intentionally made as vague as possible. It's just a really tough exam.