This is one of the confusing concept that whether we can overload or override static methods. so lets discuss at solution.
-> Can Static methods be overloaded?
The answer to this question is "YES. The static methods can be simply overloaded like other methods. For static method overloading we just need to keep the name same and signature/parameter different.
Eg: In the below example static method ABC is overloaded
-> Can we override Static methods ?
The answer to this question is "NO". It can not be overridden. If we make a method with same name in new class then it is considered as new method by java. Compiler will not give any error when we make a method in extended class with the same name as the parent class. Few more points to be considered :
Here a base class is created with overloaded methods named "a"
Output:
static method 2
Static Method 3
static method 2
Static Method 3
static method 2
Lets discuss each approaches:
1) Since static methods can be called directly by class name , so if we call the static method of base class by class name then only the method present in base will execute
2) In this case we called the method of the extended/overridden class by class name. Now the static method present in overridden class will called.
4) This is similar to 2. We have created object of extended class "Overr" and called the method "a". Here static method present in overridden class will be called.
5) In this approach we have created the object of child class by giving reference of base class. As per overriding rule the method which is overridden should be called, but static methods doesn't supports overriding so only base method will be called. This is the most important concept of static class.
-> Can Static methods be overloaded?
The answer to this question is "YES. The static methods can be simply overloaded like other methods. For static method overloading we just need to keep the name same and signature/parameter different.
Eg: In the below example static method ABC is overloaded
public class Testing {
public static void ABC() {
System.out.println("Method ");
}
public static void ABC(int a) {
System.out.println("OverLoaded Method with argument" + a);
}
public static void main(String args[])
{
Test.ABC();
Test.ABC(10);
}
}
-> Can we override Static methods ?
The answer to this question is "NO". It can not be overridden. If we make a method with same name in new class then it is considered as new method by java. Compiler will not give any error when we make a method in extended class with the same name as the parent class. Few more points to be considered :
Here a base class is created with overloaded methods named "a"
public class Base{
public static void a(int a) {
System.out.println("static method 1");
}
public static void a() {
System.out.println("static method 2");
}
}
Here we extend the base class mentioned above and made a method with the same name as "a"
public class Overr extends Base{
public static void a() {
System.out.println("Static Method 3");
}
@Test
public void b() {
Base.a();
Overr.a();
Base a1 = new Base();
a1.a();
Overr a2 = new Overr();
a2.a();
Base a3 = new Overr();
a1.a();
}
}
Now we called the method in 5 different ways and found the output below as:
static method 2
Static Method 3
static method 2
Static Method 3
static method 2
Lets discuss each approaches:
1) Since static methods can be called directly by class name , so if we call the static method of base class by class name then only the method present in base will execute
Base.a();//output static method 2
2) In this case we called the method of the extended/overridden class by class name. Now the static method present in overridden class will called.
Overr.a();// output Static Method 3
3) This is similar to 1. We can also call the static method by making object of class. So if we create object of base class then only the method present in base will execute
Base a1 = new Base(); a1.a(); //output static method 2
Overr a2 = new Overr(); a2.a(); // output Static Method 3
Base a3 = new Overr(); a1.a();//output static method 2
Hence from this logic we can say that static method cant be overridden . If we try to override it a new method is created.
No comments:
Post a Comment