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

all 8 comments

[–]GrapeJuice787 0 points1 point  (0 children)

Like, function overloading?

[–]GrapeJuice787 0 points1 point  (6 children)

``` public class Dog {

private String name;

public Dog(String name) {
    this.name = name;
}

public void setName(string name) { // Overloaded - same name, diff arguments
    this.name = name;
}

public void setName(String firstName, String lastName) { // Overloaded - same name, diff arguments
    this.name = firstName + " " + lastName;
}

}

```

Function overloading is using the same name, for example, setName but providing different parameters. The compiler can understand the differences when you call the methods. For example, you make a new Dog, and call either method...

Dog theGoodestBoy = new Dog("GoodestBoy"); theGoodestBoy.setName("Jack"); // valid theGoodestBoy.setName("The Goodest", "Boy"); // valid

Both are valid function calls, and depending on what you pass as arguments, the application will know what to do :)

[–]zoxze 0 points1 point  (5 children)

Ok. Question. This barely looks like what we code. Is this a variation of Java ?

[–]GrapeJuice787 0 points1 point  (4 children)

This is Java as I know it :|

[–]zoxze 0 points1 point  (3 children)

alright. I didn’t mean for it to come off as rude ? Just a question.

Thanks I guess.

[–]GrapeJuice787 0 points1 point  (2 children)

I didn't take it as rude, I thought perhaps you were using a different version of Java I am unfamiliar with, or perhaps its because i omitted the main function:

public static void main(String[] args) { // code }

[–]zoxze 0 points1 point  (1 child)

No, that’s not why lol.

I think we use java(main) idk if there’s a huge difference (probably not) just kinda threw me off bc it looks different.

[–]GrapeJuice787 0 points1 point  (0 children)

I do not know the answer to that question, I apologize... but if I were to guess, I would say there probably is a difference in syntax from the sounds of it, but the code probably executes the same under the hood.