Java method Overloading and Overriding example


On this page, you will learn Java method overloading and overriding example. Method overloading and overriding covers the Polymorphism concept in object-oriented programming (OOPs). Polymorphism is derived from two geeks words “poly” means many and “morphs” means forms.

In short, you can say performing a single action in different ways. There are two types of Polymorphism:

  1. Runtime Polymorphism
  2. Compile Time Polymorphism

Method Overloading Example

Method overloading come under the Compile Time Polymorphism. If a class have multiple methods with the same name but differ in signature or number of argument. Those methods called an overloaded method and can be performed with the class.

package org.websparrow;

public class MthodOverloadingDemo {

	public int add(int x, int y) {
		return x + y;
	}

	public int add(int x, int y, int z) {
		return x + y * z;
	}

	public String greet(String name) {

		return "Hello " + name;
	}

	public String greet(String name, int age) {

		return "Hello " + name + ". Your are " + age + " years old.";
	}

	public static void main(String[] args) {

		MthodOverloadingDemo demo = new MthodOverloadingDemo();

		System.out.println(demo.add(5, 5));
		System.out.println(demo.add(5, 5, 10));
		System.out.println(demo.greet("Atul"));
		System.out.println(demo.greet("Atul", 25));
	}

}

You can also overload the static method.

public static void country() {
    System.out.println("INDIA");
}

public static void country(String name) {
    System.out.println("Your country name is:" + name);
}

Method Overriding Example

Method overriding covers the Runtime Polymorphism. Method overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. But make sure subclass has the same method name and arguments. If you are overriding any method in your class use @Override annotation, it helps to recognize these are overridden method.

package org.websparrow;

class Student {

	public void result() {
		System.out.println("If you score at least 70 % marks then you are passed.");
	}
}

public class MthodOverridingDemo extends Student {

	@Override
	public void result() {
		System.out.println("50 % marks is enough to jump into next class.");
	}

	public static void main(String[] args) {

		MthodOverridingDemo demo = new MthodOverridingDemo();
		demo.result();
	}

}

Rules for Method Overriding

There are some rules that must keep in mind before overriding any method.

  1. You can not perform the method overriding in the same class.
  2. The method must have the same name and same arguments as in the super or parent class.
  3. A method declared final cannot be overridden.
  4. A method declared static cannot be overridden but can be re-declared.
  5. If a method cannot be inherited then it cannot be overridden.
  6. You can not override the constructors.

References

  1. Defining Methods

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.