You are on page 1of 3

WHY DO WE NEED INTERFACES

An interface is a guarantee. When a class implements an interface, it is


guaranteeing that it will have public implementations for the methods listed in
the interface. The interface itself provides no implementations of methods. It's
just a list of what you can expect.
So let's say you have a "HappyAnnesha" class that implements the "Annesha"
interface. You have another class "EmotionalAnnesha" class having a
"callAnnesha" method.
You don't want your user bothered with the fact that whether Annesha is happy
when they call the method. So you can have callAnnesha return an object of an
interface type, Annesha, rather than the class HappyAnnesha with the full
implementation. The Annesha interface guarantees that no matter in what
state Annesha is, whether a HappyAnnesha, SadAnnesha, or else, will have a
"readNiceQuotes" method, and a "loveTaraMore" method. Annesha might be
happy and might love Tara more, and record very enthusiastic quotes, but you
don't have to worry about that.

1. interface Annesha //Just declarations, no implementation


2. {
3.

void readNiceQuotes();

4.

int loveTaraMore();

5.
6. }
7.
8. class HappyAnnessha implements Annesha{
9.

public void readNiceQuotes() {


System.out.println("Its one thing to fall in love. Its another to
feel someone else fall in love with you, and to feel a responsibility toward
that love. ");

10.

11.
12.

public int loveTaraMore()

13.

14.

return infnity;

15.

16.
17.
18.

19.
20.

class SadAnnesha implements Annesha {

21.

public void readNiceQuote() {

22.

System.out.println("Jao.");

23.

24.
25.

public int loveTaraMore()

26.

27.

return (infinity-1);

28.

When a class implements an interface, you can think of the class as signing
a contract, agreeing to perform the specific behaviors of the interface. If a
class does not perform all the behaviors of the interface, the class must
declare itself as abstract.
A class uses the implements keyword to implement an interface. The
implements keyword appears in the class declaration following the extends
portion of the declaration.

Simply put, abstract class achieves partial abstraction (0 to 100%) whereas


interface achieves fully abstraction (100%).

Interface
Interface
Interface
Interface
Interface

can have only abstract methods.


supports multiple inheritance.
has only static and final variables.
can't have static methods, main method or constructor.
can't provide the implementation of abstract class.

Achieving Multiple Inheritance using Interfaces

The following are the examples of multiple inheritance in Java. The


first method is not applicable for Java (Its applicable for C++
though, and even in Java 8), hence the wordwround using Interfaces.

First one uses the classes and the second one uses Interfaces
/***********************Using Classes*************/
class A
{
public void methodAClassA()
{
System.out.println("In ClassA MethodA");
}
}
class B extends A{
public void methodAClassA(){
System.out.print("in Class B Method ClassA");
}
}
class C extends A,B {
/*
* Which is not possible becuase
* if u use the above code the compiler does not
* know which instace method could be used in the class C
* So they made it as error it will show the compilation error
*
*/
}
/******************Using Interfaces*******************/
interface A
{
public void methodAClassA();
}
interface B extends A{
public void methodAClassA();
}
class C implements A,B {
/*
* Which is possible becasue
* if we use the above code there is no need of the
* knowing which method instace could be used
*/
}

You might also like