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

More Related Content

What's hot (20)

Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
RAGAVIC2
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Access modifier and inheritance
Access modifier and inheritanceAccess modifier and inheritance
Access modifier and inheritance
Dikshyanta Dhungana
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Inheritance
InheritanceInheritance
Inheritance
Sapna Sharma
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
AKANSH SINGHAL
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
Hitesh Kumar
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
interface in c#
interface in c#interface in c#
interface in c#
Deepti Pillai
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 

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
 
INTERFACES. with machine learning and data
INTERFACES. with machine learning and dataINTERFACES. with machine learning and data
INTERFACES. with machine learning and data
dineshkesav07
 
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
 
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMARINTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
Sarathkumar Narsupalli
 
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
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
Kalai Selvi
 
INTERFACES. with machine learning and data
INTERFACES. with machine learning and dataINTERFACES. with machine learning and data
INTERFACES. with machine learning and data
dineshkesav07
 
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
 
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMARINTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
INTERFACES IN JAVA PROGRAMMING BY N SARATH KUMAR
Sarathkumar Narsupalli
 
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
 
Ad

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
 
Ad

Recently uploaded (20)

Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New VersionPixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
saimabibi60507
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New VersionPixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
saimabibi60507
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 

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