Interface Naming Conflicts in Java
Last Updated :
18 Feb, 2022
Interfaces in Java consist of abstract methods (which do not contain a body) and variables (which are public static final). Implementation of the methods of the interface is defined in classes that implement that interface. It helps Java to achieve abstraction.
Naming Conflicts occur when a class implements two interfaces that have methods and variables with the same name.
Interface Naming Conflicts
As Interface consists of variables and methods, So two types of naming conflicts can happen.
- Method Naming Conflict
- Variable Naming Conflict
1. Method Naming Conflict
This method naming conflicts can happen in various cases.
Case-1: When we have two interfaces with the same method name, same signature, same return type, the implementation class can implement any one of them, and we can’t say which one of these implemented.
Code
Java
public interface Interface1 {
public void show();
}
|
Java
public interface Interface2 {
public void show();
}
|
Java
public class Case1 implements Interface1, Interface2 {
public void show()
{
System.out.println( "Geeks For Geeks" );
}
public static void main(String[] args)
{
Case1 obj = new Case1();
obj.show();
}
}
|
Output
Geeks For Geeks
Explanation: Here in the below code, we can’t confirm which show a method of 2 interfaces gets executed. No error will be thrown.
Case-2: When two interfaces consist of methods with the same name but different signatures, then in the implementation class, we need to implement both the methods and methods get executed based on the kind of method we called.
Code
Java
public interface Interface1 {
public void show();
}
|
Java
package geeks;
public interface Interface2 {
public void show(String s);
}
|
Java
public class Case2 implements Interface1, Interface2 {
@Override public void show(String s)
{
System.out.println(s);
}
@Override public void show()
{
System.out.println( "Geeks For Geeks" );
}
public static void main(String[] args)
{
Case2 obj = new Case2();
obj.show();
obj.show( "GFG" );
}
}
|
Output
Geeks For Geeks
GFG
Explanation: In this case, the execution of these methods can be differentiated based on the signature, and return type is ignored in this case.
Case-3: When two interfaces contain a method with the same name, same signature but different return types, in this case, both interfaces can’t be implemented in the same class. Separate classes need to be created to implement each interface of that type.
Code
Java
public interface Interface1 {
public void show();
}
|
Java
public interface Interface2 {
public String show();
}
|
Java
public class Case3_1 implements Interface1 {
@Override public void show()
{
System.out.println( "Geeks for Geeks" );
}
public static void main(String[] args)
{
Case3_1 obj = new Case3_1();
obj.show();
}
}
|
Output
Geeks for Geeks
Explanation: In this case, we can’t implement both interfaces in the same class because an error can be thrown due to ambiguity between them. So we need to create a different class to implement Interface2.
Other Examples:
Java
public class Case3_2 implements Interface2 {
@Override public String show() { return "GFG" ; }
public static void main(String[] args)
{
Case3_2 obj = new Case3_2();
String res = obj.show();
System.out.println(res);
}
}
|
Output
GFG
If we try to implement both interfaces in the same class then, it will throw an error.
Code (Throws error)
Java
public class Case3_1_2 implements Interface1, Interface2 {
public void show()
{
System.out.println( "Geeks for Geeks" );
}
@Override public String show()
{
String s = "GFG" return s;
}
public static void main(String[] args)
{
Case3_1_2 obj = new Case3_1_2();
obj.show();
}
}
|
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The return type is incompatible with Interface2.show()
Duplicate method show() in type Case3_1_2
- As we implemented methods having the same signature and names with a different return type of both the interfaces in the same class throws an error.
- It is impossible to implement these in the same class.
Now we will move into another type of interface naming conflicts
2. Variable Naming Conflict
When two interface consists of the variable with the same name, then the class which implements those interfaces can’t identify which variable to access and throws an error. Hence, to resolve that, access the variable using the interface name as the reference.
Code
Java
public interface Interface1 {
String s = "Geeks for Geeks" ;
}
|
Java
public interface Interface2 {
String s = "GFG" ;
}
|
Java
public class VariableConflict
implements Interface1, Interface2 {
public static void main(String[] args)
{
VariableConflict obj = new VariableConflict();
System.out.println(Interface1.s);
System.out.println(Interface2.s);
}
}
|
Output
Geeks for Geeks
GFG
Explanation: Here, if we don’t use of reference of interface name while accessing strings s it throws an ambiguous reference error. So using the Interface name as a reference prevents ambiguity while accessing.
Similar Reads
Cloneable Interface in Java
The Java.lang.Cloneable interface is a marker interface. It was introduced in JDK 1.0. There is a method clone() in the Object class. Cloneable interface is implemented by a class to make Object.clone() method valid thereby making field-for-field copy. This interface allows the implementing class to
4 min read
Function Interface in Java
The Function Interface is a part of the java.util.function package that has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in one argument and produces a result. Hence, this functional interface takes in 2 generics, namely as follows: T
7 min read
Naming Conventions in Java
A programmer is always said to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way out to read the code. At a smaller level, this seems meaningless but think of the industrial level where it becomes necessary to write clean codes in order to
5 min read
Evolution of interface in Java
In Java, interfaces define a contract for classes without specifying how the methods are implemented. With time, Java has introduced major enhancements or we can say evolution of interfaces to make them more powerful. So, in this article, we will explore the evolution of interfaces in Java from Java
3 min read
Map Interface in Java
In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types. Key Features: No Duplicates in Keys: Keys should be
12 min read
Closeable Interface in Java
A Closeable is a source or destination of the data that needs to be closed. The close() method is invoked when we need to release resources that are being held by objects such as open files. It is one of the important interfaces to stream classes. Closeable Interface was introduced in JDK 5 and is d
3 min read
Collection Interface in Java with Examples
The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
6 min read
Resolving Conflicts During Multiple Inheritance in Java
A class can implement multiple interfaces in java, but what if the implemented multiple default interfaces have default methods with the same signatures? Then in the implementing class, which of the default implementations would be invoked from the several parent interfaces. Java 8 designers have be
5 min read
Java Functional Interfaces
A functional interface in Java is an interface that contains only one abstract method. Functional interfaces can have multiple default or static methods, but only one abstract method. Runnable, ActionListener, and Comparator are common examples of Java functional interfaces. From Java 8 onwards, lam
7 min read
Nested Interface in Java
We can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declared inside
5 min read