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

What's hot (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java IO
Java IOJava IO
Java IO
 
Basic of Java
Basic of JavaBasic of Java
Basic of Java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Files in java
Files in javaFiles in java
Files in java
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Exception handling
Exception handlingException handling
Exception handling
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 

Similar to Interfaces in java

Similar to Interfaces in java (20)

Interface &packages
Interface &packagesInterface &packages
Interface &packages
 
Inheritance
InheritanceInheritance
Inheritance
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
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
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
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
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Interface
InterfaceInterface
Interface
 
Interface
InterfaceInterface
Interface
 
Java Interface
Java InterfaceJava Interface
Java Interface
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
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...
 
Java interface
Java interfaceJava interface
Java interface
 
Interface
InterfaceInterface
Interface
 
Abstraction
AbstractionAbstraction
Abstraction
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
14 interface
14  interface14  interface
14 interface
 

More from Abishek Purushothaman

More from Abishek Purushothaman (8)

Aws solution architect
Aws solution architectAws solution architect
Aws solution architect
 
Machine learning
Machine learningMachine learning
Machine learning
 
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
 
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
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
Mini Project presentation for MCA
Mini Project presentation for MCAMini Project presentation for MCA
Mini Project presentation for MCA
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
Exception handling
Exception handlingException handling
Exception handling
 

Recently uploaded

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 

Recently uploaded (20)

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 

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