0% found this document useful (0 votes)
51 views

Interface

An interface in Java is a blueprint of a class that defines abstract methods and static constants. Interfaces are used to achieve abstraction and multiple inheritance in Java. An interface cannot be instantiated or contain method bodies, only abstract method signatures. Classes implement interfaces to inherit their abstract methods. Interfaces represent "is-a" relationships and are declared using the interface keyword followed by abstract methods.

Uploaded by

honaday945
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)
51 views

Interface

An interface in Java is a blueprint of a class that defines abstract methods and static constants. Interfaces are used to achieve abstraction and multiple inheritance in Java. An interface cannot be instantiated or contain method bodies, only abstract method signatures. Classes implement interfaces to inherit their abstract methods. Interfaces represent "is-a" relationships and are declared using the interface keyword followed by abstract methods.

Uploaded by

honaday945
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/ 7

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.

It cannot be instantiated just like the abstract class.

Why use Java interface?

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

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling

How to declare an interface?

An interface is declared by using the interface keyword. It provides total


abstraction; means all the methods in an interface are declared with the
empty body, and all the fields are public, static and final by default. A class
that implements an interface must implement all the methods declared in
the interface.

Syntax:
interface <interface_name>{

// declare constant fields


// declare methods that abstract
// by default.
}
interface Parent
{
void m1();
void m2();
}
class Child implements Parent
{
public void m1()
{
System.out.println("this is m1() method");
}
public void m2()
{
System.out.println("this is m2() method");
}
public static void main(String args[])
{
Child c=new Child();
c.m1();
c.m2();
}
}
Output:
This is m1() method
This is m2() method
interface AA
{
void show();
}
interface BB
{
void display();
}
class CC implements AA,BB
{
public void show()
{
System.out.println("this is show method");
}
public void display()
{
System.out.println("this is display method");
}
public static void main(String args[])
{
CC c=new CC();
c.show();
c.display();
}
}
Output:
This is show method
This is display method
interface Shape
{
void draw();
}
class Triangle implements Shape
{
public void draw()
{
System.out.println("drawing Triange......");
}
}
class Square implements Shape
{
public void draw()
{
System.out.println("drawing square......");
}
}
class Line implements Shape
{
public void draw()
{
System.out.println("drawing line......");
}
}
class Circle implements Shape
{
public void draw()
{
System.out.println("drawing circle......");
}
}
class ShapeDemo
{
public static void main(String args[])
{
Triangle t=new Triangle();
t.draw();
Square s=new Square();
s.draw();
Line l=new Line();
l.draw();
Circle c=new Circle();
c.draw();
}
}
Output:
drawing Triangel.....
drawing square.....
drawing line.....
drawing circle.....

Nested Interface
• An interface i.e. declared within another interface or class is
known as nested interface. The nested interfaces are used to
group related interfaces so that they can be easy to maintain.
The nested interface must be referred by the outer interface or
class. It can't be accessed directly.
Points to remember for nested interfaces
• Nested interface must be public if it is declared inside the
interface but it can have any access modifier if declared within
the class.
• Nested interfaces are declared static implicitly.

interface A{
void m1();
interface B {
void m2();
}
}
class T implements A,A.B{
public void m1(){
System.out.println("this is m1 method");
}
public void m2(){
System.out.println("this is m2 method");
}
public static void main(String args[])
{
T t=new T();
t.m2();
t.m1();
}
}
Output:
This is m2 method
This is m1 method

You might also like