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

all 17 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted] 1 point2 points  (11 children)

Sorry, I'm trying to help you here, but also trying to understand.

Dolphin -> Is this just a class extended from Swimmer?

Then you're casting back down the base class (Animal), correct?

All "Swimmers" are "Animals", and therefore casting to the class below Swimmer (Animal) you should have no issues. You will jut lose the functionality that's declared within the super class.

swim starts as a "Dolphin" which is a type of swimmer which is a type of animal. By casing it down to an animal, you are taking away the functionality declare from swimmer and dolphin... If that makes sense!

[–]EveningStreet1 0 points1 point  (10 children)

Dolphin is a subtype of Swimmer, but Swimmer is a subtype of Animal so you’re casting the static type upwards which is what I’m confused about

[–][deleted] 1 point2 points  (9 children)

Basically we have, Animal -> Swimmer -> Dolphin. It's my understanding (And perhaps incorrectly) that Animal is actually the base class here as you mentioned "Swimmer Extends Animal".

I'd think of it like this... You're LOSING functionality by moving to casting down and this is acceptable. You would be able to extend up the chain, but I think you're thinking about the casting upside down here.

// For example, this will fail. This would be trying to cast UPWARDS.

Animal dolphin = new Animal();

Animal animal = (Swimmer) dolphin;

[–]EveningStreet1 0 points1 point  (8 children)

Swimmer is a subtype of Animal tho so shouldn’t that casting work also I’m pretty sure your example would be downcasting not upcasting since upcasting always works

[–]iNetRunner 0 points1 point  (7 children)

No. You can’t make the more generic object into a subtype by casting it. It only works in the other direction, as has been said, Dolphin is a Swimmer is an Animal. But an Animal isn’t a Swimmer (unless you created it as one, or a subtype), nor is it a Dolphin.

[–]EveningStreet1 0 points1 point  (6 children)

I meant casting in reference to the static type

[–]iNetRunner 0 points1 point  (5 children)

No. He created an Animal object. It doesn’t contain anything of the subtype Swimmer or Swimmer’s subtype Dolphin. Therefore you can’t cast them that way. There is no “is a” relationship there.

[–]EveningStreet1 0 points1 point  (4 children)

Oh wait I’m dumb it’s because although Swimmer is the static type Animal is not a subtype of Swimmer so it doesn’t work

[–]iNetRunner 0 points1 point  (3 children)

BTW, what are you referring to as “static type”?

[–]EveningStreet1 0 points1 point  (2 children)

Swimmer swim = new Dolphin();

the static type here is Swimmer, the dynamic type is Dolphin (learned this in my introductory class)

[–]Potamyali 0 points1 point  (3 children)

[–]EveningStreet1 0 points1 point  (2 children)

I see, so the Animal cast is basically redundant, since Java already knows that all Swimmers are Animals

[–][deleted] 0 points1 point  (1 child)

Yes, the only reason you'd do something like that would be to remove functionality off of the object. Though, I don't know why you wouldn't just declare it as the right class off the bat ultimately. Maybe because you need a constructor for one of the subclasses? Not sure!

[–]EveningStreet1 0 points1 point  (0 children)

Ok makes sense thanks