SlideShare a Scribd company logo
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

What's hot (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Java interface
Java interfaceJava interface
Java interface
BHUVIJAYAVELU
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Access Modifier.pptx
Access Modifier.pptxAccess Modifier.pptx
Access Modifier.pptx
Margaret Mary
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Jussi Pohjolainen
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Access Modifier.pptx
Access Modifier.pptxAccess Modifier.pptx
Access Modifier.pptx
Margaret Mary
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 

Similar to Interfaces in java (20)

Interface &packages
Interface &packagesInterface &packages
Interface &packages
Shah Ishtiyaq Mehfooze
 
Inheritance
InheritanceInheritance
Inheritance
abhay singh
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
Kalai Selvi
 
it is the quick gest about the interfaces in java
it is the quick gest about the interfaces in javait is the quick gest about the interfaces in java
it is the quick gest about the interfaces in java
arunkumarg271
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Interface
InterfaceInterface
Interface
Muthiah Abbhirami
 
Interface
InterfaceInterface
Interface
Shantilal Bhayal
 
Java Interface
Java InterfaceJava Interface
Java Interface
Manish Tiwari
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
PRIYACHAURASIYA25
 
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
ssuser7fe189
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Java interface
Java interfaceJava interface
Java interface
GaneshKumarKanthiah
 
Interface
InterfaceInterface
Interface
vvpadhu
 
Abstraction
AbstractionAbstraction
Abstraction
zindadili
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
Kalai Selvi
 
it is the quick gest about the interfaces in java
it is the quick gest about the interfaces in javait is the quick gest about the interfaces in java
it is the quick gest about the interfaces in java
arunkumarg271
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
jehan1987
 
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
ssuser7fe189
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Interface
InterfaceInterface
Interface
vvpadhu
 

More from Abishek Purushothaman (8)

Aws solution architect
Aws solution architectAws solution architect
Aws solution architect
Abishek Purushothaman
 
Machine learning
Machine learningMachine learning
Machine learning
Abishek Purushothaman
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritance
Abishek Purushothaman
 
Multiple Choice Questions for Java interfaces and exception handling
Multiple Choice Questions for Java interfaces and exception handlingMultiple Choice Questions for Java interfaces and exception handling
Multiple Choice Questions for Java interfaces and exception handling
Abishek Purushothaman
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
Abishek Purushothaman
 
Mini Project presentation for MCA
Mini Project presentation for MCAMini Project presentation for MCA
Mini Project presentation for MCA
Abishek Purushothaman
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
Abishek Purushothaman
 
Exception handling
Exception handlingException handling
Exception handling
Abishek Purushothaman
 

Recently uploaded (20)

AI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI AssistansAI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI Assistans
HusseinMalikMammadli
 
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Yann-Gaël Guéhéneuc
 
Wondershare Filmora Crack Free Download
Wondershare Filmora  Crack Free DownloadWondershare Filmora  Crack Free Download
Wondershare Filmora Crack Free Download
zqeevcqb3t
 
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen EngineRise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
stevebrudz1
 
DevOpsDays LA - Platform Engineers are Product Managers.pdf
DevOpsDays LA - Platform Engineers are Product Managers.pdfDevOpsDays LA - Platform Engineers are Product Managers.pdf
DevOpsDays LA - Platform Engineers are Product Managers.pdf
Justin Reock
 
A Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman BhaumikA Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman Bhaumik
Raman Bhaumik
 
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?
Ava Isley
 
Wondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free DownloadWondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free Download
arshadkhokher01
 
Advance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management OdooAdvance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management Odoo
Aagam infotech
 
SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?
kiran10101khan
 
How John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talkHow John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talk
Nacho Cougil
 
LLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protectedLLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protected
Ivo Andreev
 
Enscape Latest 2025 Crack Free Download
Enscape Latest 2025  Crack Free DownloadEnscape Latest 2025  Crack Free Download
Enscape Latest 2025 Crack Free Download
rnzu5cxw0y
 
Data Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - WebinarData Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - Webinar
OnePlan Solutions
 
AutoDesk Revit Crack | Revit Update 2025 free download
AutoDesk Revit Crack | Revit Update 2025 free downloadAutoDesk Revit Crack | Revit Update 2025 free download
AutoDesk Revit Crack | Revit Update 2025 free download
anamaslam971
 
Instagram Feed Snippet, Instagram posts display in odoo website
Instagram Feed Snippet, Instagram posts display in odoo websiteInstagram Feed Snippet, Instagram posts display in odoo website
Instagram Feed Snippet, Instagram posts display in odoo website
AxisTechnolabs
 
EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]
bhagasufyan
 
Online Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi NcrOnline Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi Ncr
Home
 
Douwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-LatestDouwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-Latest
mubeen010khan
 
LDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free DownloadLDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free Download
5ls1bnl9iv
 
AI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI AssistansAI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI Assistans
HusseinMalikMammadli
 
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Yann-Gaël Guéhéneuc
 
Wondershare Filmora Crack Free Download
Wondershare Filmora  Crack Free DownloadWondershare Filmora  Crack Free Download
Wondershare Filmora Crack Free Download
zqeevcqb3t
 
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen EngineRise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Engine
stevebrudz1
 
DevOpsDays LA - Platform Engineers are Product Managers.pdf
DevOpsDays LA - Platform Engineers are Product Managers.pdfDevOpsDays LA - Platform Engineers are Product Managers.pdf
DevOpsDays LA - Platform Engineers are Product Managers.pdf
Justin Reock
 
A Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman BhaumikA Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman Bhaumik
Raman Bhaumik
 
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?
Ava Isley
 
Wondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free DownloadWondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free Download
arshadkhokher01
 
Advance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management OdooAdvance Website Helpdesk Customer Support Ticket Management Odoo
Advance Website Helpdesk Customer Support Ticket Management Odoo
Aagam infotech
 
SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?
kiran10101khan
 
How John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talkHow John started to like TDD (instead of hating it) - TED talk
How John started to like TDD (instead of hating it) - TED talk
Nacho Cougil
 
LLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protectedLLM Security - Smart to protect, but too smart to be protected
LLM Security - Smart to protect, but too smart to be protected
Ivo Andreev
 
Enscape Latest 2025 Crack Free Download
Enscape Latest 2025  Crack Free DownloadEnscape Latest 2025  Crack Free Download
Enscape Latest 2025 Crack Free Download
rnzu5cxw0y
 
Data Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - WebinarData Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - Webinar
OnePlan Solutions
 
AutoDesk Revit Crack | Revit Update 2025 free download
AutoDesk Revit Crack | Revit Update 2025 free downloadAutoDesk Revit Crack | Revit Update 2025 free download
AutoDesk Revit Crack | Revit Update 2025 free download
anamaslam971
 
Instagram Feed Snippet, Instagram posts display in odoo website
Instagram Feed Snippet, Instagram posts display in odoo websiteInstagram Feed Snippet, Instagram posts display in odoo website
Instagram Feed Snippet, Instagram posts display in odoo website
AxisTechnolabs
 
EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]
bhagasufyan
 
Online Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi NcrOnline Software Testing Training Institute in Delhi Ncr
Online Software Testing Training Institute in Delhi Ncr
Home
 
Douwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-LatestDouwan Preactivated Plus Crack 2025-Latest
Douwan Preactivated Plus Crack 2025-Latest
mubeen010khan
 
LDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free DownloadLDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free Download
5ls1bnl9iv
 

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(); } }