an Interface is a shared boundary across which two or more separate components of a computer system exchange information
Interfaces have to be implemented by a specific class
They can't function on their own
They use the keyword implements.
You also have to implement all the methods the interface declares.
Coding Problem 1: Interfaces (10 minutes)
Modify Player so that it implements DungeonMaster.
publicinterfaceDungeonMaster{
booleancanCreateDungeonInstance(); // should return true intincreaseHealth(); // should increase health points by one
};
publicclassPlayer{
private String userName;
privateint healthPoints;
private String state;
publicPlayer(String name){
userName = name;
state = "ALIVE";
healthPoints = 100;
}
public String isAlive(){
return state;
}
}
Coding Problem 2: Interfaces (10 minutes)
Implement the comparable interface for the class given below. If two users have the same name, they are equal. If they have different usernames, compare using the healthPoints.
publicinterfaceDungeonMaster{
booleancanCreateDungeonInstance(); // should return true intincreaseHealth(); // should increase health points by one
};
publicclassPlayer{
private String userName;
privateint healthPoints;
private String state;
publicPlayer(String name){
userName = name;
state = "ALIVE";
healthPoints = 100;
}
}
"A [Class] Has No Name"
We've seen named classes since the beginning on the semester.
Camera one = new Camera();
So, what's so special about anonymous classes?
No Name
Must extend another class or implement an interface
created immediately, since they have no name and can't be named later
cannot provide a constructor
can capture variables outside the scope of the anonymous class
Let's take a look at an anonymous class
publicclassCamera{
public String getType(){
return"Nikon";
}
}
Camera basic = new Camera();
// anonymous class that extends the Camera class
Camera dslr = new Camera() {
@Overridepublic String getType(){
return"Nikon D90 DX";
}
};
System.out.println(basic.getType());
System.out.println(dslr.getType());
interfaceInput{
String getInputType();
};
//anonymous class that implements an interface
Input in = new Input() {
@Overridepublic String getInputType(){
return"Valid Input";
}
};
System.out.println(in.getInputType());
Short Answer Questions: Anonymous Classes (5 minutes)
True/False: Anonymous classes must extend a class or implement an interface
Can you create a method inside an anonymous class that is not an override of the original class?
Can interfaces be instantiated using new? For example, is Test test = new Test(); going to result in a compiler error if Test is an interface?
When can interfaces be instantiated using new?
Can the anonymous class have a constructor (aside from the constructor in the original class)?
Coding Problem 1: Anonymous Class (5 minutes)
Use an anonymous class to implement the following interface for a language of your choice (I used Spanish in my example).
interfaceHelloWorld{
voidgreetSomeone(String someone);
};
// create your anonymous class here
spanishGreeting.greetSomeone("Akhila"); //prints Hola Akhila
Coding Problem 2: Anonymous Class (5 minutes)
Use an anonymous class to capture the value of the tn variable and implement the GetName interface.
publicclassState{
public String name;
publicState(String setName){
name = setName;
}
}
interfaceGetName{
String getName();
};
State tn = new State("Tennessee");
System.out.println(example.getName());
tn.name = "Illinois";
System.out.println(example.getName());
Two Special Interfaces: Iterable and Iterator
iterable: allows an object to be part of a for-each loop
iterable REQUIRES that you implement the iterator() method
RandomClass iterable = new RandomClass(8);
for (Object value : iterable) { // we can't do this unless RandomClass implements the iterable interfacce
System.out.println(value);
}
iterator: does the grunt work here and figures out what the next item in the collection is and iterates through all the items in the collection
iterator REQUIRES that you implement next() and hasNext()
Coding Problem: Iterable and Iterators (10 minutes)
Modify the class below so that it implements the Iterable and Iterator interfaces correctly.
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
publicclassPlaylistIterableimplementsIterable, Iterator{
private List<String> names = new ArrayList<>();
publicPlaylistIterable(){
names.add("Rams");
names.add("Posa");
names.add("Chinni");
}
}
PlaylistIterable iterable = new PlaylistIterable();
for (Object value : iterable) {
System.out.println(value);
}
That's All Folks!
Do you have other questions? Do you have feedback? Let us know!