CS515 Module 1 - InterfaceAbstractClass
CS515 Module 1 - InterfaceAbstractClass
Advance Programming
Interface and Abstract Class
By;
Dr. Y. B Mohammed
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.
9-2
Interface in UML and Java
<<Interface>> Public interface IUSBMemory {
IUSBMemory void save();
save() }
Producer of an Interface
• If a class implements an interface, it uses the
implements keyword in the class header.
<<Interface>> public class USBMemory
IUSBMemory implements IUSBMemory
save()
{
// more fields are declared
void save() {…};
USBMemory // more methods are implemented
}
save()
9-4
Client of an Interface
• Class Computer uses any class implementing
IUSBMemory to save a file
public class Computer {
<<Interface>> Computer private IUSBMemory usb;
IUSBMemory usb
1 // more fields are here.
save() editAfile()
void setUsb(IUSBMemory m)
{ usb = m; }
public void editAfile()
USBMemory { usb.save();
}
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.
public class Computer {
private IUSBMemory usb;
<<Interface>> Computer
usb // more fields are here.
IUSBMemory
1
save() editAfile() void setUsb(IUSBMemory m)
{ usb = m; }
public void editAfile()
{ usb.save();
Phone
}
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 class Computer {
public static void main(String[] args) { private IUSBMemory usb;
Computer myComputer = new Computer(); …..
IUSBMemory myusb1 = new USBMemory(); void setUsb(IUSBMemory m)
myComputer.setUsb(myusb1); { usb = m; }
myComputer.editAfile(); ………..
Phone phone = new Phone(); }
myComputer.setUsb(phone);
myComputer.editAfile();
}
}
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.
9-9
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 {
<<Abstract>>
usb
Computer private AbsUSBMemory usb;
AbsUSBMemory
save()
1
editAfile() // more fields are here.
void setUsb(AbsUSBMemory
m)
{ usb = m; }
public void editAfile()
{ usb.save();
}
}
Still Need to wait..
• No product based on AbsUSBMemory is available
yet. Method save() is not implemented yet…
<<abstract>> public class USBMemory
AbsUSBMemory extends AbsUSBMemory {
-brand:String public void save()
void setBrand(String b) {
String getBrand() // Your implementation
<<abstract>> void save()
// is here
}
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.
public class Computer {
<Abstract>>
usb
Computer private AbsUSBMemory usb;
AbsUSBMemory // more fields are here.
1
save() editAfile()
void setUsb(AbsUSBMemory m)
{ usb = m; }
public void editAfile()
Phone { usb.save();
}
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();
}
}
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-17
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-18
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.
9-19