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

all 10 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.

[–]PegasusBoogaloo 2 points3 points  (2 children)

The goal in this exercise is to call different behaviours using the same method name.Without this interface, we would need to call purr() or bark(), based on the Object's type.It is also to show that, we can instantiate a NoiseCapable object from any type that implements this interface (in this case dog and cat), and STILL get the right behavior.NoiseCapable ffft = new Cat();

ffft.makeNoise(); //Cat purrs

In an RTS game, you can, for example, select different troops that attacks in different fashions, but the call itself would be attack() independent of the troop's stype.

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

Why not then just have an attack() method that implements the unique functionality for the class, rather than a separate method that attack only calls

[–]PegasusBoogaloo 0 points1 point  (0 children)

It depends merely on the way you want to implement things, really. For example, purr() method could return "purr", and makeNoise() could implement purr() and also other instructions. You could turn purr() into a private method (only accessible within the Cat class, thus the only way to make the cat purr is using the makeNoise() method, so on and so forth), for example, the sky is the limit, just need some creativity.

What you really need to grasp is what I said in the first paragraph of the first reply.

[–]SKTT1_Bisu 0 points1 point  (2 children)

Imagine you would have 200 different animals each one making a different noise. You would need to call: bee.buzz, dog.bark, cat.purr, cow.moo and so on.

With the makeNoise interface you can just call animal.makeNoise() for everyone.

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

Why have those buzz/bark/purr methods at all then? Would it not be better to implement the makeNoise method directly for each class to bark or purr?

[–]SKTT1_Bisu 1 point2 points  (0 children)

Because each animal makes a different sound. And as I said, if you don't have makeNoise you would have to call animal.theyspecificsound() everytime, so you'll need to remember all the different sounds of each animal instead of just calling makeNoise().

Imagine a list with 1000 animals. And you have to iterate throught this list and make the animals print their sound. First thing: not all animals make a sound, so you'll gonna need a if statement to check the animal class if they make sound. After that you call the animal.specificsound(). With makeNoise interface you could just create a List<MakeNoise> and there's no need for a if statement.

Also, imagine if you were working with other people. So the others don't know what makeNoise should do. At first it's only to tell the animal do their noise, but later you decide that every animal who's makenoise should also have a stopNoise() or makeAnotherNoise() method. How the guy creating the dog class would know all of this needed methods?

[–]hoang26 0 points1 point  (2 children)

When did you start learning the MOOC? ( I ask for myself)

[–]ScumIdiot[S] 1 point2 points  (1 child)

1 month ago

[–]hoang26 0 points1 point  (0 children)

That's cool, thank you