What is meant by method description

Override or overload Java methods. What is the difference? How are you?

Keylearnings:

  • What is the difference between overriding and overloading a method?
  • How to Override Methods.
  • How to overload methods.
  • How to chain methods.

There were three Martins in my school class.

That was a problem in physical education. Whenever someone shouted: "Martin catch the ball!", You never knew who was meant.

We have the same problem with Java methods.

Because in Java we can define several methods with the same name.

But how do we know which method is meant when a call is made?

This is exactly the problem we want to deal with in this article.

The difference between overwriting and overloading

We distinguish between two cases for this problem.

For one thing, we can have several methods with the same name within the same class. In this case we are talking about overloading a method.

On the other hand, there can be methods with the same name in an inheritance hierarchy, i.e. a method in a subclass has the same name as the method in a superclass. In this case we are talking about overriding a method.

First, let's look at overloading a method.

Overloading a method

A method is defined by three properties:

  1. Method name
  2. Parameter list
  3. Return value

If we overload a method it means that we have at least two methods with the same name within a class.

So we only have the parameter list and the return value to differentiate.

And to make it short! A differentiation based on the return value does not work.

The reason for this is that we are not forced to process the return value in JAVA.

Let us consider the following two methods,

private int work hard () {// DO SOMETHING} private double work hard () {// DO SOMETHING}

which only differ in the return type.

Let's try the method using (i.e. without processing the return type)

work hard ();

call, the compiler cannot determine whether the method should be called with the return value or and we get a compiler error.

The only thing left is the distinction via the parameter list.

Parameter lists with methods of the same name can differ on the one hand in the data types and on the other hand in the number of parameters.

Let's make this clear with a few examples and consider the following method definitions:

1: public static void work hard (int x, double y, double z) {2: System.out.println ("work hard 1"); 3:} 4: public static void work hard (int x, double y) {5: System.out.println ("work hard 2"); 6:} 7: public static void work hard (double x, int y) {8: System.out.println ("work hard 3"); 9:}

Do you fancy a quiz?

Which method does the following call execute?

WorkHard (6,7.0,6.7);

It's still easy!

We have three arguments and only one method that matches this parameter list and that is the method from lines one through three. And in fact we get the program output:

work hard 1

Let's look at another call:

workhard (3.1,5);

This time we have two arguments; both the parameter list of the method in lines four to six and the method in lines seven to nine match.

So to make a final decision we need to look at the data types of the arguments.

The first argument is of the data type and the second argument is an integer value.

So only the method from lines seven to nine comes into question. And in fact the program output is:

work hard 3

Do you have any idea what the method call has to be so that the method from lines four through six is ​​executed?

Yep, we just have to swap the arguments.

WorkHard (5,3.1);

Now the first argument is of the data type integer and the second argument is a value that exactly matches the parameter list of the desired method. As expected, the program output is:

work hard 2

So now another trick question! What is the result of the following method call:

WorkHard (5.0,5.0);

Here both parameters are of the data type.

Do you get it?

Unfortunately we do not have a method in which the parameter list consists of two variables, so the compiler cannot determine which of the methods is meant and throws an error.

In this example we can see that there is no automatic typecasting. Although the value could be converted to an integer without loss, it is still processed as a value.

To make the program executable for this call too, we need to add another version of the method that can process two values.

public static void work hard (double x, double y) {System.out.println ("work hard 4"); }

With this our program can be executed and provides the output:

work hard 4

Fine! Next, let's look at method overriding.

Overriding methods

When overloading a method, we consider methods with the same name within a single class.

But what do we do with methods of the same name that are distributed to different classes within an inheritance hierarchy?

Method overwriting solves this problem.

Let's start with an example and write a class.

1: public class four-legged friends {2: protected string name = null; 3: four-legged friends (String name) {4: this.name = name; 5:} 6: public void myName () {7: System.out.println ("My name is:" + this.name); 8:} 9:}

This class has an attribute that we can initialize using the constructor and output on the screen using the method.

It makes sense to derive a dog from this class.

1: public class dog extends four-legged friend {2: dog (String name) {3: super (name); 4:} 5:}

The class only has one constructor. Since we can only derive from a constructed superclass, we have to call the constructor of in line three using the keyword.

Okay, with this we can now create a dog object called Bello.

Dog dog = new dog ("Bello");

What happens if we try to call the method?

dog.myName ();

Because there is an instance of the class, the compiler first looks for a method with the name and an empty parameter list in this class.

Since such a method does not exist in the class, the search is continued in the superclass, in which the compiler finally finds what it is looking for and executes the method.

The output of our program is:

My name is: Bello

What do you think will happen if we also add the method to the class?

public class dog extends Vierbeiner {public void myName () {System.out.println ("I am a dog and my name is" + name + "!"); } Dog (string name) {super (name); }}

What program output do we have to expect when we call the method?

dog.myName ();

Since there is an instance of the class, the compiler first looks for a method with the name and an empty parameter list in the class.

This time he finds it and executes the method from the class.

The program output is:

I am a dog and my name is Bello!

And this is exactly the phenomenon we call overwriting methods.

As with overloading, the parameter list is the decisive criterion for the clear assignment of a call to a method when overwriting methods.

Wait a moment! Does the method still have a function in the four-legged class?

Concatenate methods

In our example, we have completely replaced the method from the class for instances of a dog with the method of the same name from the class.

Often you just want to refine a method from a superclass.

And that is also possible! We call this process concatenation.

It's best to look at this again using an example:

We add a eat method to the class.

public void fressen () {System.out.println ("I'm hungry!"); }

Really nothing special! The method only outputs an output on the screen.

Of course, our dog also gets hungry on a regular basis, which he announces just like every other four-legged friend. However, unlike e.g. an elephant, our Bello also requires a feeding bowl, so we have to expand the method accordingly.

The most obvious way to do this is to copy the method from the class, paste it into the class and add a corresponding screen output.

public void fressen () {System.out.println ("I'm hungry!"); System.out.println ("Please bring me a bowl!"); }

As a result, we have overwritten the method from the class with a new, extended method.

However, this approach has one major disadvantage.

We generate redundant program code, which we absolutely want to avoid in object-oriented programming.

Our goal is to be able to reuse program code that has been written once. In our example this may seem a little petty, but that's because we're looking at a toy problem here.

In reality, redundant program code leads to software that is difficult to maintain, since changes have to be made in all places where the copied code is located.

It would be better if we had the output “I'm hungry!” Generated by the method from the upper class.

And here the Java keyword helps us.

But first one warning! To call the method from the superclass one could come up with the following idea.

1: public void fressen () {2: fressen (); 3: System.out.println ("Please bring me a bowl!"); 4:}

However, this is completely wrong!

In line two, the method from the superclass is not called, instead there are an infinite number of recursive calls in the class, which leads to a program crash.

Oh, if you don't know the term recursive yet. Recursion means that a function calls itself.

The keyword super

With the help of we can access methods of the superclass.

Let's change the method in the class as follows:

public void fressen () {super.fressen (); System.out.println ("Please bring me a bowl!"); }

By prefixing the keyword (line 2), we explain to the compiler that the eat method from the superclass is meant.

The program output is as desired:

I'm hungry! Please bring me a bowl!

The first output is generated by the method of the class and the second by the method of the same name in the class.

What is the added value of overloading and overwriting

The possibility of overloading and overwriting methods offers us an easy way to extend classes.

With the help of overloaded methods, it is easy for us to provide the same functionality for different data, because the parameter list is decisive for the assignment of overloaded methods.

Changing parameter lists in particular repeatedly leads to serious errors in projects, since method calls no longer match the actual definitions of the methods.

Overloading methods saves us from having to modify the parameter list of existing methods.

Existing methods can remain untouched, so that existing method calls continue to work. We can implement extensions in completely new methods with the same name and adapted parameter list.

By overwriting methods we have, on the one hand, the option of replacing a functionality that a superclass provides and, on the other hand, expanding it with the help of the keyword.

I look forward to your questions in the comment area!

Did you like the article? Then follow us on Facebook right away!