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

PDF
Interface
PPTX
Interfaces in java
PPT
Interfaces
PDF
8 abstract classes and interfaces
PPTX
interface in c#
PPTX
Java interfaces
PPTX
Lecture 18
PPTX
Java interfaces
Interface
Interfaces in java
Interfaces
8 abstract classes and interfaces
interface in c#
Java interfaces
Lecture 18
Java interfaces

What's hot (20)

PPTX
Interface in java
PPTX
DOCX
Static keyword u.s ass.(2)
PDF
itft-Decision making and branching in java
PPTX
8abstact class in c#
PPT
Ap Power Point Chpt8
DOC
PPT
Java interfaces
PPT
Chapter 09
PPTX
Implementing polymorphism
PDF
LPR - Week 2 - DEputty.pdf
PPTX
Java interfaces
DOC
PPTX
Abstract Class Presentation
DOCX
C# interview quesions
PPT
Unit 4 Java
PPT
Java
PPTX
2- Introduction to java II
PDF
14 interface
Interface in java
Static keyword u.s ass.(2)
itft-Decision making and branching in java
8abstact class in c#
Ap Power Point Chpt8
Java interfaces
Chapter 09
Implementing polymorphism
LPR - Week 2 - DEputty.pdf
Java interfaces
Abstract Class Presentation
C# interview quesions
Unit 4 Java
Java
2- Introduction to java II
14 interface
Ad

Viewers also liked (18)

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

Similar to Oop interfaces (20)

PPT
06 abstract-classes
PPT
06 abstract-classes
PPT
Interfaces .ppt
PPT
Interfaces.ppt
PPTX
OOFeatures_revised-2.pptx
PDF
Interfaces in java
PPT
Java interfaces
PPTX
Interface
PPTX
it is the quick gest about the interfaces in java
PPTX
INTERFACES. with machine learning and data
PDF
Java 06
PDF
Abstraction in Java: Abstract class and Interfaces
PPT
Ap Power Point Chpt5
PDF
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
PDF
Smart material - Unit 2 (1).pdf
PDF
Smart material - Unit 2 (1).pdf
PPTX
Interface in Java
PPT
Java interfaces & abstract classes
PDF
javainterface
PDF
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
06 abstract-classes
06 abstract-classes
Interfaces .ppt
Interfaces.ppt
OOFeatures_revised-2.pptx
Interfaces in java
Java interfaces
Interface
it is the quick gest about the interfaces in java
INTERFACES. with machine learning and data
Java 06
Abstraction in Java: Abstract class and Interfaces
Ap Power Point Chpt5
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Smart material - Unit 2 (1).pdf
Smart material - Unit 2 (1).pdf
Interface in Java
Java interfaces & abstract classes
javainterface
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Welding lecture in detail for understanding
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Internet of Things (IOT) - A guide to understanding
Sustainable Sites - Green Building Construction
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
R24 SURVEYING LAB MANUAL for civil enggi
Welding lecture in detail for understanding
additive manufacturing of ss316l using mig welding
CYBER-CRIMES AND SECURITY A guide to understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

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