Interfaces in Java
Interfaces
public interface PetForSale {
/** Display yourself */
void lookCute();
/** How much is that doggy in the window? */
float price();
}
class PetHamster extends Hamster implements PetForSale {
public Hamster(String name, float weight) {
super(name, weight);
}
void lookCute()
{
System.out.println("Hi, I'm " + name +
" the talking hamster!");
System.out.println("I am " + age + " years old.");
}
float price()
{
return 50.0 - age*10.0;
}
}
PetForSale p = new PetHamster("Johnny",1.0);
p.lookCute();
- Interfaces for reusable code
- Multiple interface implementation
- Single class inheritance
Next | Previous