1. 3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 1/4
Interfaces
Interface:
An interface in java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in
the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in
Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have
a method body.
Java Interface also represents the IS-A relationship.
If a class implements an interface and does not provide method bodies for all functions specified in
the interface, then class must be declared abstract.
Syntax :
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the
methods in interface are declared with empty body and are public and all fields are public, static and final
by default. A class that implement interface must implement all the methods declared in the interface. To
implement interface use implements keyword.
Why do we use interface ?
It is used to achieve total abstraction.
Since java does not support multiple inheritance in case of class, but by using interface it can achieve
multiple inheritance .
It is also used to achieve loose coupling.
Interfaces are used to implement abstraction. So the question arises why use interfaces when we
have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in interface are
final, public and static.
The relationship between classes and interfaces:
As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface.
1111111111111.jpg
2. 3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 2/4
interface Animal
{
public void animalSound();
public void sleep();
}
class Pig implements Animal
{
public void animalSound()
{
System.out.println("The pig says: wee wee");
}
public void sleep()
{
System.out.println("Zzz");
}
}
class MyMainClass
{
public static void main(String[] args)
{
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}
Multiple inheritance in Java by interface:
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as
multiple inheritance.
2222222222222.jpg
interface FirstInterface
{
public void myMethod();
}
interface SecondInterface
3. 3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 3/4
{
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface
{
public void myMethod()
{
System.out.println("Some text..");
}
public void myOtherMethod()
{
System.out.println("Some other text...");
}
}
class MyMainClass
{
public static void main(String[] args)
{
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
Important points about interface or summary of article:
We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it
that refers to the Object of its implementing class.
A class can implement more than one interface.
An interface can extends another interface or interfaces (more than one interface) .
A class that implements interface must implements all the methods in interface.
All the methods are public and abstract. And all the fields are public, static, and final.
It is used to achieve multiple inheritance.
It is used to achieve loose coupling.
The Difference between interface and abstract class:
Sno interface abstract class
1 If we don’t" know anything about If we are talking about implementation but
4. 3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 4/4
implementation just we have
requirement specification then we go
for interface.
not completely (partial implementation)
then we should go for abstract class.
2
Every method present inside interface
is always public and abstract whether
we are declaring or not.
Every method present inside abstract
class need not be public and abstract.
3
We can't declare interface methods with
the modifiers private, protected, final,
static, synchronized, native, strictfp.
there are no restrictions on abstract
classes class method modifiers.
4
Every interface variable is always public
static final whether we are declaring or
not.
Every abstract class variable need not be
public static final.
5
Every interface variable is always public
static final we can't declare with the
following modifiers. Private, protected,
transient, volatile.
There are no restrictions on abstract class
variable modifiers.
6
For the interface variables compulsory
we should perform initialization at the
time of declaration otherwise we will get
compile time error.
It is not require performing initialization for
abstract class variables at the time of
declaration.
7
Inside interface we can't take static and
instance blocks.
Inside abstract class we can take both
static and instance blocks.
8
Inside interface we can't take
constructor.
Inside abstract class we can take
constructor.