SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Interface in Java
-Arulkumar V
Assistant Professor, SECE
Interface in Java
 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.
 Java Interface also represents IS-A
relationship.
Why use Java interface?
 There are mainly three reasons to
use interface. They are given below.
 It is used to achieve abstraction.
 By interface, we can support the
functionality of multiple inheritance.
 It can be used to achieve loose
coupling.
Understand Interface
Example of Interface
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Printable interface has only one method, its
implementation is provided in the A class.
Example Program:
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawi
ng rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawi
ng circle");} }
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, obj
ect is provided by method e.g. getDrawable()
d.draw();
}}
Cont.,
Bank example
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
Cont.,
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInter
est());
}}
Output:
ROI: 9.15
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an
interface extends multiple interfaces i.e. known as
multiple inheritance.
Multiple inheritance example
interface Printable{
void print();
}
interface Showable{
void show(); }
class A7 implements Printable,Showable
{
public void print(){System.out.println("Hello");
}
public void show()
{System.out.println("Welcome");}
public static void main(String args[])
{
A7 obj = new A7();
obj.print();
obj.show(); } }
Output: Hello
Welcome
Interface inheritance
A class implements interface but one interface extends another
interface .
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
} Hello
Welcome
Thank You
19-12-2017 Sampath Kumar.S, AP/IT 13

More Related Content

PPTX
oops concept in java | object oriented programming in java
PPTX
Inheritance in java
PPTX
Java package
PPT
Java interfaces
DOCX
project on Ecosystem(EVS)
PPT
Java Presentation
PPTX
RURAL HEALTH SERVICES (1).pptx
PPTX
Arrays in Java
oops concept in java | object oriented programming in java
Inheritance in java
Java package
Java interfaces
project on Ecosystem(EVS)
Java Presentation
RURAL HEALTH SERVICES (1).pptx
Arrays in Java

What's hot (20)

PPT
Final keyword in java
PPTX
I/O Streams
PPTX
Inheritance in java
PPTX
Packages in java
PPTX
Constructor in java
PPTX
Features of java
PPTX
Methods in java
PPTX
Multithreading in java
PPT
Function overloading(c++)
PPS
Wrapper class
PPTX
Applets in java
PPT
Abstract class
PPTX
JAVA AWT
PPT
Abstract class in java
PPTX
Control structures in java
PPT
Java-java virtual machine
PDF
Methods in Java
PPTX
Java exception handling
PPTX
Java string handling
PPTX
OOPS In JAVA.pptx
Final keyword in java
I/O Streams
Inheritance in java
Packages in java
Constructor in java
Features of java
Methods in java
Multithreading in java
Function overloading(c++)
Wrapper class
Applets in java
Abstract class
JAVA AWT
Abstract class in java
Control structures in java
Java-java virtual machine
Methods in Java
Java exception handling
Java string handling
OOPS In JAVA.pptx
Ad

Similar to Interface in java (20)

PDF
Basic_Java_10.pdf
PPTX
OOP with Java - Abstract Classes and Interfaces
PPTX
Interface in java
PPTX
abstract,final,interface (1).pptx upload
PDF
Exception handling and packages.pdf
PPTX
JAVA.pptx
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
oops with java modules i & ii.ppt
DOCX
Java interface
PPTX
Session 10 - OOP with Java - Abstract Classes and Interfaces
PPSX
OOP with Java - Abstract Classes and Interfaces
PPTX
Java notes of Chapter 3 presentation slides
PDF
Core Java Interface Concepts for BCA Studetns
PPT
Session 6_Interfaces in va examples .ppt
PPT
Session 6_Interfaces in va examples .ppt
PDF
Session 6_Java Interfaces_Details_Programs.pdf
PPT
Java interface
PPS
Interface
PPT
Basic_Java_10.pdf
OOP with Java - Abstract Classes and Interfaces
Interface in java
abstract,final,interface (1).pptx upload
Exception handling and packages.pdf
JAVA.pptx
21UCAC31 Java Programming.pdf(MTNC)(BCA)
Interface in java ,multiple inheritance in java, interface implementation
oops with java modules i & ii.ppt
Java interface
Session 10 - OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
Java notes of Chapter 3 presentation slides
Core Java Interface Concepts for BCA Studetns
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
Session 6_Java Interfaces_Details_Programs.pdf
Java interface
Interface
Ad

More from PhD Research Scholar (20)

PPTX
Quiz servlet
PPTX
servlet db connectivity
PPTX
2.java script dom
PPTX
1.java script
PPTX
Quiz javascript
PPTX
Thread&multithread
PPTX
Streams&io
PPTX
PPTX
Inner classes in java
PPTX
PPTX
Exception handling
PPTX
Abstract class
PPTX
7. tuples, set & dictionary
PPTX
PPTX
4. functions
Quiz servlet
servlet db connectivity
2.java script dom
1.java script
Quiz javascript
Thread&multithread
Streams&io
Inner classes in java
Exception handling
Abstract class
7. tuples, set & dictionary
4. functions

Recently uploaded (20)

PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Business Ethics Teaching Materials for college
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
English Language Teaching from Post-.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Anesthesia in Laparoscopic Surgery in India
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction and Scope of Bichemistry.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Open Quiz Monsoon Mind Game Prelims.pptx
Insiders guide to clinical Medicine.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Cell Structure & Organelles in detailed.
Business Ethics Teaching Materials for college
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
English Language Teaching from Post-.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Revamp in MTO Odoo 18 Inventory - Odoo Slides
human mycosis Human fungal infections are called human mycosis..pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Interface in java

  • 1. Interface in Java -Arulkumar V Assistant Professor, SECE
  • 2. Interface in Java  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.  Java Interface also represents IS-A relationship.
  • 3. Why use Java interface?  There are mainly three reasons to use interface. They are given below.  It is used to achieve abstraction.  By interface, we can support the functionality of multiple inheritance.  It can be used to achieve loose coupling.
  • 5. Example of Interface interface printable{ void print(); } class A6 implements printable{ public void print(){System.out.println("Hello");} public static void main(String args[]){ A6 obj = new A6(); obj.print(); } } Printable interface has only one method, its implementation is provided in the A class.
  • 6. Example Program: interface Drawable{ void draw(); } //Implementation: by second user class Rectangle implements Drawable{ public void draw(){System.out.println("drawi ng rectangle");} } class Circle implements Drawable{ public void draw(){System.out.println("drawi ng circle");} }
  • 7. //Using interface: by third user class TestInterface1{ public static void main(String args[]){ Drawable d=new Circle();//In real scenario, obj ect is provided by method e.g. getDrawable() d.draw(); }} Cont.,
  • 8. Bank example interface Bank{ float rateOfInterest(); } class SBI implements Bank{ public float rateOfInterest(){return 9.15f;} } class PNB implements Bank{ public float rateOfInterest(){return 9.7f;} }
  • 9. Cont., class TestInterface2{ public static void main(String[] args){ Bank b=new SBI(); System.out.println("ROI: "+b.rateOfInter est()); }} Output: ROI: 9.15
  • 10. Multiple inheritance in Java by interface If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
  • 11. Multiple inheritance example interface Printable{ void print(); } interface Showable{ void show(); } class A7 implements Printable,Showable { public void print(){System.out.println("Hello"); } public void show() {System.out.println("Welcome");} public static void main(String args[]) { A7 obj = new A7(); obj.print(); obj.show(); } } Output: Hello Welcome
  • 12. Interface inheritance A class implements interface but one interface extends another interface . interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class TestInterface4 implements Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ TestInterface4 obj = new TestInterface4(); obj.print(); obj.show(); } } Hello Welcome
  • 13. Thank You 19-12-2017 Sampath Kumar.S, AP/IT 13