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

Interface

Uploaded by

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

Interface

Uploaded by

Thangaaruna M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Interface in JAVA

Presented By
M.Thanga Aruna, AP/CSE
P.S.R. Engineering College,
Sivakasi
Interface
• An interface in java is a blueprint of a class.
• An interface contains behaviors that a class implements.
• It has static constants and abstract methods.
• The interface in java is a mechanism to achieve abstraction and
multiple inheritance.
• Interface is declared by using interface keyword.
• A class that implement interface must implement all the methods
declared in the interface.
Declaration
• To define an interface, we write:
public interface [InterfaceName] {
// declare constant fields
// declare methods that abstract by default
}
Relationship between classes and
interfaces
• A class extends another class, an interface extends another interface but a
class implements an interface.
Various ways of Interface
Implementation
Example

interface printable{
void print();
}

class A implements printable{


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

public static void main(String args[]){


A obj = new A();
obj.print();
}
}
Multiple inheritance by interface
• If a class implements multiple interfaces, or an interface extends
multiple interfaces i.e. known as multiple inheritance.
Example
interface Printable{ public void show(){
void print(); System.out.println("Welcome");
} }
interface Showable{ public static void main(String args[]){
void show(); A7 obj = new A7();
}
obj.print();
class A7 implements
Printable,Showable{ obj.show();
public void print(){ }}
System.out.println("Hello");
}
Extending Interfaces
A class implements interface but one interface extends
another interface.
interface Printable{ public void show(){
void print1(); System.out.println("Welcome");
}
}
interface Showable extends Printable{
void show(); public static void main(String
} args[]){
class TestInterface4 implements Showable{ TestInterface4 obj = new
public void print1(){ TestInterface4();
System.out.println("Hello"); obj.print1();
}
obj.show();
}}

You might also like