Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Method Overloading

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      Method Overloading

      Java Method Overloading

      When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.

      Advantage of Method Overloading

      Method overloading improves the code readability and reduces code redundancy. Method overloading also helps to achieve compile-time polymorphism.

      Example of Method Overloading

      If you observe the following example, Here we have created a class named Tester this class has two methods with same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).
      class Calculator{
      public static int add(int a, int b){
      return a + b;
      }
      public static int add(int a, int b, int c){
      return a + b + c;
      }
      }
      When you invoke the add() method based on the parameters you pass respective method body gets executed.
      int result = Calculator.add(1,2); // returns 3;
      result = Calculator.add(1,2,3); // returns 6;

      Different Ways of Java Method Overloading

      Method overloading can be achieved using following ways while having same name methods in a class.
      • Use different number of arguments
      • Use different type of arguments

      Invalid Ways of Java Method Overloading

      Method overloading cannot be achieved using following ways while having same name methods in a class. Compiler will complain of duplicate method presence.
      • Using different return type
      • Using static and non-static methods

      Method Overloading: Different Number of Arguments

      You can implement method overloading based on the different number of arguments.

      Example: Different Number of Arguments (Static Methods)

      In this example, we've created a Calculator class having two static methods with same name but different arguments to add two and three int values respectively. In main() method, we're calling these methods and printing the result. Based on the type of arguments passed, compiler decides the method to be called and result is printed accordingly.
      package com.tutorialspoint;
      
      class Calculator{
      public static int add(int a, int b){
      return a + b;
      }
      public static int add(int a, int b, int c){
      return a + b + c;
      }
      }
      
      public class Tester {
      public static void main(String args[]){
      System.out.println(Calculator.add(20, 40));
      System.out.println(Calculator.add(40, 50, 60));
      }
      }

      Output

      60
      150

      Example: Different Number of Arguments (Non Static Methods)

      In this example, we've created a Calculator class having two non-static methods with same name but different arguments to add two and three int values respectively. In main() method, we're calling these methods using object of Calculator class and printing the result. Based on the number of arguments passed, compiler decides the method to be called and result is printed accordingly.
      package com.tutorialspoint;
      
      class Calculator{
      public int add(int a, int b){
      return a + b;
      }
      public int add(int a, int b, int c){
      return a + b + c;
      }
      }
      
      public class Tester {
      public static void main(String args[]){
      Calculator calculator = new Calculator();
      System.out.println(calculator.add(20, 40));
      System.out.println(calculator.add(40, 50, 60));
      }
      }

      Output

      60
      150

      Method Overloading: Different Type of Arguments

      You can implement method overloading based on the different type of arguments.

      Example: Different Type of Arguments

      In this example, we've created a Calculator class having two non-static methods with same name but different types of arguments to add two int values and two double values respectively. In main() method, we're calling these methods using object of Calculator class and printing the result. Based on the type of arguments passed, compiler decides the method to be called and result is printed accordingly.
      package com.tutorialspoint;
      
      class Calculator{
      public int add(int a, int b){
      return a + b;
      }
      public double add(double a, double b){
      return a + b;
      }
      }
      
      public class Tester {
      public static void main(String args[]){
      Calculator calculator = new Calculator();
      System.out.println(calculator.add(20, 40));
      System.out.println(calculator.add(20.0, 40.0));
      }
      }

      Output

      60
      60.0