class

Java Overloading Methods

With this example we are going to demonstrate how to use Overloading Java Methods in a class. Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. We have created a class, that has constructors and methods with the same names but with different arguments and use them in new class instances to see their behavour. Specificaly:

  • Tree class has an int field, treeHeight.
  • It has a constructor where it sets its int field to zero.
  • It also has a constructor where it sets its int field to a given int value.
  • It has a method void info() where it prints a message with the field.
  • It also has a method void info(String str) where it prints a message with a given String and the int field.
  • We create two new Tree objects and for each one of them call info() and info(String str) methods.
  • The objects are created using the constructor with the int argument.
  • Then a new instance of Tree is created using the constructor without fields.

Let’s take a look at the code snippet that follows:  

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.javacodegeeks.snippets.core;
 
 
class Tree {
 
    int treeHeight;
 
    Tree() {
 
  System.out.println("Plant a seedling");
 
  treeHeight = 0;
    }
 
    //Overloaded Constructor
    //Notice that the two constructors have the same name , but different arguments
    Tree(int num) {
 
  System.out.println("Creating new Tree that is " + num + " meters tall");
 
  treeHeight = num;
    }
 
    void info() {
 
  System.out.println("Tree is " + treeHeight + " meters tall");
    }
 
    void info(String str) {
 
  System.out.println(str + ": Tree is " + treeHeight + " meters tall");
    }
}
 
public class  MethodOverloading{
 
    public static void main(String[] args) {
 
   
 
  for (int i = 0; i < 2; i++) {
 
 
Tree t = new Tree(i);
 
 
t.info();
 
 
t.info("overloaded method");
 
  }
 
  // Overloaded constructor:
 
  new Tree();
 
    }
}
Output
Creating new Tree that is 0 meters tall
Tree is 0 meters tall
overloaded method: Tree is 0 meters tall
Creating new Tree that is 1 meters tall
Tree is 1 meters tall
overloaded method: Tree is 1 meters tall
Plant a seedling

 This was an example of how to use overloaded methods in a class in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button