0% found this document useful (0 votes)
29 views4 pages

Pract 6

Uploaded by

Up ka sher king
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)
29 views4 pages

Pract 6

Uploaded by

Up ka sher king
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
You are on page 1/ 4

Interface in Java

An interface in Java is a blueprint of a class. It has sta c constants and abstract methods.

The interface in Java is a mechanism to achieve abstrac on. There can be only abstract methods in
the Java interface, not method body. It is used to achieve abstrac on and mul ple inheritance in
Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot have a
method body.

o Java Interface also represents the IS-A rela onship.

o It cannot be instan ated just like the abstract class.

o Since Java 8, we can have default and sta c methods in an interface.

o Since Java 9, we can have private methods in an interface.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

o It is used to achieve abstrac on.

o By interface, we can support the func onality of mul ple inheritance.

o It can be used to achieve loose coupling.

AD

How to declare an interface?


An interface is declared by using the interface keyword. It provides total abstrac on; means all the
methods in an interface are declared with the empty body, and all the fields are public, sta c and
final by default. A class that implements an interface must implement all the methods declared in
the interface.

Syntax:

1. interface <interface_name>{

2.

3. // declare constant fields

4. // declare methods that abstract

5. // by default.

6. }

Example:

1. interface Animal {

2. void eat();

3. void sleep();

4. }

In this example, the Animal interface declares two method signatures: eat() and sleep(). Any class
implemen ng the Animal interface must provide concrete implementa ons for these methods.

AD

Java 8 Interface Improvement

Since Java 8, interface can have default and sta c methods which is discussed later.

Internal addi on by the compiler

The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds
public, sta c and final keywords before data members.

In other words, Interface fields are public, sta c and final by default, and the methods are public and
abstract.

Rela onship 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.

Java Interface Example

In this example, the Printable interface has only one method, and its implementa on is provided in
the A6 class.

File Name: InterfaceExample.java

1. interface printable{

2. void print();

3. }

4. class InterfaceExample implements printable{

5. public void print(){System.out.println("Hello");}

6. public sta c void main(String args[]){

7. InterfaceExample obj = new InterfaceExample();

8. obj.print();

9. }

10. }

Output:

Hello

AD

Java Interface Example: Drawable


In this example, the Drawable interface has only one method. Its implementa on is provided by
Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its
implementa on is provided by different implementa on providers. Moreover, it is used by someone
else. The implementa on part is hidden by the user who uses the interface.

File: TestInterface1.java

1. //Interface declara on: by first user

2. interface Drawable{

3. void draw();

4. }

5. //Implementa on: by second user

6. class Rectangle implements Drawable{

7. public void draw(){System.out.println("drawing rectangle");}

8. }

9. class Circle implements Drawable{

10. public void draw(){System.out.println("drawing circle");}

11. }

12. //Using interface: by third user

13. class TestInterface1{

14. public sta c void main(String args[]){

15. Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()

16. d.draw();

17. }}

Test it Now

Output:

You might also like