
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Interface Naming Conflicts in Java
In Java, interfaces serve two purposes pure abstraction and multiple inheritance. Generally, an interface consists of abstract methods and variables that define the behavior which a class can implement. If we create two interfaces that contain methods and variables with the same name, then interface naming conflicts may arise. However, it is not the only scenario that can cause this conflict, we are going to explore all the possible situations causing interface naming conflicts.
Interface Naming Conflicts in Java
Before heading to the interface naming conflicts, it is necessary to understand the abstract methods and how to create interfaces in Java.
Abstract method
It is a method without any implementation or body and is declared using the abstract keyword. Creating an abstract method is a way of achieving abstraction. Here, the term abstraction means hiding those details that are not useful for a common user.
These methods can be declared either inside abstract classes or inside interfaces. To use the functionality of an abstract method one needs to inherit the method by implementing the interface.
Defining an Interface
Other than abstract methods and variables, an interface can also contain default methods, constants, and static methods. To create an interface, we use the keyword 'interface' and to access its members within a class, we need to use the 'implements' keyword while defining that class.
Syntax
interface nameOfInterface { method1(); method2(); }
Naming Conflicts of Interface
There are two types of naming conflicts related to Interfaces ?
Variable Naming Conflict
Method Naming Conflict
Let's discuss them one by one with the help of example programs.
Variable Naming Conflict
When we declare two variables with the same name in two different interfaces, then it causes a conflict and we may encounter an error. One way to resolve this error is that try to access those variables using name of the interface as a reference.
Example
The following example illustrates how to resolve the variable naming conflict of Interface.
interface String1 { // interface 1 String message = "Tutorialspoint"; } interface String2 { // interface 2 String message = "Tutorix"; } public class IntrfExample1 implements String1, String2 { public static void main(String []args){ // calling the variables using interfaces System.out.println(String1.message); System.out.println(String2.message); } }
Output
Tutorialspoint Tutorix
Method Naming Conflict
There exist multiple situations related to method naming conflict.
Case 1
When the declared methods have the same signature and same return type, then we can't know which method is getting executed by the compiler as illustrated in the next example.
Example
interface Message1 { // interface 1 public void method(); } interface Message2 { // interface 2 public void method(); } public class IntrfExample2 implements Message1, Message2 { // using the method here public void method() { System.out.println("Tutorialspoint"); } public static void main(String []args){ // object creation IntrfExample2 exp = new IntrfExample2(); exp.method(); // method calling } }
Output
Tutorialspoint
Case 2
When the declared methods have the same name but different signatures, then we need to overload them and provide different implementations also as demonstrated in the next example.
Example
interface Message1 { // interface 1 public void method(); } interface Message2 { // interface 2 public void method(int id); } public class IntrfExample3 implements Message1, Message2 { // using the method here public void method() { System.out.println("Tutorialspoint"); } public void method(int id) { System.out.println("ID: " + id); } public static void main(String []args){ // object creation IntrfExample3 exp = new IntrfExample3(); // method calling exp.method(); exp.method(125); } }
Output
Tutorialspoint ID: 125
Case 3
When the declared methods have the same name, but different return types then we need to implement them in two distinct classes otherwise we will encounter an error.
Example
interface Message1 { // interface 1 public int method(); } interface Message2 { // interface 2 public String method(); } class Class1 implements Message1 { // using the first method here public int method() { return 125; } } class Class2 implements Message2 { // using the second method here public String method() { return "Tutorialspoint"; } } public class IntrfExample { public static void main(String []args){ // Creating objects Class1 exp1 = new Class1(); Class2 exp2 = new Class2(); // method calling System.out.println(exp1.method()); System.out.println(exp2.method()); } }
Output
125 Tutorialspoint
Conclusion
In this article, we have learned the interface naming conflicts in Java. It may occur when we work with two interfaces that contain methods and variables with the same name. To avoid these conflicts, we discussed various approaches with the help of example programs.