static method overloading overriding java code example

Example 1: what is method overloading and method overriding in Java?

Method overloading is providing two separate methods in a class with the same name but different arguments, while the method return type may or may not be different, which allows us to reuse the same method name.

Method overriding means defining a method in a child class that is already defined in the parent class with the same method signature, same name, arguments, and return type

Example 2: can we overload a static method in java

package test.main;
 
public class TestOverload {
 
	public static void main(String args[]) {
 
		TestOverload.show();
		TestOverload.show("FrugalisMinds");
	}
 
	public static void show() {
		System.out.println("Show Called ");
	}
 
	public   void show(String name) {
		System.out.println("Overloaded Show Called" + name);
	}
}

Example 3: can we overload a static method in java

package test.main;
 
public class TestOverload {
 
	public static void main(String args[]) {
 
		TestOverload.show();
		TestOverload.show("FrugalisMinds");
	}
 
	public static void show() {
		System.out.println("Show Called ");
	}
 
	public static void show(String name) {
		System.out.println("Overloaded Show Called" + name);
	}
}

Example 4: can we overload a static method in java

package test.main;
 
public class TestOverload {
 
	public static void main(String args[]) {
 
		Base obj = new Derived();
          
	       obj.display();  
	       obj.print(); 
	}
 
}
 
class Base {
 
	public static void display() {
		System.out.println("Base Class Display Called");
	}
 
	public void print() {
		System.out.println("Base Class Print Called");
	}
}
 
class Derived extends Base {
 
	public static void display() {
		System.out.println("Derived Class Display Called");
	}
 
	public void print() {
		System.out.println("Derived Class Print Called");
	}
}

Tags:

Java Example