You are on page 1of 12

Java Interfaces

Interface Naming Conflicts


Case-I: Two interfaces containing the same method name & same
return type:
Interface Naming Conflicts (Contd.)
Case-II: Two interfaces containing the same method name & different
argument types:
Interface Naming Conflicts (Contd.)
• Case-III: Two interfaces contain a method with same signature but
different return types

Note: The above code will get compilation errors due to variant return types.
Interface variable naming conflicts
Case: Two interface containing same variable name

Note: Code will get compilation error as “Reference to x is ambiguous”


Interface variable naming conflicts
Variable ambiguity problem can be solved by using interface names

Note: Above code works well.


Problem
• If an interface contains 100 methods and a class has to implement only a specific method then it
can implement that method along with other methods too or it can implement that method and
declare it as abstract class and it will let its child classes implement other methods.

• If a class implements an interface, for each and every method of that interface,
compulsorily, it is must to provide implementation whether it is required or not.

• The problem with this approach is:


a) Reduces code Readability
b) Increases length of the code

• The above problem can be solved using adapter class discussed next.
Adapter Class

Adapter class is a java class that implements an interface with only


empty implementation.

Advantages:
A) Simplifies complexity of the program.
B) Best utility to the programmer for their ease.
Use of Adapter Class in java
Difference between Interface and Abstract class

Interface Abstract class 1. when nothing is


known 1.If partial implementation
about implementation. is known.
2. Every method is always 2. Every method inside abstract
public and abstract class need not to be public and
abstract.
3. Every variable present inside 3. Every variable inside abstract
interface is always public, static class need not be public,static
and final. and final.
Difference between Interface and Abstract class

Interface Abstract class 4. For interface variable ,


4. Abstract class variables are not
initialization is done at the time required to perform initialization at
of declaration only. the time declaration.

5. Static blocks are not 5. Static blocks are allowed inside an


allowed inside an interface. Abstract Class.
6. We cannot declare a constructor 6. Constructor can be declared inside
inside an interface. Abstract class.
Questions?
Q1. we cannot create an objects for abstract class and interface but
abstract class can contain constructor but interface does not contain
Constructor. Why? Give your reasons?

Q2. inside an interface, every method is always an abstract and we can


take abstract methods also in abstract class as well, then what is the
difference between an abstract class and an interface? Can we replace
interface with an abstract class?

You might also like