0% found this document useful (0 votes)
18 views8 pages

Interface in Java

An interface in Java is a collection of abstract methods and constants used to achieve full abstraction and multiple inheritance. It cannot be instantiated, does not contain constructors, and all methods are implicitly public and abstract. Interfaces allow for loose coupling and can be implemented by classes, which can implement multiple interfaces simultaneously.

Uploaded by

alishapandey090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Interface in Java

An interface in Java is a collection of abstract methods and constants used to achieve full abstraction and multiple inheritance. It cannot be instantiated, does not contain constructors, and all methods are implicitly public and abstract. Interfaces allow for loose coupling and can be implemented by classes, which can implement multiple interfaces simultaneously.

Uploaded by

alishapandey090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Interface in Java

Interface is similar to class which is collection of public static final variables (constants) and

abstract methods.

The interface is a mechanism to achieve fully abstraction in java. There can be only abstract

methods in the interface. It is used to achieve fully abstraction and multiple inheritance in

Java.

Why we use Interface ?

• It is used to achieve fully abstraction.

• By using Interface, you can achieve multiple inheritance in java.

• It can be used to achieve loose coupling.

properties of Interface

• It is implicitly abstract. So we no need to use the abstract keyword when


declaring an interface.

• Each method in an interface is also implicitly abstract, so the abstract


keyword is not needed.

• Methods in an interface are implicitly public.

• All the data members of interface are implicitly public static final.

How interface is similar to class ?

Whenever we compile any Interface program it generate .class file. That means the bytecode

of an interface appears in a .class file.


How interface is different from class ?

• You can not instantiate an interface.

• It does not contain any constructors.

• All methods in an interface are abstract.

• Interface can not contain instance fields. Interface only contains public
static final variables.

• Interface is can not extended by a class; it is implemented by a class.

• Interface can extend multiple interfaces. It means interface support


multiple inheritance

Declaring Interfaces:

The interface keyword is used to declare an interface.


Example
interface Person
{
datatype variablename=value;
//Any number of final, static fields
returntype methodname(list of parameters or no parameters)
//Any number of abstract method declarations
}

Explanations

In the above syntax Interface is a keyword interface name can be user defined name the

default signature of variable is public static final and for method is public abstract. JVM will be

added implicitly public static final before data members and public abstract before method.

Example :-
public static final datatype variable name=value; ----> for data member
public abstract returntype methodname(parameters)---> for method
Implementing Interfaces:

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.

Example Interface in Java :-


interface Person
{
void run();
}
class Employee implements Person
{
public void run()
{
[Link]("Run fast");
}
}

When we use abstract and when Interface

If we do not know about any things about implementation just we have requirement

specification then we should be go for Interface

If we are talking about implementation but not completely (partially implemented) then we

should be go for abstract

Rules for implementation interface

• A class can implement more than one interface at a time.

• A class can extend only one class, but implement many interfaces.

• An interface can extend another interface, similarly to the way that a class
can extend another class.
Relationship between class and Interface

• Any class can extends another class

• Any Interface can extends another Interface.

• Any class can Implements another Interface


• Any Interface can not extend or Implements any class.

Difference between Abstract class and Interface

Abstract class Interface

It is collection of abstract method


1 It is collection of abstract method.
and concrete methods.

There properties can be reused There properties commonly usable in


2
commonly in a specific application. any application of java environment.

It does not support multiple


3 It support multiple inheritance.
inheritance.

Abstract class is preceded by


4 It is preceded by Interface keyword.
abstract keyword.

Which may contain either variable or


5 Which should contains only constants.
constants.

The default access specifier of There default access specifier of


6
abstract class methods are default. interface method are public.

These class properties can be


These properties can be reused in any
7 reused in other class using extend
other class using implements keyword.
keyword.

Inside abstract class we can take Inside interface we can not take any
8
constructor. constructor.
For the abstract class there is no
For the interface it should be compulsory
restriction like initialization of
9 to initialization of variable at the time of
variable at the time of variable
variable declaration.
declaration.

For the interface variable can not declare


There are no any restriction for
10 variable as private, protected, transient,
abstract class variable.
volatile.

There are no any restriction for For the interface method can not declare
11 abstract class method modifier that method as strictfp, protected, static,
means we can use any modifiers. native, private, final, synchronized.

Example of Interface in Java


interface Person
{
void run(); // abstract method
}
class A implements Person
{
public void run()
{
[Link]("Run fast");
}
public static void main(String args[])
{
A obj = new A();
[Link]();
}
}
Output :-
Run fast
Multiple Inheritance using interface
Example of Interface in Java
interface Developer
{
void disp();
}
interface Manager
{
void show();
}

class Employee implements Developer, Manager


{
public void disp()
{
[Link]("Hello Good Morning");
}
public void show()
{
[Link]("How are you ?");
}
public static void main(String args[])
{
Employee obj=new Employee();
[Link]();
[Link]();
}
}

Output
Hello Good Morning
How are you ?
Why interface have no constructor ?

Because, constructor are used for eliminate the default values by user defined values, but in

case of interface all the data members are public static final that means all are constant so no

need to eliminate these values.

Other reason because constructor is like a method and it is concrete method and interface

does not have concrete method it have only abstract methods that's why interface have no

constructor.

You might also like