SlideShare a Scribd company logo
Interface
• using interface, you can specify what a class
must do, but not how it does it.
• Interfaces are syntactically similar to classes,
but they lack instance variables, and their
methods are declared without any body.
• Through the use of the interface keyword,
Java allows you to fully abstract the interface
from its implementation.
Interface
• Using interface, you can specify a set of
method which can be implemented by one or
more classes.
• The interface, itself, does not actually define
any implementation.
• A class can implement more than one
interface.
• A class can only inherit a single superclass.
Defining an Interface
• The general form :
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
• access is either public or not used.
• When no access specifier is included, then
default access results, and the interface is only
available to other members of the package in
which it is declared.
• When it is declared as public, the interface
can be used by any other code.
• All methods and variables are implicitly public
if the interface, itself, is declared as public.
• Variables can be declared inside of interface
declarations.
• They are implicitly final and static, meaning
they cannot be changed by the implementing
class.
Sample
interface Callback {
void callback(int param);
}
Implementing Interfaces
• To implement an interface, include the
implements clause in a class definition, and
then create the methods defined by the
interface.
• The general form:
access class classname [extends superclass]
[implements interface [,interface...]] {
// class-body
}
Sample
class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that implement interfaces "
+
"may also define other members, too.");
} }
Partial Implementations
• If a class includes an interface but does not
fully implement the methods defined by that
interface, then that class must be declared as
abstract.
Interface sample
// Define an integer stack interface.
interface IntStack {
void push(int item); // store an item
int pop(); // retrieve an item
}
class FixedStack implements IntStack {
private int stck[];
private int tos;
FixedStack(int size) {
stck = new int[size];
tos = -1;
}
public void push(int item) {
if(tos==stck.length-1) // use length member
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
public int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
} }
class IFTest {
public static void main(String args[]) {
FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
// push some numbers onto the stack
for(int i=0; i<5; i++) mystack1.push(i);
for(int i=0; i<8; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
Variables in Interfaces
• To import shared constants into multiple
classes by simply declaring an interface that
contains variables which are initialized to the
desired values.
Sample
import java.util.Random;
interface SharedConstants {
int NO = 0;
int YES = 1;
int MAYBE = 2;
}
class Question implements SharedConstants {
Random rand = new Random();
int ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 50)
return NO;
else if (prob < 80)
return YES;
else
return NEVER;
}
}
class AskMe implements SharedConstants {
static void answer(int result) {
switch(result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes"); break;
case MAYBE:
System.out.println("Maybe");
break;
} }
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
} }
Interfaces Can Be Extended
• One interface can inherit another by use of the
keyword extends.
interface A {
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds
meth3().
interface B extends A {
void meth3(); }

More Related Content

PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
PPTX
Unit3 part1-class
PPTX
Basic concept of class, method , command line-argument
PPTX
Unit 4 exceptions and threads
PPTX
Java interface
DOCX
Keyword of java
PPTX
Unit 5 java-awt (1)
PPTX
Std 12 computer chapter 8 classes and object in java (part 2)
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit3 part1-class
Basic concept of class, method , command line-argument
Unit 4 exceptions and threads
Java interface
Keyword of java
Unit 5 java-awt (1)
Std 12 computer chapter 8 classes and object in java (part 2)

What's hot (20)

PPT
Java interfaces
PPTX
Ppt on this and super keyword
PPT
4. java intro class
PPTX
Hemajava
PDF
Java keywords
PPTX
encapsulation, inheritance, overriding, overloading
PPT
Inheritance
PPTX
Mutable and immutable classes
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
Object oriented programming in java
PPTX
Inheritance and Polymorphism in java simple and clear
PPTX
Method overloading
PPTX
Method overloading and constructor overloading in java
DOCX
Lecture22.23.07.2014
PPTX
PDF
Polymorphism and Software Reuse
PPT
L7 inheritance
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
PPTX
Lecture - 2 Environment setup & JDK, JRE, JVM
Java interfaces
Ppt on this and super keyword
4. java intro class
Hemajava
Java keywords
encapsulation, inheritance, overriding, overloading
Inheritance
Mutable and immutable classes
Abstract Class & Abstract Method in Core Java
Object oriented programming in java
Inheritance and Polymorphism in java simple and clear
Method overloading
Method overloading and constructor overloading in java
Lecture22.23.07.2014
Polymorphism and Software Reuse
L7 inheritance
Java OOP Programming language (Part 6) - Abstract Class & Interface
Lecture - 2 Environment setup & JDK, JRE, JVM
Ad

Similar to Java interface (20)

PDF
14 interface
PPT
Interfaces
PPTX
FINAL_DAY10_INTERFACES_roles and benefits.pptx
PDF
Interface
PPTX
Javainterface
PDF
Lecture 5 interface.pdf
PPTX
Objects and classes in OO Programming concepts
PPTX
Java interface
PPT
Interface in java By Dheeraj Kumar Singh
PPTX
working with interfaces in java programming
PPTX
OOFeatures_revised-2.pptx
PDF
Exception handling and packages.pdf
PPTX
13. interfaces
PPTX
Lecture 18
PPTX
Interface in java
PPTX
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
PPTX
Interface
PDF
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
PPT
oops with java modules i & ii.ppt
PPTX
Interfaces-in-Java.for engineering students pptx
14 interface
Interfaces
FINAL_DAY10_INTERFACES_roles and benefits.pptx
Interface
Javainterface
Lecture 5 interface.pdf
Objects and classes in OO Programming concepts
Java interface
Interface in java By Dheeraj Kumar Singh
working with interfaces in java programming
OOFeatures_revised-2.pptx
Exception handling and packages.pdf
13. interfaces
Lecture 18
Interface in java
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
Interface
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
oops with java modules i & ii.ppt
Interfaces-in-Java.for engineering students pptx
Ad

More from GaneshKumarKanthiah (11)

PPTX
Software testing regression testing
PPTX
Software testing acceptance testing
PPTX
Software testing performance testing
PPTX
Software testing introduction
PPTX
Java exception handling
PPTX
Java string handling
PPTX
Java packages
PPT
Java introduction
PPT
Java inheritance
PPT
PPTX
Java applet
Software testing regression testing
Software testing acceptance testing
Software testing performance testing
Software testing introduction
Java exception handling
Java string handling
Java packages
Java introduction
Java inheritance
Java applet

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Spectroscopy.pptx food analysis technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
Teaching material agriculture food technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Electronic commerce courselecture one. Pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectroscopy.pptx food analysis technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Electronic commerce courselecture one. Pdf
20250228 LYD VKU AI Blended-Learning.pptx

Java interface

  • 2. • using interface, you can specify what a class must do, but not how it does it. • Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. • Through the use of the interface keyword, Java allows you to fully abstract the interface from its implementation.
  • 3. Interface • Using interface, you can specify a set of method which can be implemented by one or more classes. • The interface, itself, does not actually define any implementation. • A class can implement more than one interface. • A class can only inherit a single superclass.
  • 4. Defining an Interface • The general form : access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; // ... return-type method-nameN(parameter-list); type final-varnameN = value; }
  • 5. • access is either public or not used. • When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. • When it is declared as public, the interface can be used by any other code. • All methods and variables are implicitly public if the interface, itself, is declared as public.
  • 6. • Variables can be declared inside of interface declarations. • They are implicitly final and static, meaning they cannot be changed by the implementing class.
  • 7. Sample interface Callback { void callback(int param); }
  • 8. Implementing Interfaces • To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. • The general form: access class classname [extends superclass] [implements interface [,interface...]] { // class-body }
  • 9. Sample class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println("callback called with " + p); } void nonIfaceMeth() { System.out.println("Classes that implement interfaces " + "may also define other members, too."); } }
  • 10. Partial Implementations • If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract.
  • 11. Interface sample // Define an integer stack interface. interface IntStack { void push(int item); // store an item int pop(); // retrieve an item } class FixedStack implements IntStack { private int stck[]; private int tos; FixedStack(int size) { stck = new int[size]; tos = -1; }
  • 12. public void push(int item) { if(tos==stck.length-1) // use length member System.out.println("Stack is full."); else stck[++tos] = item; } // Pop an item from the stack public int pop() { if(tos < 0) { System.out.println("Stack underflow."); return 0; } else return stck[tos--]; } }
  • 13. class IFTest { public static void main(String args[]) { FixedStack mystack1 = new FixedStack(5); FixedStack mystack2 = new FixedStack(8); // push some numbers onto the stack for(int i=0; i<5; i++) mystack1.push(i); for(int i=0; i<8; i++) mystack2.push(i); // pop those numbers off the stack System.out.println("Stack in mystack1:"); for(int i=0; i<5; i++) System.out.println(mystack1.pop()); System.out.println("Stack in mystack2:"); for(int i=0; i<8; i++) System.out.println(mystack2.pop()); }
  • 14. Variables in Interfaces • To import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values.
  • 15. Sample import java.util.Random; interface SharedConstants { int NO = 0; int YES = 1; int MAYBE = 2; }
  • 16. class Question implements SharedConstants { Random rand = new Random(); int ask() { int prob = (int) (100 * rand.nextDouble()); if (prob < 50) return NO; else if (prob < 80) return YES; else return NEVER; } }
  • 17. class AskMe implements SharedConstants { static void answer(int result) { switch(result) { case NO: System.out.println("No"); break; case YES: System.out.println("Yes"); break; case MAYBE: System.out.println("Maybe"); break; } } public static void main(String args[]) { Question q = new Question(); answer(q.ask()); answer(q.ask()); answer(q.ask()); answer(q.ask()); } }
  • 18. Interfaces Can Be Extended • One interface can inherit another by use of the keyword extends. interface A { void meth1(); void meth2(); } // B now includes meth1() and meth2() -- it adds meth3(). interface B extends A { void meth3(); }