- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we define multiple methods in a class with the same name in Java?
Yes, we can define multiple methods in a class with the same name but with different types of parameters. Which method is to get invoked will depend upon the parameters passed.
In the below example, we have defined three display methods with the same name but with different parameters. Depending on the parameters, the appropriate method will be called.
Example
public class MethodWthSameNameTest { public void display() { // method with no parameters System.out.println("display() method with no parameter"); } public void display(String name) { // method with a single parameter System.out.println("display() method with a single parameter"); } public void display(String firstName, String lastName) { // method with multiple parameters System.out.println("display() method with multiple parameters"); } public static void main(String args[]) { MethodWthSameNameTest test = new MethodWthSameNameTest(); test.display(); test.display("raja"); test.display("raja", "ramesh"); } }
Output
display() method with no parameter display() method with a single parameter display() method with multiple parameters
- Related Articles
- Can we define a method name same as class name in Java?
- Can we define an abstract class with no abstract methods in Java?
- Can we define a try block with multiple catch blocks in Java?
- Can we define an enum inside a class in Java?
- Can we define a class inside a Java interface?
- Can we define an interface inside a Java class?
- Can we have multiple type parameters in generic methods in Java?
- Can we define a parameterized constructor in an abstract class in Java?
- Can we define an abstract class without abstract method in java?
- Can we define constant in class constructor PHP?
- Can we define a static constructor in Java?
- Why the constructor name is same as the class name in Java?
- Can a method have the same name as the class?
- Can we override private methods in Java
- Can we override final methods in Java?

Advertisements