Hello, Java folks! Have you ever tried to override the static method in java?
No? Let's override it ✌️. But before overriding the static method, let me briefly explain what the overriding and static method is.
What is the Static method?
The static method belongs to a class rather than an instance of a class.
If you want to call a static method, you don't need to create an object of the class (Amazing isn't it? ).
Static methods are also known as class methods.
Utility classes with general-purpose methods can be constructed using static methods.
Static methods are created using the static keyword.
You can call the static method directly by class Name.
Let's create one class called StaticMethodDemo class in which we will create a static method.
public class StaticMethodDemo {
private String name;
private String age;
public StaticMethodDemo(String name, String age) {
}
//static method
public static void iAmStaticMethod() {
System.out.println("I am static method, I can be called without creating object");
}
}
Now in the main method, we will call this static method without creating an object of StaticMethodDemo class.
public static void main(String[] args) {
StaticMethodDemo.iAmStaticMethod(); //I am static method, I can be called with out creating object
}
What is the method Overriding?
If the child class has the same method as declared in the parent class, it is known as method overriding.
Method overriding is used to give specific implementation in the method of child class which is already provided by the parent class.
It is used for runtime polymorphism.
Some Rules for the Java method overriding:
Child class should inherit from the parent class( Should have IS-A relationship).
Method name should be same as parent class.
The method has the same parameter as in the parent class.
Let's understand method overriding with an example.
Let's create one class Person which has one method named "introduction" in which there will be a print statement--> "Hello, I am a person".
public class Person {
private String name;
private String age;
private String gender;
public void introduction() {
System.out.println("Hello, I am a person");
}
}
Let's create another class Employee which will extend the Person class. Now we will override the Person class's introduction method in the Employee class with the print statement---> "Hello, I am an Employee".
public class Employee extends Person {
private int employee_id;
@Override
public void introduction() {
System.out.println("Hello, I am an Employee");
}
}
In the main method, we will create a reference variable of the Person class which will hold the object of the Employee class and call the introduction() method.
public class Main {
public static void main(String[] args) {
Person p = new Employee();
p.introduction();
}
}
Output:
Here you can see that the introduction method of the Person class is overridden. and "Hello, I am an Employee" is printed.
That is method overriding.
Now you know both static method and method overriding. Congratulations :)
Overriding Static methods
Now let's understand what happens when we try to override the static method. Taking the above example of Person and Employee classes, Let's make the introduction method static in both classes.
Person.java
public class Person {
private String name;
private String age;
private String gender;
public static void introduction() {
System.out.println("Hello, I am person ");
}
}
Employee.java
public class Employee extends Person {
private int employee_id;
public static void introduction() {
System.out.println("Hello, I am Employee ");
}
}
Output:
As you can see if we try to call the introduction method using the parent object reference, the introduction method of the parent class is called not of an Employee class. As we made the method static it becomes class level method and it does not depend on what kind of memory is allocated to it on runtime. It only depends on the reference variable.
Conclusion:
The child class method will be hidden and the parent class method will be called based on the object reference if we attempt to override the static method in the child class.
PS: This question is also often asked in interviews.
If you like my article, Please like and comment :)