SlideShare a Scribd company logo
INTERFACE
An interface is a list of constants and method
headers. The methods are not implemented in the
interface.
A class that implements an interface must
implement each of the methods listed in the
interface.
Defining an Interface
the general form of an interface
access interface 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;
}
Example
interface MyInterface
{
//All the methods are public abstract by default
public void method1();
public void method2();
}
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 of a class that includes the
implements clause looks like this:
class classname [extends superclass] [implements
interface [,interface...]]
{
// class-body
}
Example
interface MyInterface
{
public void method1();
public void method2();
}
class XYZ implements MyInterface
{
public void method1()
{ System.out.println("implementation of method1");
}
public void method2()
{ System.out.println("implementation of method2");
}
public static void main(String arg[])
{
XYZ obj = new XYZ();
obj. method1();
obj. method2();
}
}
Accessing Implementations Through Interface References
interface MyInterface
{
public void method1();
public void method2();
}
class XYZ implements MyInterface
{
public void method1()
{ System.out.println("implementation of method1");
}
public void method2()
{ System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new XYZ();
obj. method1();
obj. method2();
}
}
Variables in Interface
interface doesn't allow you to declare any instance variables.
You can declare a constant variable, using static final.
By default these variables are public.
Example
interface MyInterface
{
int aConstant = 32; // a constant (public static final, by default)
double pi = 3.14159; // a constant (public static final, by default)
void methodA( int x ); // a method header (public, by default)
double methodB(); // a method header (public, by default)
}
interface SomeInterface
{
public final int x = 32;
public double y;//
public double addup( );
}
Inspect the interface. Is it correct?
Interfaces can be extended
One interface can inherit another by use of the keyword extends.
The syntax is the same as for inheriting classes.
Example
interface A
{
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A
{
void meth3();
}
When a class implements an interface that inherits another interface, it must provide implementations for all methods.
Example
interface A
{
void meth1();
void meth2();
}
interface B extends A
{
void meth3();
}
// This class must implement all of A and B
class MyClass implements B
{
public void meth1()
{
System.out.println("Implement meth1().");
}
public void meth2()
{ System.out.println("Implement meth2().");
}
public void meth3()
{
System.out.println("Implement meth3().");
}
}
class InterfaceExtend
{
public static void main(String arg[])
{
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
Ad

Recommended

Interfaces
Interfaces
RBIEBT,MOHALI
 
14 interface
14 interface
Ravindra Rathore
 
Java interface
Java interface
GaneshKumarKanthiah
 
Javainterface
Javainterface
prakash GB
 
Java interface
Java interface
BHUVIJAYAVELU
 
Interface
Interface
Muthiah Abbhirami
 
FINAL_DAY10_INTERFACES_roles and benefits.pptx
FINAL_DAY10_INTERFACES_roles and benefits.pptx
VGaneshKarthikeyan
 
Interface
Interface
Shantilal Bhayal
 
Java interfaces
Java interfaces
Raja Sekhar
 
Lecture 5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
Interface
Interface
vvpadhu
 
Interface
Interface
kamal kotecha
 
Java Interface
Java Interface
Manish Tiwari
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
 
Java/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Interface in java
Interface in java
Kavitha713564
 
Exception handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Java interface
Java interface
Arati Gadgil
 
Interfaces
Interfaces
Jai Marathe
 
13. interfaces
13. interfaces
M H Buddhika Ariyaratne
 
Interfaces in java
Interfaces in java
Shiv Mehmi
 
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
Sarathkumar Narsupalli
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Interfaces-in-Java.for engineering students pptx
Interfaces-in-Java.for engineering students pptx
waarrior1234567
 
Java interfaces
Java interfaces
jehan1987
 
Java interface
Java interface
Md. Tanvir Hossain
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
EventHandling in object oriented programming
EventHandling in object oriented programming
Parameshwar Maddela
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 

More Related Content

Similar to working with interfaces in java programming (20)

Java interfaces
Java interfaces
Raja Sekhar
 
Lecture 5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
Interface
Interface
vvpadhu
 
Interface
Interface
kamal kotecha
 
Java Interface
Java Interface
Manish Tiwari
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
 
Java/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Interface in java
Interface in java
Kavitha713564
 
Exception handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Java interface
Java interface
Arati Gadgil
 
Interfaces
Interfaces
Jai Marathe
 
13. interfaces
13. interfaces
M H Buddhika Ariyaratne
 
Interfaces in java
Interfaces in java
Shiv Mehmi
 
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
Sarathkumar Narsupalli
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Interfaces-in-Java.for engineering students pptx
Interfaces-in-Java.for engineering students pptx
waarrior1234567
 
Java interfaces
Java interfaces
jehan1987
 
Java interface
Java interface
Md. Tanvir Hossain
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
Lecture 5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
Interface
Interface
vvpadhu
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
 
Java/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Exception handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Interfaces in java
Interfaces in java
Shiv Mehmi
 
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
Sarathkumar Narsupalli
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Interfaces-in-Java.for engineering students pptx
Interfaces-in-Java.for engineering students pptx
waarrior1234567
 
Java interfaces
Java interfaces
jehan1987
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 

More from Parameshwar Maddela (11)

EventHandling in object oriented programming
EventHandling in object oriented programming
Parameshwar Maddela
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
introduction to object orinted programming through java
introduction to object orinted programming through java
Parameshwar Maddela
 
multhi threading concept in oops through java
multhi threading concept in oops through java
Parameshwar Maddela
 
Object oriented programming -QuestionBank
Object oriented programming -QuestionBank
Parameshwar Maddela
 
file handling in object oriented programming through java
file handling in object oriented programming through java
Parameshwar Maddela
 
22H51A6755.pptx
22H51A6755.pptx
Parameshwar Maddela
 
swings.pptx
swings.pptx
Parameshwar Maddela
 
03_Objects and Classes in java.pdf
03_Objects and Classes in java.pdf
Parameshwar Maddela
 
02_Data Types in java.pdf
02_Data Types in java.pdf
Parameshwar Maddela
 
Intro tooop
Intro tooop
Parameshwar Maddela
 
EventHandling in object oriented programming
EventHandling in object oriented programming
Parameshwar Maddela
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
introduction to object orinted programming through java
introduction to object orinted programming through java
Parameshwar Maddela
 
multhi threading concept in oops through java
multhi threading concept in oops through java
Parameshwar Maddela
 
Object oriented programming -QuestionBank
Object oriented programming -QuestionBank
Parameshwar Maddela
 
file handling in object oriented programming through java
file handling in object oriented programming through java
Parameshwar Maddela
 
03_Objects and Classes in java.pdf
03_Objects and Classes in java.pdf
Parameshwar Maddela
 
Ad

Recently uploaded (20)

Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Ad

working with interfaces in java programming

  • 1. INTERFACE An interface is a list of constants and method headers. The methods are not implemented in the interface. A class that implements an interface must implement each of the methods listed in the interface.
  • 2. Defining an Interface the general form of an interface access interface 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; }
  • 3. Example interface MyInterface { //All the methods are public abstract by default public void method1(); public void method2(); }
  • 4. 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 of a class that includes the implements clause looks like this: class classname [extends superclass] [implements interface [,interface...]] { // class-body }
  • 5. Example interface MyInterface { public void method1(); public void method2(); } class XYZ implements MyInterface { public void method1() { System.out.println("implementation of method1"); } public void method2() { System.out.println("implementation of method2"); } public static void main(String arg[]) { XYZ obj = new XYZ(); obj. method1(); obj. method2(); } }
  • 6. Accessing Implementations Through Interface References interface MyInterface { public void method1(); public void method2(); } class XYZ implements MyInterface { public void method1() { System.out.println("implementation of method1"); } public void method2() { System.out.println("implementation of method2"); } public static void main(String arg[]) { MyInterface obj = new XYZ(); obj. method1(); obj. method2(); } }
  • 7. Variables in Interface interface doesn't allow you to declare any instance variables. You can declare a constant variable, using static final. By default these variables are public. Example interface MyInterface { int aConstant = 32; // a constant (public static final, by default) double pi = 3.14159; // a constant (public static final, by default) void methodA( int x ); // a method header (public, by default) double methodB(); // a method header (public, by default) }
  • 8. interface SomeInterface { public final int x = 32; public double y;// public double addup( ); } Inspect the interface. Is it correct?
  • 9. Interfaces can be extended One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. Example interface A { void meth1(); void meth2(); } // B now includes meth1() and meth2() -- it adds meth3(). interface B extends A { void meth3(); }
  • 10. When a class implements an interface that inherits another interface, it must provide implementations for all methods. Example interface A { void meth1(); void meth2(); } interface B extends A { void meth3(); } // This class must implement all of A and B class MyClass implements B { public void meth1() { System.out.println("Implement meth1()."); } public void meth2() { System.out.println("Implement meth2()."); } public void meth3() { System.out.println("Implement meth3()."); } }
  • 11. class InterfaceExtend { public static void main(String arg[]) { MyClass ob = new MyClass(); ob.meth1(); ob.meth2(); ob.meth3(); } }