SlideShare a Scribd company logo
Interface and Abstract Class
9-2
Interfaces
• An interface is a contract between its producer and
client
• The general format of an interface definition:
public interface InterfaceName
{
//(Method headers...)
}
• All methods specified by an interface are public by
default.
• A class (as a producer) can implement one or more
interfaces.
Interface in UML and Java
<<Interface>>
IUSBMemory
save()
Public interface IUSBMemory {
void save();
}
9-4
Producer of an Interface
• If a class implements an interface, it uses the
implements keyword in the class header.
<<Interface>>
IUSBMemory
save()
USBMemory
save()
public class USBMemory
implements IUSBMemory
{
// more fields are declared
void save() {…};
// more methods are implemented
}
Client of an Interface
• Class Computer uses any class implementing
IUSBMemory to save a file
<<Interface>>
IUSBMemory
save()
USBMemory
save()
Computer
editAfile()
usb
1
public class Computer {
private IUSBMemory usb;
// more fields are here.
void setUsb(IUSBMemory m)
{ usb = m; }
public void editAfile()
{ usb.save();
}
}
Now run your program
• Remember a class is only a blue-print. To run it,
you need to “buy” computer and usb memory
objects.
public class Test {
public static void main(String[] args) {
Computer myComputer = new Computer();
IUSBMemory myusb1 = new USBMemory();
myComputer.setUsb(myusb1);
myComputer.editAfile();
}
}
New Product Comes
• A new product implementing IUSBMemory called
phone is available.
<<Interface>>
IUSBMemory
save()
Phone
save()
Computer
editAfile()
usb
1
public class Computer {
private IUSBMemory usb;
// more fields are here.
void setUsb(IUSBMemory m)
{ usb = m; }
public void editAfile()
{ usb.save();
}
}
Your Computer Class still works. No need to change!!!
Don’t forget to buy a phone!!!
• Remember a class is a blue-print. Don’t forget
to buy a phone and connect to your computer.
public class Test {
public static void main(String[] args) {
Computer myComputer = new Computer();
IUSBMemory myusb1 = new USBMemory();
myComputer.setUsb(myusb1);
myComputer.editAfile();
Phone phone = new Phone();
myComputer.setUsb(phone);
myComputer.editAfile();
}
}
public class Computer {
private IUSBMemory usb;
…..
void setUsb(IUSBMemory m)
{ usb = m; }
………..
}
9-9
Fields in Interfaces
• An interface can contain field declarations:
– all fields in an interface are treated as final and static.
• Because they automatically become final, you must provide
an initialization value.
public interface Doable
{
int FIELD1 = 1, FIELD2 = 2;
(Method headers...)
}
• In this interface, FIELD1 and FIELD2 are final static
int variables.
• Any class that implements this interface has access to these
variables.
Problem of Interface
• IUSBMemory has many features other than
save a file, such as set brand, get brand, set
speed and get speed etc. When a producer
implements IUSBMemory, it should provide its
implementations. But if many producers
implement IUSBMemory but share same
implementation for some of methods such as
set brand and get brand etc. Only difference is
how to save, i.e. the implementation of save()?
Abstract Class
• We declare AbsUSBMemory as an abstract class
public abstract class AbsUSBMemory {
private String brand;
public void setBrand(String b) {brand = b;}
public String getBrand() { return brand;}
// more fields are here
public abstract void save();
}
<<abstract>>
AbsUSBMemory
-brand:String
void setBrand(String b)
String getBrand()
<<abstract>> void save()
Your Computer Class
• Class computer can use the all the features
defined in an abstract class.
public class Computer {
private AbsUSBMemory usb;
// more fields are here.
void setUsb(AbsUSBMemory
m)
{ usb = m; }
public void editAfile()
{ usb.save();
}
}
<<Abstract>>
AbsUSBMemory
save()
Computer
editAfile()
usb
1
Still Need to wait..
• No product based on AbsUSBMemory is available
yet. Method save() is not implemented yet…
public class USBMemory
extends AbsUSBMemory {
public void save()
{
// Your implementation
// is here
}
}
<<abstract>>
AbsUSBMemory
-brand:String
void setBrand(String b)
String getBrand()
<<abstract>> void save()
USBMemory
save()
Now you can buy your products
• You can buy your computer with a usb
memory based on the features provided by
AbsUSBMemory
public class Test {
public static void main(String[] args) {
Computer myComputer = new Computer();
AbsUSBMemory myusb1 = new USBMemory();
myComputer.setUsb(myusb1);
myComputer.editAfile();
}
}
Similarly, New Product Available
• A new product called phone extends AbsUSBMemory
and is available.
<Abstract>>
AbsUSBMemory
save()
Phone
save()
Computer
editAfile()
usb
1
public class Computer {
private AbsUSBMemory usb;
// more fields are here.
void setUsb(AbsUSBMemory m)
{ usb = m; }
public void editAfile()
{ usb.save();
}
}
Your Computer Class still works. No need to change!!!
Now you can buy your phone
• You can buy your computer with a usb
memory based on the features provided by
AbsUSBMemory
public class Test {
public static void main(String[] args) {
Computer myComputer = new Computer();
Phone myusb1 = new Phone();
myComputer.setUsb(myusb1);
myComputer.editAfile();
}
}
9-17
Abstract Classes
• An abstract class cannot be instantiated, but other
classes are derived from it.
• An Abstract class serves as a superclass for other
classes.
• The abstract class represents the generic or abstract
form of all the classes that are derived from it.
• A class becomes abstract when you place the abstract
key word in the class definition.
public abstract class ClassName
9-18
Abstract Methods
• An abstract method has no body and must be
overridden in a subclass.
• An abstract method is a method that appears in a
superclass, but expects to be overridden in a subclass.
• An abstract method has only a header and no body.
AccessSpecifier abstract ReturnType MethodName(ParameterList);
• Example:
– Student.java, CompSciStudent.java, CompSciStudentDemo.java
9-19
Abstract Methods
• Notice that the key word abstract appears in the
header, and that the header ends with a semicolon.
public abstract void setValue(int value);
• Any class that contains an abstract method is
automatically abstract.
• If a subclass fails to override an abstract method, a
compiler error will result.
• Abstract methods are used to ensure that a subclass
implements the method.

More Related Content

PDF
Debugging with EMUX - RIngzer0 BACK2WORKSHOPS
Saumil Shah
 
PDF
Mastering SOLIDWORKS BOMs in OpenBOM - Part IV (Webinar #27 - )
Oleg Shilovitsky
 
PPTX
short_intro_to_CMake_(inria_REVES_team)
Jérôme Esnault
 
PDF
Session11 Ucc Intro
ISSGC Summer School
 
PPT
Core JAVA Training.ppt
RadzT1
 
PDF
Advanced Node.JS Meetup
LINAGORA
 
PDF
Container Support in IBM Spectrum LSF
Gabor Samu
 
PDF
1. Java Basic.pdf aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
AnhKhoaTrng1
 
Debugging with EMUX - RIngzer0 BACK2WORKSHOPS
Saumil Shah
 
Mastering SOLIDWORKS BOMs in OpenBOM - Part IV (Webinar #27 - )
Oleg Shilovitsky
 
short_intro_to_CMake_(inria_REVES_team)
Jérôme Esnault
 
Session11 Ucc Intro
ISSGC Summer School
 
Core JAVA Training.ppt
RadzT1
 
Advanced Node.JS Meetup
LINAGORA
 
Container Support in IBM Spectrum LSF
Gabor Samu
 
1. Java Basic.pdf aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
AnhKhoaTrng1
 

Similar to InterfaceAbstractClass presentation.pptx (20)

ODP
Bci for Beginners
IainLewis
 
PDF
Storage classes, linkage & memory management
MomenMostafa
 
PDF
How to build beautiful slides pdf format idea .
GhulamMurtazaBaig3
 
PPTX
Introduction to Murasaki
Seiichi Horie
 
DOCX
LMP1 IO and Filesystems=========================Welcome .docx
manningchassidy
 
PPTX
Java - A broad introduction
Birol Efe
 
PPTX
Fuse- Filesystem in User space
Danny Tseng
 
PPT
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
PDF
sponsorAVAST-VB2014
Martin Hron
 
PDF
Android open source project build system phi innovations - android summit 2015
Rafael Coutinho
 
PDF
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
Arun Gupta
 
PPTX
Linux Usb overview
Satyam Sharma
 
PPTX
java basic for begginers
divaskrgupta007
 
PPTX
lecture 6
umardanjumamaiwada
 
PPTX
Introduction to computer science
umardanjumamaiwada
 
DOCX
Implementing of classical synchronization problem by using semaphores
Gowtham Reddy
 
PPTX
Compose in Theory
Garth Gilmour
 
PPTX
HPC_MPI_CICD.pptx
ObjectAutomation2
 
PPTX
Adobe FrameMaker Workshop TC Camp 2015
Maxwell Hoffmann
 
PDF
unix-editors.pdf
samuelEstevam3
 
Bci for Beginners
IainLewis
 
Storage classes, linkage & memory management
MomenMostafa
 
How to build beautiful slides pdf format idea .
GhulamMurtazaBaig3
 
Introduction to Murasaki
Seiichi Horie
 
LMP1 IO and Filesystems=========================Welcome .docx
manningchassidy
 
Java - A broad introduction
Birol Efe
 
Fuse- Filesystem in User space
Danny Tseng
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
sponsorAVAST-VB2014
Martin Hron
 
Android open source project build system phi innovations - android summit 2015
Rafael Coutinho
 
S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010
Arun Gupta
 
Linux Usb overview
Satyam Sharma
 
java basic for begginers
divaskrgupta007
 
lecture 6
umardanjumamaiwada
 
Introduction to computer science
umardanjumamaiwada
 
Implementing of classical synchronization problem by using semaphores
Gowtham Reddy
 
Compose in Theory
Garth Gilmour
 
HPC_MPI_CICD.pptx
ObjectAutomation2
 
Adobe FrameMaker Workshop TC Camp 2015
Maxwell Hoffmann
 
unix-editors.pdf
samuelEstevam3
 
Ad

More from ArunPatrick2 (20)

PPT
Introduction to HTML table,width,height.ppt
ArunPatrick2
 
PPTX
introduction to java Multithreading presentation.pptx
ArunPatrick2
 
PPT
topic-6-Presentation e-commerce,B2B,B2C,C2C.ppt
ArunPatrick2
 
PPTX
collectionsframework210616084411 (1).pptx
ArunPatrick2
 
PPT
Interfaces implements,presentation in java.ppt
ArunPatrick2
 
PPTX
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
PPTX
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
ArunPatrick2
 
PPTX
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
PPTX
java package java package in java packages
ArunPatrick2
 
PPT
Interfaces in java.. introduction, classes, objects
ArunPatrick2
 
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
PPTX
Exception Handling,finally,catch,throw,throws,try.pptx
ArunPatrick2
 
PPTX
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
PPTX
Data Analytics overview,kDD process,mining Techniques.pptx
ArunPatrick2
 
PPTX
Data warehouse-complete-1-100227093028-phpapp01.pptx
ArunPatrick2
 
PPTX
DataWarehouse Architecture,daat mining,data mart,etl process.pptx
ArunPatrick2
 
PPTX
Difference between Abstract class and Interface.pptx
ArunPatrick2
 
PPT
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
ArunPatrick2
 
PPTX
InterfaceAbstractClass,interfaces,final keyword,.pptx
ArunPatrick2
 
Introduction to HTML table,width,height.ppt
ArunPatrick2
 
introduction to java Multithreading presentation.pptx
ArunPatrick2
 
topic-6-Presentation e-commerce,B2B,B2C,C2C.ppt
ArunPatrick2
 
collectionsframework210616084411 (1).pptx
ArunPatrick2
 
Interfaces implements,presentation in java.ppt
ArunPatrick2
 
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
presentation-on-exception-handling-160611180456 (1).pptx
ArunPatrick2
 
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
java package java package in java packages
ArunPatrick2
 
Interfaces in java.. introduction, classes, objects
ArunPatrick2
 
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
Exception Handling,finally,catch,throw,throws,try.pptx
ArunPatrick2
 
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
Data Analytics overview,kDD process,mining Techniques.pptx
ArunPatrick2
 
Data warehouse-complete-1-100227093028-phpapp01.pptx
ArunPatrick2
 
DataWarehouse Architecture,daat mining,data mart,etl process.pptx
ArunPatrick2
 
Difference between Abstract class and Interface.pptx
ArunPatrick2
 
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
ArunPatrick2
 
InterfaceAbstractClass,interfaces,final keyword,.pptx
ArunPatrick2
 
Ad

Recently uploaded (20)

PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 

InterfaceAbstractClass presentation.pptx

  • 2. 9-2 Interfaces • An interface is a contract between its producer and client • The general format of an interface definition: public interface InterfaceName { //(Method headers...) } • All methods specified by an interface are public by default. • A class (as a producer) can implement one or more interfaces.
  • 3. Interface in UML and Java <<Interface>> IUSBMemory save() Public interface IUSBMemory { void save(); }
  • 4. 9-4 Producer of an Interface • If a class implements an interface, it uses the implements keyword in the class header. <<Interface>> IUSBMemory save() USBMemory save() public class USBMemory implements IUSBMemory { // more fields are declared void save() {…}; // more methods are implemented }
  • 5. Client of an Interface • Class Computer uses any class implementing IUSBMemory to save a file <<Interface>> IUSBMemory save() USBMemory save() Computer editAfile() usb 1 public class Computer { private IUSBMemory usb; // more fields are here. void setUsb(IUSBMemory m) { usb = m; } public void editAfile() { usb.save(); } }
  • 6. Now run your program • Remember a class is only a blue-print. To run it, you need to “buy” computer and usb memory objects. public class Test { public static void main(String[] args) { Computer myComputer = new Computer(); IUSBMemory myusb1 = new USBMemory(); myComputer.setUsb(myusb1); myComputer.editAfile(); } }
  • 7. New Product Comes • A new product implementing IUSBMemory called phone is available. <<Interface>> IUSBMemory save() Phone save() Computer editAfile() usb 1 public class Computer { private IUSBMemory usb; // more fields are here. void setUsb(IUSBMemory m) { usb = m; } public void editAfile() { usb.save(); } } Your Computer Class still works. No need to change!!!
  • 8. Don’t forget to buy a phone!!! • Remember a class is a blue-print. Don’t forget to buy a phone and connect to your computer. public class Test { public static void main(String[] args) { Computer myComputer = new Computer(); IUSBMemory myusb1 = new USBMemory(); myComputer.setUsb(myusb1); myComputer.editAfile(); Phone phone = new Phone(); myComputer.setUsb(phone); myComputer.editAfile(); } } public class Computer { private IUSBMemory usb; ….. void setUsb(IUSBMemory m) { usb = m; } ……….. }
  • 9. 9-9 Fields in Interfaces • An interface can contain field declarations: – all fields in an interface are treated as final and static. • Because they automatically become final, you must provide an initialization value. public interface Doable { int FIELD1 = 1, FIELD2 = 2; (Method headers...) } • In this interface, FIELD1 and FIELD2 are final static int variables. • Any class that implements this interface has access to these variables.
  • 10. Problem of Interface • IUSBMemory has many features other than save a file, such as set brand, get brand, set speed and get speed etc. When a producer implements IUSBMemory, it should provide its implementations. But if many producers implement IUSBMemory but share same implementation for some of methods such as set brand and get brand etc. Only difference is how to save, i.e. the implementation of save()?
  • 11. Abstract Class • We declare AbsUSBMemory as an abstract class public abstract class AbsUSBMemory { private String brand; public void setBrand(String b) {brand = b;} public String getBrand() { return brand;} // more fields are here public abstract void save(); } <<abstract>> AbsUSBMemory -brand:String void setBrand(String b) String getBrand() <<abstract>> void save()
  • 12. Your Computer Class • Class computer can use the all the features defined in an abstract class. public class Computer { private AbsUSBMemory usb; // more fields are here. void setUsb(AbsUSBMemory m) { usb = m; } public void editAfile() { usb.save(); } } <<Abstract>> AbsUSBMemory save() Computer editAfile() usb 1
  • 13. Still Need to wait.. • No product based on AbsUSBMemory is available yet. Method save() is not implemented yet… public class USBMemory extends AbsUSBMemory { public void save() { // Your implementation // is here } } <<abstract>> AbsUSBMemory -brand:String void setBrand(String b) String getBrand() <<abstract>> void save() USBMemory save()
  • 14. Now you can buy your products • You can buy your computer with a usb memory based on the features provided by AbsUSBMemory public class Test { public static void main(String[] args) { Computer myComputer = new Computer(); AbsUSBMemory myusb1 = new USBMemory(); myComputer.setUsb(myusb1); myComputer.editAfile(); } }
  • 15. Similarly, New Product Available • A new product called phone extends AbsUSBMemory and is available. <Abstract>> AbsUSBMemory save() Phone save() Computer editAfile() usb 1 public class Computer { private AbsUSBMemory usb; // more fields are here. void setUsb(AbsUSBMemory m) { usb = m; } public void editAfile() { usb.save(); } } Your Computer Class still works. No need to change!!!
  • 16. Now you can buy your phone • You can buy your computer with a usb memory based on the features provided by AbsUSBMemory public class Test { public static void main(String[] args) { Computer myComputer = new Computer(); Phone myusb1 = new Phone(); myComputer.setUsb(myusb1); myComputer.editAfile(); } }
  • 17. 9-17 Abstract Classes • An abstract class cannot be instantiated, but other classes are derived from it. • An Abstract class serves as a superclass for other classes. • The abstract class represents the generic or abstract form of all the classes that are derived from it. • A class becomes abstract when you place the abstract key word in the class definition. public abstract class ClassName
  • 18. 9-18 Abstract Methods • An abstract method has no body and must be overridden in a subclass. • An abstract method is a method that appears in a superclass, but expects to be overridden in a subclass. • An abstract method has only a header and no body. AccessSpecifier abstract ReturnType MethodName(ParameterList); • Example: – Student.java, CompSciStudent.java, CompSciStudentDemo.java
  • 19. 9-19 Abstract Methods • Notice that the key word abstract appears in the header, and that the header ends with a semicolon. public abstract void setValue(int value); • Any class that contains an abstract method is automatically abstract. • If a subclass fails to override an abstract method, a compiler error will result. • Abstract methods are used to ensure that a subclass implements the method.