SlideShare a Scribd company logo
Interfaces 
Kaustubh Joshi
Interfaces 
 An interface declares (describes) methods but 
does not supply bodies for them 
 interface interFaceName{ 
public void function1(Class1 obj1); 
public void function2(Class2 obj2); 
} 
 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) which act like constants 
2
Implementin 
g 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 otherClass 
implements Interface1, Interface2 { 
//declarations 
} 
3
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” 
 This concept could be explained as partial multiple 
inheritance 
 In writing Applets, it is common to have one class 
implement several different listeners 
 Example: 
class otherClass extends Applet 
implements Interface1, Interface2 { 
//Statements 
} 
 Reason 2: You can write methods that work for more than 
one kind of class 
4
 instanceof is a keyword that tells you whether 
a variable 
“is a” member of a class or interface 
 For example, if 
class Dog extends Animal implements Pet {...} 
Animal fido = new Dog(); 
then the following are all true: 
fido instanceof Dog 
fido instanceof Animal 
fido instanceof Pet 
 instanceof is seldom used 
 When you find yourself wanting to use instanceof, 
think about whether the method you are writing 
should be moved to the individual subclasses 
5 
Instance of
 When you implement an interface, you promise to define all 
the functions it declares 
 There can be a lot of methods 
interface KeyListener { 
public void keyPressed(KeyEvent e); 
public void keyReleased(KeyEvent e); 
public void keyTyped(KeyEvent e); 
} 
 What if you only care about a couple of these methods? 
6 
Interfaces 
again
 Solution: use an adapter class 
 An adapter class implements an interface and 
provides empty method bodies 
class KeyAdapter implements KeyListener { 
public void keyPressed(KeyEvent e) { }; 
public void keyReleased(KeyEvent e) { }; 
public void keyTyped(KeyEvent e) { }; 
} 
 You can override only the methods you care 
about 
 This isn’t elegant, but it does work 
 Java provides a number of adapter classes 
7 
Adapter 
class
Vocabulary 
 abstract method—a method which is declared but not 
defined (it has no method body) 
 abstract class—a class which either (1) contains abstract 
methods, or (2) has been declared abstract 
 instantiate—to create an instance (object) of a class 
 interface—similar to a class, but contains only abstract 
methods (and possibly constants) 
 adapter class—a class that implements an interface but 
has only empty method bodies 
8

More Related Content

What's hot (20)

PPTX
Interface in java
PhD Research Scholar
 
PPTX
Abstract method
Yaswanth Babu Gummadivelli
 
DOCX
Static keyword u.s ass.(2)
Syed Umair
 
PDF
itft-Decision making and branching in java
Atul Sehdev
 
PPTX
8abstact class in c#
Sireesh K
 
PPT
Ap Power Point Chpt8
dplunkett
 
DOC
Vvi
Laxman Nikam
 
PPT
Java interfaces
RanjithaM32
 
PPT
Chapter 09
Graham Royce
 
PPTX
Implementing polymorphism
rajshreemuthiah
 
PDF
LPR - Week 2 - DEputty.pdf
FaridRomadhana
 
PPTX
Java interfaces
Sujit Kumar
 
DOC
C#
LiquidHub
 
PPTX
Abstract Class Presentation
tigerwarn
 
DOCX
C# interview quesions
Shashwat Shriparv
 
PPT
Unit 4 Java
arnold 7490
 
PPT
Java
mbruggen
 
PPTX
2- Introduction to java II
Ghadeer AlHasan
 
PDF
14 interface
Ravindra Rathore
 
Interface in java
PhD Research Scholar
 
Static keyword u.s ass.(2)
Syed Umair
 
itft-Decision making and branching in java
Atul Sehdev
 
8abstact class in c#
Sireesh K
 
Ap Power Point Chpt8
dplunkett
 
Java interfaces
RanjithaM32
 
Chapter 09
Graham Royce
 
Implementing polymorphism
rajshreemuthiah
 
LPR - Week 2 - DEputty.pdf
FaridRomadhana
 
Java interfaces
Sujit Kumar
 
Abstract Class Presentation
tigerwarn
 
C# interview quesions
Shashwat Shriparv
 
Unit 4 Java
arnold 7490
 
Java
mbruggen
 
2- Introduction to java II
Ghadeer AlHasan
 
14 interface
Ravindra Rathore
 

Viewers also liked (18)

PPT
Module1
常 琳
 
PDF
Notam 24-04-2015
Carlos Carvalho
 
PPTX
Goose chasegroup
Les Davy
 
PPTX
Deber de imformatica jimena y galo
jime15
 
PPTX
Denk Modulair, Denk Lego
Ide Koops
 
PPTX
Magazine design evaluation pp
FirstClassProductions
 
ODP
公共施設白書を知ろう会 資料
長野市議会議員小泉一真
 
PPTX
Nutrición vs Alimentación
charito ybarra
 
PDF
2 wireless
Joel Ruiz Ibarra
 
PPTX
วัดพระศรีรัตนศาสดาราม 4121
marut4121
 
PPTX
Week Aef4 11
Les Davy
 
PDF
Brand story of Nalacity Foundation
Nalacity Shop
 
PDF
Voto de Gilmar Mendes em 15/09/2005
Miguel Rosario
 
PDF
Cables Brochure Web
gm330
 
ODP
Kites
robthrr
 
PDF
Дмитрий Волох_фулфилмент_Owox_2014
TOCHKA
 
PPT
Oh the Places You'll Go
msawesome
 
PDF
Hello openstack 2014
OpenCity Community
 
Module1
常 琳
 
Notam 24-04-2015
Carlos Carvalho
 
Goose chasegroup
Les Davy
 
Deber de imformatica jimena y galo
jime15
 
Denk Modulair, Denk Lego
Ide Koops
 
Magazine design evaluation pp
FirstClassProductions
 
公共施設白書を知ろう会 資料
長野市議会議員小泉一真
 
Nutrición vs Alimentación
charito ybarra
 
2 wireless
Joel Ruiz Ibarra
 
วัดพระศรีรัตนศาสดาราม 4121
marut4121
 
Week Aef4 11
Les Davy
 
Brand story of Nalacity Foundation
Nalacity Shop
 
Voto de Gilmar Mendes em 15/09/2005
Miguel Rosario
 
Cables Brochure Web
gm330
 
Kites
robthrr
 
Дмитрий Волох_фулфилмент_Owox_2014
TOCHKA
 
Oh the Places You'll Go
msawesome
 
Hello openstack 2014
OpenCity Community
 
Ad

Similar to Oop interfaces (20)

PPT
06 abstract-classes
Abhishek Khune
 
PPT
06 abstract-classes
Anup Burange
 
PPT
Interfaces .ppt
AsifMulani17
 
PPT
Interfaces.ppt
VarunP31
 
PPTX
OOFeatures_revised-2.pptx
ssuser84e52e
 
PDF
Interfaces in java
TharuniDiddekunta
 
PPT
Java interfaces
Raja Sekhar
 
PPTX
Interface
vvpadhu
 
PPTX
it is the quick gest about the interfaces in java
arunkumarg271
 
PPTX
INTERFACES. with machine learning and data
dineshkesav07
 
PDF
Java 06
Loida Igama
 
PDF
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
PPT
Ap Power Point Chpt5
dplunkett
 
PDF
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Raffi Khatchadourian
 
PDF
Smart material - Unit 2 (1).pdf
GayathriRHICETCSESTA
 
PDF
Smart material - Unit 2 (1).pdf
GayathriRHICETCSESTA
 
PPTX
Interface in Java
Ducat India
 
PPT
Java interfaces & abstract classes
Shreyans Pathak
 
PDF
javainterface
Arjun Shanka
 
PDF
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Raffi Khatchadourian
 
06 abstract-classes
Abhishek Khune
 
06 abstract-classes
Anup Burange
 
Interfaces .ppt
AsifMulani17
 
Interfaces.ppt
VarunP31
 
OOFeatures_revised-2.pptx
ssuser84e52e
 
Interfaces in java
TharuniDiddekunta
 
Java interfaces
Raja Sekhar
 
Interface
vvpadhu
 
it is the quick gest about the interfaces in java
arunkumarg271
 
INTERFACES. with machine learning and data
dineshkesav07
 
Java 06
Loida Igama
 
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
Ap Power Point Chpt5
dplunkett
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Raffi Khatchadourian
 
Smart material - Unit 2 (1).pdf
GayathriRHICETCSESTA
 
Smart material - Unit 2 (1).pdf
GayathriRHICETCSESTA
 
Interface in Java
Ducat India
 
Java interfaces & abstract classes
Shreyans Pathak
 
javainterface
Arjun Shanka
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Raffi Khatchadourian
 
Ad

Recently uploaded (20)

PDF
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PPTX
Work at Height training for workers .pptx
cecos12
 
PPT
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PPTX
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
PPT
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
PDF
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Work at Height training for workers .pptx
cecos12
 
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 

Oop interfaces

  • 2. Interfaces  An interface declares (describes) methods but does not supply bodies for them  interface interFaceName{ public void function1(Class1 obj1); public void function2(Class2 obj2); }  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) which act like constants 2
  • 3. Implementin g 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 otherClass implements Interface1, Interface2 { //declarations } 3
  • 4. 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”  This concept could be explained as partial multiple inheritance  In writing Applets, it is common to have one class implement several different listeners  Example: class otherClass extends Applet implements Interface1, Interface2 { //Statements }  Reason 2: You can write methods that work for more than one kind of class 4
  • 5.  instanceof is a keyword that tells you whether a variable “is a” member of a class or interface  For example, if class Dog extends Animal implements Pet {...} Animal fido = new Dog(); then the following are all true: fido instanceof Dog fido instanceof Animal fido instanceof Pet  instanceof is seldom used  When you find yourself wanting to use instanceof, think about whether the method you are writing should be moved to the individual subclasses 5 Instance of
  • 6.  When you implement an interface, you promise to define all the functions it declares  There can be a lot of methods interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e); }  What if you only care about a couple of these methods? 6 Interfaces again
  • 7.  Solution: use an adapter class  An adapter class implements an interface and provides empty method bodies class KeyAdapter implements KeyListener { public void keyPressed(KeyEvent e) { }; public void keyReleased(KeyEvent e) { }; public void keyTyped(KeyEvent e) { }; }  You can override only the methods you care about  This isn’t elegant, but it does work  Java provides a number of adapter classes 7 Adapter class
  • 8. Vocabulary  abstract method—a method which is declared but not defined (it has no method body)  abstract class—a class which either (1) contains abstract methods, or (2) has been declared abstract  instantiate—to create an instance (object) of a class  interface—similar to a class, but contains only abstract methods (and possibly constants)  adapter class—a class that implements an interface but has only empty method bodies 8