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

What's hot (20)

PPTX
Ado.Net Tutorial
prabhu rajendran
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Interfaces in java
Abishek Purushothaman
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Java package
CS_GDRCST
 
PPTX
Java string handling
Salman Khan
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PPTX
Inheritance in java
RahulAnanda1
 
PPT
Java collections concept
kumar gaurav
 
PPS
Interface
kamal kotecha
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PDF
Generics
Ravi_Kant_Sahu
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
This keyword in java
Hitesh Kumar
 
Ado.Net Tutorial
prabhu rajendran
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Arrays in Java
Naz Abdalla
 
Interfaces in java
Abishek Purushothaman
 
Java threads
Prabhakaran V M
 
Inner classes in java
PhD Research Scholar
 
Type casting in java
Farooq Baloch
 
Java package
CS_GDRCST
 
Java string handling
Salman Khan
 
Java I/o streams
Hamid Ghorbani
 
Constructor in java
Pavith Gunasekara
 
Inheritance in java
RahulAnanda1
 
Java collections concept
kumar gaurav
 
Interface
kamal kotecha
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Generics
Ravi_Kant_Sahu
 
Java exception handling
BHUVIJAYAVELU
 
JAVA AWT
shanmuga rajan
 
This keyword in java
Hitesh Kumar
 

Similar to Interface in java (20)

PPTX
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
PDF
Basic_Java_10.pdf
KumarUtsav24
 
PPTX
JAVA.pptx
SohamMathur1
 
DOCX
Java interface
HoneyChintal
 
PPT
Interface in java
Lovely Professional University
 
PPTX
Interface in java
Kavitha713564
 
PPTX
INTERFACES. with machine learning and data
dineshkesav07
 
PPTX
it is the quick gest about the interfaces in java
arunkumarg271
 
PPT
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
abstract,final,interface (1).pptx upload
dashpayal697
 
DOC
How would you implement multiple inheritance in java
Tyagi2636
 
PPTX
inheritance, Packages and Interfaces.pptx
Vanitha Alagesan
 
PPTX
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
officialpriyanshu228
 
PPTX
OOFeatures_revised-2.pptx
ssuser84e52e
 
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
PPTX
Interface
vvpadhu
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PDF
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
VGaneshKarthikeyan
 
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Basic_Java_10.pdf
KumarUtsav24
 
JAVA.pptx
SohamMathur1
 
Java interface
HoneyChintal
 
Interface in java
Kavitha713564
 
INTERFACES. with machine learning and data
dineshkesav07
 
it is the quick gest about the interfaces in java
arunkumarg271
 
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
abstract,final,interface (1).pptx upload
dashpayal697
 
How would you implement multiple inheritance in java
Tyagi2636
 
inheritance, Packages and Interfaces.pptx
Vanitha Alagesan
 
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
officialpriyanshu228
 
OOFeatures_revised-2.pptx
ssuser84e52e
 
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Interface
vvpadhu
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
oops with java modules i & ii.ppt
rani marri
 
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
VGaneshKarthikeyan
 
Ad

More from PhD Research Scholar (20)

PPTX
Quiz servlet
PhD Research Scholar
 
PPTX
servlet db connectivity
PhD Research Scholar
 
PPTX
2.java script dom
PhD Research Scholar
 
PPTX
1.java script
PhD Research Scholar
 
PPTX
Quiz javascript
PhD Research Scholar
 
PPTX
Thread&multithread
PhD Research Scholar
 
PPTX
Streams&io
PhD Research Scholar
 
PPTX
Packages
PhD Research Scholar
 
PPTX
Inheritance
PhD Research Scholar
 
PPTX
Exception handling
PhD Research Scholar
 
PPTX
Abstract class
PhD Research Scholar
 
PPTX
7. tuples, set & dictionary
PhD Research Scholar
 
PPTX
5. string
PhD Research Scholar
 
PPTX
4. functions
PhD Research Scholar
 
PPTX
3.2 looping statement
PhD Research Scholar
 
Quiz servlet
PhD Research Scholar
 
servlet db connectivity
PhD Research Scholar
 
2.java script dom
PhD Research Scholar
 
1.java script
PhD Research Scholar
 
Quiz javascript
PhD Research Scholar
 
Thread&multithread
PhD Research Scholar
 
Streams&io
PhD Research Scholar
 
Exception handling
PhD Research Scholar
 
Abstract class
PhD Research Scholar
 
7. tuples, set & dictionary
PhD Research Scholar
 
4. functions
PhD Research Scholar
 
3.2 looping statement
PhD Research Scholar
 
Ad

Recently uploaded (20)

PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 

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