Can we define two methods in a class with the same name?

Can we define two methods in a class with the same name?

Yes, We can define two methods in a class with the same name but with different number/type of parameters. Which method is to get invoked will depend upon the parameters passed.

 

class Test {

public void print()

{

System.out.println("Print method without parameters.");

}

public void print(String name)

{

System.out.println("Print method with paramter");

}

public static void main(String args[]) {

Test t=new Test();

t.print();

t.print("pradeep");

}

}

Read More →