Enter your keyword

post

Overloading

Definition


Method overloading in Java is a programming concept when programmer declare two methods of same name but with different method signature e.g. change in argument list or change in type of argument. method overloading is a powerful Java programming technique to declare method which does similar performance but with different kind of input. One of the most popular example of method overloading is System.out.println() method which is overloaded to accept all kinds of data types in Java.

Example


class MyClass {
int height;
MyClass() {
System.out.println("bricks");
height = 0;
}
MyClass(int i) {
System.out.println("Building new House that is "
+ i + " feet tall");
height = i;
}
void info() {
System.out.println("House is " + height
+ " feet tall");
}
void info(String s) {
System.out.println(s + ": House is "
+ height + " feet tall");
}
}
public class MainClass {
public static void main(String[] args) {
MyClass t = new MyClass(0);
t.info();
t.info("overloaded method");
//Overloaded constructor:
new MyClass();
}
}

Commonly asked interview questions on Constructors

1.What is method overloading in java?

Answer : Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed constructor overloading that allows a class to have more than one constructors having different argument lists.

2) What are the other names for method overloading?

Answer :Method overloading also known as static polymorphism or compile time polymorphism because at compile time itself we can tell which method going to get executed based on method arguments.
So method overloading also called as static binding.

3) What are the basic rules of method overloading?

Answer : Defining a method with same name and differ in number of arguments
Defining a method with same name and differ in type of arguments
Defining a method with same name and differ in order of type of arguments
Return type of the method not involved in meth

4)What is meant by method overloading in java?

Answer :Defining multiple methods with same name is known as polymorphism.
Defining multiple methods with same name and with different arguments known as method overloading.

package com.instanceofjava;
class A{

public void show(int a){
System.out.println(“saidesh”);
}

public void show(int a,int b){
System.out.println(“ajay”);
}

public void show(float a){
System.out.println(“vinod”);
}
public static void main(String args[]){

A a=new A();

a.show(10);
a.show(1,2);
a.show(1.2);

}
}
Output:

saidesh
ajay
vinod

5) .Can we overload static methods in java?

Answer :Yes. We can overload static methods in java.
Method overriding is not possible but method overloading is possible for static methods.
Before that lets see about method overloading in java.
lets see an example java program which explains static method overloading.
class StaticMethodOverloading{

public static void staticMethod(){

System.out.println(“staticMethod(): Zero arguments”);

}

public static void staticMethod(int a){

System.out.println(“staticMethod(int a): one argument”);

}

public static void staticMethod(String str, int x){

System.out.println(“staticMethod(String str, int x): two arguments”);

}

public static void main(String []args){

StaticMethodOverloading.staticMethod();
StaticMethodOverloading.staticMethod(12);
StaticMethodOverloading.staticMethod(“Static method overloading”,10);

}
}

Output:

staticMethod(): Zero arguments
staticMethod(int a): one argument
staticMethod(String str, int x): two arguments

Take Sample Test

Reference Books


1. Head First Java

No Comments

Add your review

Your email address will not be published.