Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Method Overloading in Java

    By using method overloading, several methods can have the same name but different signatures, and the signature can change depending on the number, type, or combination of input arguments. Method overloading is also known as early binding, static polymorphism, and compile-time polymorphism in the Java programming language. The child argument will always take precedence over the parent argument when a method overloads.

Table Of Content

  • Method Overloading in Java
  • Method Overloading by Changing the Number of Parameters.
  • Method Overloading by Changing Data Types of the Arguments.
  • Method Overloading by Changing the Order of the Parameters of Methods






Method overloading example
 // Method overloading example  
class Product {
    int Multiply(int p, int q){  
     return p * q;
  }

  float Multiply(float p, float q,float r){   
	  return p * q * r;
  }
}
class Overloading {
 public static void main(String[] args){
    Product product=new Product();
     System.out.println(product.Multiply(2, 4));
     System.out.println(product.Multiply(5.2f, 6.3f,2.3f));
 }
}




Output:

8 
75.34799 

Different Approaches for Method Overloading in Java

  • By Changing the Number of Parameters.
  • By Changing Data Types of the Arguments.
  • By Changing the Order of the Parameters of Methods

1.Method Overloading by Changing the Number of Parameters.


Modifying the number of parameters while passing to various methods allows for method overloading.


Method Overloading by Changing the Number of Parameters.
 // Method Overloading by Changing the Number of Parameters.
class Product {
    public void adding(int a, int b){
        int add = a+b;
       System.out.println(add);
    }
    public void adding(int a, int b, int c){
        int add = a+b +c;
        System.out.println(add);
    }
}
class DockerTpoint {
    public static void main(String[] args)
    {
     Product product = new Product();
     product.adding(10,20);
     product.adding(10, 20, 30);
    }
}



Output:

8 
32.76 

2.Method Overloading by Changing Data Types of the Arguments.


When two methods share the same name but have different Data Types of parameter , this is referred to as method overloading.


Method Overloading by Changing Data Types of the Arguments
 // Method Overloading by Changing Data Types of the Arguments
class Product {
    public void adding(int a, int b){
        int add = a+b;
       System.out.println(add);
    }
    public void adding(float a, float b, float c){
    	float add = a+b+c;
        System.out.println(add);
    }
}
class DockerTpoint {
    public static void main(String[] args){
     Product product = new Product();
     product.adding(10,20);
     product.adding(10.2f, 20.1f, 30.3f);
    }
}



Output:

30 
60.6 



3.Method Overloading by Changing the Order of the Parameters of Methods


Rearranging the parameters of two or more overloading methods is another way to implement overloading of methods.

For example, if method 1's parameters are (String name, int ID) and method 2's parameters are (int ID, String name), but both methods have the same name, then these 2 methods are deemed to be overloaded with various parameter combinations.


Method Overloading by Changing the Order of the Parameters of Methods
 // Method Overloading by Changing the Order of the Parameters of Methods
class Product {
    public void adding(int ID, String name){
    	System.out.println("Id : "+ID+" Name : "+name);
    }
    public void adding(String name,int ID){
       System.out.println("Id : "+ID+" Name : "+name);
    }
}
class DockerTpoint {
    public static void main(String[] args){
     Product product = new Product();
     product.adding(113,"Alok");
     product.adding("Alok",123);
    }
}



Output:

Id : 113 Name :Alok 
Id : 123 Name :Alok 

Note : The better error is the compile time error, not the run time error. Therefore, if you declare the same method with the same parameters in Java, a compiler error is produced.


Method Overloading and Type Promotion


If the exact prototype does not match the arguments, the compiler will use type conversion but to a higher type within the same family. Type conversion to the following higher family (If a long data type is not available for an int data type, let's say it will look for a float data type instead).

Small Data type get promoted to high or big data type

Small Data type get promoted to high or big data type


Byte can be promoted to short, int, long, float, or double as shown in the diagram above. Promotion from the short datatype to int, long, float, or double is possible. Char can be elevated to int, long, float, double, and other datatypes.





Example of Method Overloading and Type Promotion

 // Method Overloading and Type Promotion 
class Product {
    public void adding(int ID, long fee) // here int get promoted to long
    {
      System.out.println("Id : "+ID+" fee : "+fee);
    }
    public void adding(int fee,int ID){
       System.out.println("Id : "+ID+" fee : "+fee);
    }
}
class DockerTpoint {
    public static void main(String[] args){
     Product product = new Product();
     product.adding(113,1400);
     product.adding(1400,123);
    }
}



Output:

Id : 1400 fee : 113
Id : 123 fee : 1400 

Example of Type Promotion if matching found


Type promotion is not done if there are type-matching arguments in the method.


 // Example of Type Promotion if matching found 
class Product {
    public void adding(int ID, long fee){
        System.out.println("Check 1: Id : "+ID+" fee : "+fee);
      }
      public void adding(int fee,int ID) // here matching found so this method will get called 
      {
         System.out.println("Check 2: Id : "+ID+" fee : "+fee);
      }
  }
  class DockerTpoint {
      public static void main(String[] args){
       Product product = new Product();
       product.adding(113,1400);
      }
  }
  



Output:

Check 2: Id : 1400 fee : 113

Example of Type Promotion in the case of uncertainty


There will be ambiguity if there are no arguments of the same type in the method and each method encourages a similar number of arguments.


 // Example of Type Promotion in the case of uncertainty 
class Product {
    public void adding(int ID, long fee){
        System.out.println("Check 1: Id : "+ID+" fee : "+fee);
      }
      public void adding(long fee,int ID) // here matching found so this method will get called 
      {
         System.out.println("Check 2: Id : "+ID+" fee : "+fee);
      }
  }
  class DockerTpoint {
      public static void main(String[] args){
       Product product = new Product();
       product.adding(113,1400);  //C.T Error
      }
  }
  



Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem: The method adding(int, long) is ambiguous for the type Product

Java method overriding Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Difference between Early and Late Binding in Java
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.