SlideShare a Scribd company logo
1 of 13
UNIVERSITY COLLEGE OF
COMPUTER
APPLICATIONS
WELCOME YOU ALL
INTERFACES
JAVA INTERFACE
3
◉ A J ava interface is a collection of constants and
abstract methods
◼ abstract method: a method header without a method
body; we declare an abstract method using the
modifier a b s t ra c t
◼ since all methods in an interface are abstract, the
a b s t ra c t modifier is usually left off
◉ Methods in an interface have public visibility by
default
INTERFACE: SYNTAX
interfaceisa reserved word
public interface Doable
{
public static f i n a l String NAME;
public void
doThis();
public i n t doThat();
public void doThis2 ( f l o a t value, char ch);
public boolean doTheOther ( i n t num);
}
A semicolon immediately
followseach method
header
No method in an
interfacehas a definition
4
IMPLEMENTING AN INTERFACE
5
◉ A class formally implements an interface by
◼ stating so in the class header in the implements
clause
◼ a class can implement multiple interfaces: the
interfaces are listed in the implements clause,
separated by commas
◉ If a class asserts that it implements an interface, it
must define all methods in the interface or the
compiler will produce errors
IMPLEMENTING INTERFACES
public class Something implements
Doable
{
public void doThis ( )
{
//
whateve
r
}
public void doThat ( )
{
// whatever
}
implements is
a reserved
word
Each method
listed
in Doable
is
given a
definition
// etc.
}
public c l a s s ManyThings implements Doable,
AnotherDoable 6
INTERFACES: AN EXAMPLE
7
◉ A class that implements an interface can
implement other methods as well
UML DIAGRAM
<<interface>>
Complexity
+ getComplexity () : int
+ setComplexity (int) : void
Question
+ getQuestion () : String
+ getAnswer () : String
+ answerCorrect (String) : boolean
+ toString() : String
MiniQuiz
+ main(args : String[]) : void
1
8
2
INTERFACES: EXAMPLES FROM
JAVA STANDARD CLASS LIBRARY
9
◉ The Java Standard Class library defines many
interfaces:
◼ the I t e r a t o r interface contains methods that
allow the user to move through a collection of
objects easily
□ h a sN ex t (), n e x t ( ) , remove()
◼ the Comparable interface contains an abstract
method called compareTo, which is used to compare
two objects
i f (obj1.compareTo(obj2) < 0)
Syste m . o u t . pr i n t l n (“ o b j 1 i s l e s s than
o b j 2 ” ) ;
POLYMORPHISM VIA INTERFACES
10
◉ Define a polymorphism reference through
interface
◼ declare a reference variable of an interface type
Doable o b j ;
◼ the obj reference can be used to point to any object
of any class that implements the Doable interface
◼ the version of doThis depends on the type of
object that obj is referring to:
o b j . d o T h i s ( ) ;
EXAMPLE: POLYMORPHISM VIA
INTERFACE
11
◉ The payroll program revisited: we want to
sort the employees by name
MORE EXAMPLES
p u b l ic i n t e r f a c e Speaker
{
p u b l ic void sp e a k( ) ;
}
c l a s s Philosopher extends Human
implements Speaker
{
//
p u b l ic void speak()
{…}
p u b l ic void p o n t i f i c a t e ( )
{…}
}
c l a s s Dog extends Animal
implements Speaker
{
//
p u b l ic void speak()
{
…
}
Speaker g u e s t ;
guest = new Ph i l o sop he r( );
g u e s t . s p e a k ( ) ;
guest = Dog();
g u e s t . s p e a k ( ) ;
11
Speaker s p e c i a l ;
s p e c i a l = new Ph i l o so phe r( ) ;
s p e c i a l . p o n t i f i c a t e ( ) ; /
/compilererror
Speaker s p e c i a l ;
s p e c i a l = new Ph i l o so phe r( ) ;
( ( P h i l o s o p h e r ) s p e c i a l ) . p o n t i f i c a t e ( ) ;
INTERFACE HIERARCHIES
◉ Inheritance can be applied to interfaces as well
as classes
◉ One interface can be used as the parent of
another
◉ The child interface inherits all abstract methods
of the parent
◉ A class implementing the child interface must
define all methods from both the parent and
child interfaces
◉ Note that class hierarchies and interface
hierarchies are distinct (they do not overlap)
12

More Related Content

Similar to Interface.pptx

Interface
InterfaceInterface
Interfacevvpadhu
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxBruceLee275640
 
pbo 5 ervan
pbo 5 ervanpbo 5 ervan
pbo 5 ervanaris
 
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.pptVISHNUSHANKARSINGH3
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarPRAVIN GHOLKAR
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxssuser84e52e
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan Chien
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
javase8bestpractices-151015135520-lva1-app6892
javase8bestpractices-151015135520-lva1-app6892javase8bestpractices-151015135520-lva1-app6892
javase8bestpractices-151015135520-lva1-app6892SRINIVAS C
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 

Similar to Interface.pptx (20)

Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Interface
InterfaceInterface
Interface
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
pbo 5 ervan
pbo 5 ervanpbo 5 ervan
pbo 5 ervan
 
Interface
InterfaceInterface
Interface
 
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
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
javainterface
javainterfacejavainterface
javainterface
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
 
Java interface
Java interfaceJava interface
Java interface
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
Interface
InterfaceInterface
Interface
 
Overloading
OverloadingOverloading
Overloading
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
javase8bestpractices-151015135520-lva1-app6892
javase8bestpractices-151015135520-lva1-app6892javase8bestpractices-151015135520-lva1-app6892
javase8bestpractices-151015135520-lva1-app6892
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 

More from sukhpreetsingh295239 (14)

java poly ppt.pptx
java poly ppt.pptxjava poly ppt.pptx
java poly ppt.pptx
 
English Communication skills.ppt
English Communication skills.pptEnglish Communication skills.ppt
English Communication skills.ppt
 
CLOUD SECURITY.pptx
CLOUD SECURITY.pptxCLOUD SECURITY.pptx
CLOUD SECURITY.pptx
 
CCSK.pptx
CCSK.pptxCCSK.pptx
CCSK.pptx
 
MEMORY.pptx
MEMORY.pptxMEMORY.pptx
MEMORY.pptx
 
mms ppt 1.ppt
mms ppt 1.pptmms ppt 1.ppt
mms ppt 1.ppt
 
MMS PPT 2.pptx
MMS PPT 2.pptxMMS PPT 2.pptx
MMS PPT 2.pptx
 
Nfs ppt.ppt
Nfs ppt.pptNfs ppt.ppt
Nfs ppt.ppt
 
Cloud Storage Infrastructure updated.pptx
Cloud Storage Infrastructure updated.pptxCloud Storage Infrastructure updated.pptx
Cloud Storage Infrastructure updated.pptx
 
Data Center Advanced.pptx
Data Center Advanced.pptxData Center Advanced.pptx
Data Center Advanced.pptx
 
PPT (2).ppt
PPT (2).pptPPT (2).ppt
PPT (2).ppt
 
INTRODUCTION_O1.pptx
INTRODUCTION_O1.pptxINTRODUCTION_O1.pptx
INTRODUCTION_O1.pptx
 
Updated_Mathematics .pptx
Updated_Mathematics .pptxUpdated_Mathematics .pptx
Updated_Mathematics .pptx
 
CSA PPT UNIT 1.pptx
CSA PPT UNIT 1.pptxCSA PPT UNIT 1.pptx
CSA PPT UNIT 1.pptx
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Interface.pptx

  • 3. JAVA INTERFACE 3 ◉ A J ava interface is a collection of constants and abstract methods ◼ abstract method: a method header without a method body; we declare an abstract method using the modifier a b s t ra c t ◼ since all methods in an interface are abstract, the a b s t ra c t modifier is usually left off ◉ Methods in an interface have public visibility by default
  • 4. INTERFACE: SYNTAX interfaceisa reserved word public interface Doable { public static f i n a l String NAME; public void doThis(); public i n t doThat(); public void doThis2 ( f l o a t value, char ch); public boolean doTheOther ( i n t num); } A semicolon immediately followseach method header No method in an interfacehas a definition 4
  • 5. IMPLEMENTING AN INTERFACE 5 ◉ A class formally implements an interface by ◼ stating so in the class header in the implements clause ◼ a class can implement multiple interfaces: the interfaces are listed in the implements clause, separated by commas ◉ If a class asserts that it implements an interface, it must define all methods in the interface or the compiler will produce errors
  • 6. IMPLEMENTING INTERFACES public class Something implements Doable { public void doThis ( ) { // whateve r } public void doThat ( ) { // whatever } implements is a reserved word Each method listed in Doable is given a definition // etc. } public c l a s s ManyThings implements Doable, AnotherDoable 6
  • 7. INTERFACES: AN EXAMPLE 7 ◉ A class that implements an interface can implement other methods as well
  • 8. UML DIAGRAM <<interface>> Complexity + getComplexity () : int + setComplexity (int) : void Question + getQuestion () : String + getAnswer () : String + answerCorrect (String) : boolean + toString() : String MiniQuiz + main(args : String[]) : void 1 8 2
  • 9. INTERFACES: EXAMPLES FROM JAVA STANDARD CLASS LIBRARY 9 ◉ The Java Standard Class library defines many interfaces: ◼ the I t e r a t o r interface contains methods that allow the user to move through a collection of objects easily □ h a sN ex t (), n e x t ( ) , remove() ◼ the Comparable interface contains an abstract method called compareTo, which is used to compare two objects i f (obj1.compareTo(obj2) < 0) Syste m . o u t . pr i n t l n (“ o b j 1 i s l e s s than o b j 2 ” ) ;
  • 10. POLYMORPHISM VIA INTERFACES 10 ◉ Define a polymorphism reference through interface ◼ declare a reference variable of an interface type Doable o b j ; ◼ the obj reference can be used to point to any object of any class that implements the Doable interface ◼ the version of doThis depends on the type of object that obj is referring to: o b j . d o T h i s ( ) ;
  • 11. EXAMPLE: POLYMORPHISM VIA INTERFACE 11 ◉ The payroll program revisited: we want to sort the employees by name
  • 12. MORE EXAMPLES p u b l ic i n t e r f a c e Speaker { p u b l ic void sp e a k( ) ; } c l a s s Philosopher extends Human implements Speaker { // p u b l ic void speak() {…} p u b l ic void p o n t i f i c a t e ( ) {…} } c l a s s Dog extends Animal implements Speaker { // p u b l ic void speak() { … } Speaker g u e s t ; guest = new Ph i l o sop he r( ); g u e s t . s p e a k ( ) ; guest = Dog(); g u e s t . s p e a k ( ) ; 11 Speaker s p e c i a l ; s p e c i a l = new Ph i l o so phe r( ) ; s p e c i a l . p o n t i f i c a t e ( ) ; / /compilererror Speaker s p e c i a l ; s p e c i a l = new Ph i l o so phe r( ) ; ( ( P h i l o s o p h e r ) s p e c i a l ) . p o n t i f i c a t e ( ) ;
  • 13. INTERFACE HIERARCHIES ◉ Inheritance can be applied to interfaces as well as classes ◉ One interface can be used as the parent of another ◉ The child interface inherits all abstract methods of the parent ◉ A class implementing the child interface must define all methods from both the parent and child interfaces ◉ Note that class hierarchies and interface hierarchies are distinct (they do not overlap) 12