SlideShare a Scribd company logo
2
Most read
4
Most read
16
Most read
INTERFACES IN JAVA
INTERFACES
• An interface declares (describes) methods but does not supply bodies for them
• All the methods are implicitly public and abstract
• You can add these qualifiers if you like, but why bother?
• You cannot instantiate an interface
• An interface is like a very abstract class—none of its methods are defined
• An interface may also contain constants (final variables)
DESIGNING INTERFACES
• An interface is created with the following syntax
modifier interface interfaceID
{
//constants/method signatures
}
INTERFACES (CONT)
• An interface can extend other interfaces with the following syntax:
modifier interface interfaceID extends comma-delimited-list-of-
interfaces
{
//constants/method signatures
}
• Obviously, any class which implements a “sub-interface” will have to implement
each of the methods contained in it’s “super-interfaces”
IMPLEMENTING AN INTERFACE
• You extend a class, but you implement an interface
• A class can only extend (subclass) one other class, but it can implement as many
interfaces as you like
• Example:
class MyListener
implements KeyListener, ActionListener { … }
• When you say a class implements an interface, you are promising to define all the
methods that were declared in the interface
PARTIALLY IMPLEMENTING AN INTERFACE
• It is possible to define some but not all of the methods defined in an interface:
abstract class MyKeyListener implements KeyListener
{
public void keyTyped(KeyEvent e) {...};
}
• Since this class does not supply all the methods it has promised, it is an abstract
class
• You must label it as such with the keyword abstract
• You can even extend an interface (to add methods):
• interface FunkyKeyListener extends KeyListener { ... }
WHAT ARE INTERFACES FOR?
• Reason 1: A class can only extend one other class, but it can implement multiple
interfaces
• This lets the class fill multiple “roles”
• In writing Applets, it is common to have one class implement several different listeners
• Example:
class MyApplet extends Applet
implements ActionListener, KeyListener {
...
}
• Reason 2: You can write methods that work for more than one kind of class
IMPLEMENTING INTERFACES
• interface area //interface defined
{
final static float pi=3.14F;
float compute(float x, float y);
}
class rect implements area // interface implemented
{
public float compute(float x, float y)
{
return(x * y);
}
}
IMPLEMENTING MULTIPLE INHERITANCE
import java.io.*;
class student
{
String name="SACHIN";
String dept="MCA";
int rollno;
void getnumber(int n)
{
rollno=n;
}
void display()
{
System.out.println("STUDENT DATABASE USING MULTIPLE INHERITANCE AND INTERFACE CONCEPTS");
System.out.println("Student Name :"+name);
System.out.println("Rollno :"+rollno);
System.out.println("Department :"+dept);
}
}
class test extends student
{
int m1,m2,m3;
void getmarks(int a,int b,int c)
{ m1=a;
m2=b;
m3=c;
}
void displaymarks()
{ System.out.println("Marks Obtained");
System.out.println("Subject1 :="+m1);
System.out.println("Subject2 :="+m2);
System.out.println("Subject3 :="+m3);
} }
interface sports
{ float sportmarks=7.0F;
void dispsport();
}
class results extends test implements sports
{
float total,avg;
public void dispsport()
{
System.out.println("Sport Marks :="+sportmarks);
}
void displaydetails()
{
total=m1+m2+m3;
avg=total/3;
display();
displaymarks();
dispsport();
System.out.println("Total marks Secured:="+total);
System.out.println("Average :="+avg);
}
class multiple
{
public static void main(String a[])
{
results r1=new results();
r1.getnumber(4006);
r1.getmarks(80,90,95);
r1.displaydetails();
}
}
JAVA NESTED INTERFACE
• An interface i.e. declared within another interface or class is known as nested
interface.
• The nested interfaces are used to group related interfaces so that they can be easy to
maintain.
• The nested interface must be referred by the outer interface or class. It can't be
accessed directly.
• Nested interface must be public if it is declared inside the interface but it can have
any access modifier if declared within the class.
• Nested interfaces are declared static implicitely.
JAVA NESTED INTERFACE
• Syntax of nested interface which is declared within the interface
• interface interface_name
{
...
interface nested_interface_name
{
...
}
}
JAVA NESTED INTERFACE
• Syntax of nested interface which is declared within the class
• class class_name{
...
interface nested_interface_name{
...
}
}
EXAMPLE
interface Showable
{
void show();
interface Message {
void msg();
}
}
class Test implements Showable.Message
{
public void msg()
{System.out.println("Hello nested interface");}
public static void main(String args[])
{
Showable.Message message=new Test(); //upcasting here
message.msg();
}
}

More Related Content

PPT
Java interfaces
PPT
Abstract class in java
PPTX
Java abstract class & abstract methods
PPT
Exception Handling in JAVA
PPSX
Exception Handling
PPTX
WHAT IS ABSTRACTION IN JAVA
PDF
Java variable types
PPT
Final keyword in java
Java interfaces
Abstract class in java
Java abstract class & abstract methods
Exception Handling in JAVA
Exception Handling
WHAT IS ABSTRACTION IN JAVA
Java variable types
Final keyword in java

What's hot (20)

PPTX
Java interface
PPTX
Constructor in java
PPTX
Static keyword ppt
PPTX
Object oriented programming
PPTX
Java Data Types
PPSX
Data Types & Variables in JAVA
PPTX
Interface in java
PDF
Methods in Java
PPTX
Inner classes in java
PPTX
Type casting in java
PPTX
I/O Streams
PPT
Interface in java By Dheeraj Kumar Singh
PPS
Introduction to class in java
PDF
Arrays in Java
PPT
Class 5 - PHP Strings
PPS
Interface
PPTX
Abstract Class & Abstract Method in Core Java
PDF
input/ output in java
PPTX
Virtual base class
Java interface
Constructor in java
Static keyword ppt
Object oriented programming
Java Data Types
Data Types & Variables in JAVA
Interface in java
Methods in Java
Inner classes in java
Type casting in java
I/O Streams
Interface in java By Dheeraj Kumar Singh
Introduction to class in java
Arrays in Java
Class 5 - PHP Strings
Interface
Abstract Class & Abstract Method in Core Java
input/ output in java
Virtual base class
Ad

Similar to Interfaces in java (20)

PPT
Java interface
PPTX
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
PDF
Basic_Java_10.pdf
PPTX
Javainterface
PPT
Interfaces
PPT
Chapter 9 Interface
PDF
Session 6_Java Interfaces_Details_Programs.pdf
PPT
Session 6_Interfaces in va examples .ppt
PPTX
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
PPTX
Interfaces in java
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PDF
javainterface
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
06 abstract-classes
PPT
Java Programming - Abstract Class and Interface
PPTX
INTERFACES. with machine learning and data
PPTX
it is the quick gest about the interfaces in java
PPTX
Java presentation
PPT
Multiple interfaces 9 cm604.31
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
Java interface
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
Basic_Java_10.pdf
Javainterface
Interfaces
Chapter 9 Interface
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Interfaces in va examples .ppt
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
Interfaces in java
ABSTRACT CLASSES AND INTERFACES.ppt
javainterface
Interface in java ,multiple inheritance in java, interface implementation
06 abstract-classes
Java Programming - Abstract Class and Interface
INTERFACES. with machine learning and data
it is the quick gest about the interfaces in java
Java presentation
Multiple interfaces 9 cm604.31
OOPS_Unit2.inheritance and interface objected oriented programming
Ad

More from Abishek Purushothaman (8)

PPTX
Aws solution architect
PPTX
Machine learning
DOCX
Multiple choice questions for Java io,files and inheritance
DOCX
Multiple Choice Questions for Java interfaces and exception handling
PPTX
Introduction to R for beginners
PPTX
Mini Project presentation for MCA
PPTX
Python Programming Basics for begginners
PPT
Exception handling
Aws solution architect
Machine learning
Multiple choice questions for Java io,files and inheritance
Multiple Choice Questions for Java interfaces and exception handling
Introduction to R for beginners
Mini Project presentation for MCA
Python Programming Basics for begginners
Exception handling

Recently uploaded (20)

PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
AI in Product Development-omnex systems
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
System and Network Administration Chapter 2
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
Upgrade and Innovation Strategies for SAP ERP Customers
wealthsignaloriginal-com-DS-text-... (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
2025 Textile ERP Trends: SAP, Odoo & Oracle
AI in Product Development-omnex systems
Operating system designcfffgfgggggggvggggggggg
Odoo POS Development Services by CandidRoot Solutions
VVF-Customer-Presentation2025-Ver1.9.pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Navsoft: AI-Powered Business Solutions & Custom Software Development
System and Network Administration Chapter 2
CHAPTER 2 - PM Management and IT Context
Design an Analysis of Algorithms II-SECS-1021-03

Interfaces in java

  • 2. INTERFACES • An interface declares (describes) methods but does not supply bodies for them • All the methods are implicitly public and abstract • You can add these qualifiers if you like, but why bother? • You cannot instantiate an interface • An interface is like a very abstract class—none of its methods are defined • An interface may also contain constants (final variables)
  • 3. DESIGNING INTERFACES • An interface is created with the following syntax modifier interface interfaceID { //constants/method signatures }
  • 4. INTERFACES (CONT) • An interface can extend other interfaces with the following syntax: modifier interface interfaceID extends comma-delimited-list-of- interfaces { //constants/method signatures } • Obviously, any class which implements a “sub-interface” will have to implement each of the methods contained in it’s “super-interfaces”
  • 5. IMPLEMENTING AN INTERFACE • You extend a class, but you implement an interface • A class can only extend (subclass) one other class, but it can implement as many interfaces as you like • Example: class MyListener implements KeyListener, ActionListener { … } • When you say a class implements an interface, you are promising to define all the methods that were declared in the interface
  • 6. PARTIALLY IMPLEMENTING AN INTERFACE • It is possible to define some but not all of the methods defined in an interface: abstract class MyKeyListener implements KeyListener { public void keyTyped(KeyEvent e) {...}; } • Since this class does not supply all the methods it has promised, it is an abstract class • You must label it as such with the keyword abstract • You can even extend an interface (to add methods): • interface FunkyKeyListener extends KeyListener { ... }
  • 7. WHAT ARE INTERFACES FOR? • Reason 1: A class can only extend one other class, but it can implement multiple interfaces • This lets the class fill multiple “roles” • In writing Applets, it is common to have one class implement several different listeners • Example: class MyApplet extends Applet implements ActionListener, KeyListener { ... } • Reason 2: You can write methods that work for more than one kind of class
  • 8. IMPLEMENTING INTERFACES • interface area //interface defined { final static float pi=3.14F; float compute(float x, float y); } class rect implements area // interface implemented { public float compute(float x, float y) { return(x * y); } }
  • 9. IMPLEMENTING MULTIPLE INHERITANCE import java.io.*; class student { String name="SACHIN"; String dept="MCA"; int rollno; void getnumber(int n) { rollno=n; } void display() { System.out.println("STUDENT DATABASE USING MULTIPLE INHERITANCE AND INTERFACE CONCEPTS"); System.out.println("Student Name :"+name); System.out.println("Rollno :"+rollno); System.out.println("Department :"+dept); } }
  • 10. class test extends student { int m1,m2,m3; void getmarks(int a,int b,int c) { m1=a; m2=b; m3=c; } void displaymarks() { System.out.println("Marks Obtained"); System.out.println("Subject1 :="+m1); System.out.println("Subject2 :="+m2); System.out.println("Subject3 :="+m3); } } interface sports { float sportmarks=7.0F; void dispsport(); }
  • 11. class results extends test implements sports { float total,avg; public void dispsport() { System.out.println("Sport Marks :="+sportmarks); } void displaydetails() { total=m1+m2+m3; avg=total/3; display(); displaymarks(); dispsport(); System.out.println("Total marks Secured:="+total); System.out.println("Average :="+avg); }
  • 12. class multiple { public static void main(String a[]) { results r1=new results(); r1.getnumber(4006); r1.getmarks(80,90,95); r1.displaydetails(); } }
  • 13. JAVA NESTED INTERFACE • An interface i.e. declared within another interface or class is known as nested interface. • The nested interfaces are used to group related interfaces so that they can be easy to maintain. • The nested interface must be referred by the outer interface or class. It can't be accessed directly. • Nested interface must be public if it is declared inside the interface but it can have any access modifier if declared within the class. • Nested interfaces are declared static implicitely.
  • 14. JAVA NESTED INTERFACE • Syntax of nested interface which is declared within the interface • interface interface_name { ... interface nested_interface_name { ... } }
  • 15. JAVA NESTED INTERFACE • Syntax of nested interface which is declared within the class • class class_name{ ... interface nested_interface_name{ ... } }
  • 16. EXAMPLE interface Showable { void show(); interface Message { void msg(); } } class Test implements Showable.Message { public void msg() {System.out.println("Hello nested interface");} public static void main(String args[]) { Showable.Message message=new Test(); //upcasting here message.msg(); } }