SlideShare una empresa de Scribd logo
1 de 8
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

Más contenido relacionado

La actualidad más candente

Ap Power Point Chpt8
Ap Power Point Chpt8Ap Power Point Chpt8
Ap Power Point Chpt8
dplunkett
 

La actualidad más candente (20)

Interface in java
Interface in javaInterface in java
Interface in java
 
Abstract method
Abstract methodAbstract method
Abstract method
 
Static keyword u.s ass.(2)
Static keyword u.s ass.(2)Static keyword u.s ass.(2)
Static keyword u.s ass.(2)
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Ap Power Point Chpt8
Ap Power Point Chpt8Ap Power Point Chpt8
Ap Power Point Chpt8
 
Vvi
VviVvi
Vvi
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Oop
OopOop
Oop
 
Chapter 09
Chapter 09Chapter 09
Chapter 09
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
LPR - Week 2 - DEputty.pdf
LPR - Week 2 - DEputty.pdfLPR - Week 2 - DEputty.pdf
LPR - Week 2 - DEputty.pdf
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
C#
C#C#
C#
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Java
JavaJava
Java
 
2- Introduction to java II
2-  Introduction to java II2-  Introduction to java II
2- Introduction to java II
 
14 interface
14  interface14  interface
14 interface
 

Destacado

Deber de imformatica jimena y galo
Deber de imformatica jimena y galoDeber de imformatica jimena y galo
Deber de imformatica jimena y galo
jime15
 
วัดพระศรีรัตนศาสดาราม 4121
วัดพระศรีรัตนศาสดาราม 4121วัดพระศรีรัตนศาสดาราม 4121
วัดพระศรีรัตนศาสดาราม 4121
marut4121
 

Destacado (18)

Module1
Module1Module1
Module1
 
Notam 24-04-2015
Notam 24-04-2015Notam 24-04-2015
Notam 24-04-2015
 
Goose chasegroup
Goose chasegroupGoose chasegroup
Goose chasegroup
 
Deber de imformatica jimena y galo
Deber de imformatica jimena y galoDeber de imformatica jimena y galo
Deber de imformatica jimena y galo
 
Denk Modulair, Denk Lego
Denk Modulair, Denk LegoDenk Modulair, Denk Lego
Denk Modulair, Denk Lego
 
Magazine design evaluation pp
Magazine design evaluation ppMagazine design evaluation pp
Magazine design evaluation pp
 
公共施設白書を知ろう会 資料
公共施設白書を知ろう会 資料公共施設白書を知ろう会 資料
公共施設白書を知ろう会 資料
 
Nutrición vs Alimentación
Nutrición vs AlimentaciónNutrición vs Alimentación
Nutrición vs Alimentación
 
2 wireless
2 wireless2 wireless
2 wireless
 
วัดพระศรีรัตนศาสดาราม 4121
วัดพระศรีรัตนศาสดาราม 4121วัดพระศรีรัตนศาสดาราม 4121
วัดพระศรีรัตนศาสดาราม 4121
 
Week Aef4 11
Week Aef4 11Week Aef4 11
Week Aef4 11
 
Brand story of Nalacity Foundation
Brand story of Nalacity FoundationBrand story of Nalacity Foundation
Brand story of Nalacity Foundation
 
Voto de Gilmar Mendes em 15/09/2005
Voto de Gilmar Mendes em 15/09/2005Voto de Gilmar Mendes em 15/09/2005
Voto de Gilmar Mendes em 15/09/2005
 
Cables Brochure Web
Cables Brochure WebCables Brochure Web
Cables Brochure Web
 
Kites
KitesKites
Kites
 
Дмитрий Волох_фулфилмент_Owox_2014
Дмитрий Волох_фулфилмент_Owox_2014Дмитрий Волох_фулфилмент_Owox_2014
Дмитрий Волох_фулфилмент_Owox_2014
 
Oh the Places You'll Go
Oh the Places You'll GoOh the Places You'll Go
Oh the Places You'll Go
 
Hello openstack 2014
Hello openstack 2014Hello openstack 2014
Hello openstack 2014
 

Similar a Oop interfaces

Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
dplunkett
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 

Similar a Oop interfaces (20)

06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Interface
InterfaceInterface
Interface
 
Java 06
Java 06Java 06
Java 06
 
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
 
Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Smart material - Unit 2 (1).pdf
Smart material - Unit 2 (1).pdfSmart material - Unit 2 (1).pdf
Smart material - Unit 2 (1).pdf
 
Smart material - Unit 2 (1).pdf
Smart material - Unit 2 (1).pdfSmart material - Unit 2 (1).pdf
Smart material - Unit 2 (1).pdf
 
Interface in Java
Interface in JavaInterface in Java
Interface in Java
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
javainterface
javainterfacejavainterface
javainterface
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 

Último

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 

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