can we override static method in java can i override static method in java can you override static method in java can we overload static method in java can i overload static method in java can you overload static method in java
Can
we override static method in java?
A static method is not belongs to an object
and there is no notion for this nor
super
in
a static method. A static method is a global
function. Hence there is also no occurrence
of polymorphism and method override
make no sense.
Can
we overload static method in java?
Overloading static
method In Java
Yes, we can overload
static method in Java. In terms of method overloading static method
are just like normal methods and in order to overload static method you
need to provide
another static method with same name but different method
signature. Static overloaded
method are resolved using Static binding during
compile time. Overloading method in Java
is completely different than
overriding method and as discussed in our last article we cannot
override
static method in java but we can certainly overload static method in Java.
The Following Example Illustrate the Static method overloading
//StaticOverloading.java
public class StaticOverloading {
public static void main(String args[]) {
fun1("javajavax"); //will call static method with one String argument
fun1("javajavax", "welcome to"); //overloaded static method will be call
}
//static method which will be overloaded
public static void greet(String name){
System.out.println("Hello " + name);
}
public static void main(String args[]) {
fun1("javajavax"); //will call static method with one String argument
fun1("javajavax", "welcome to"); //overloaded static method will be call
}
//static method which will be overloaded
public static void greet(String name){
System.out.println("Hello " + name);
}
//Another static method which overload above Hello method
//This shows that we can overload static method in Java
public static void fun(String name, String greeting){
System.out.println(greeting + " " + name);
}
}
//This shows that we can overload static method in Java
public static void fun(String name, String greeting){
System.out.println(greeting + " " + name);
}
}
Output
Hello JavaJavax
Welcome to JavaJavax
No comments:
Post a Comment