Saturday, November 9, 2019

Can Nosotros Overload Static Method Inwards Coffee Plan - Example

Overloading static method In Java
Yes, nosotros tin overload static method inwards Java. In damage of method overloading static method are merely similar normal methods too inwards social club to overload static method y'all postulate to furnish unopen to other static method amongst same cite but unlike method signature. Static overloaded method are resolved using Static Binding during compile time. Overloading method inwards Java is completely unlike than overriding method too equally discussed inwards our final article nosotros can non override static method inwards Java but nosotros tin sure enough overload static method inwards Java. Here is an event which confirms that we tin overload static method inwards Java:

 

Overloading Static method inwards Java - example

 static method are merely similar normal methods too inwards social club to overload  Can nosotros overload static method inwards Java Program - ExampleIn this event nosotros stimulate got a static method called greet(String name) which takes a String declaration equally cite too impress a default greeting message equally "Hello John". Now to present that nosotros tin overload static method inwards Java I stimulate got provided unopen to other static method amongst same cite but unlike method signature which non entirely takes cite of somebody to greet but likewise greeting message e.g. Good Morning, Good Evening etc.

/**   
 * Java programme to present that nosotros tin overload static method inwards Java.
 */

public class StaticOverloadingTest {
 
    public static void main(String args[]) {
        greet("John"); //will telephone band static method amongst ane String argument
        greet("John", "Good Morning"); //overloaded static method volition endure telephone band    

    }
 
    /*
     * static method which volition endure overloaded
     */

    public static void greet(String name){
        System.out.println("Hello " + name);
    }
 
    /*
     * Another static method which overload higher upwards Hello method
     * This shows that nosotros tin overload static method inwards Java
     */

    public static void greet(String name, String greeting){
        System.out.println(greeting + " " + name);
    }


}
Output
Hello John
Good Morning John


That's all on How tin nosotros overload static method inwards Java. In summary, Don't confuse betwixt method overloading too method overriding. In short, y'all tin overload static method inwards Java but y'all tin non override static method inwards Java.



Other Java OOPS tutorial from JDK67


Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming inwards Java
Java - Object Oriented Programming [For Absolute Beginners]

No comments:

Post a Comment