Interfaces are a kind of contract: classes implementing it
must implement the methods of the interface.
So when you get an object typified by an interface, you are sure it has a given set of methods.
Note an object can implement several interfaces.
On the other hand, you can make a class to inherit from (extend) another. The interest is that you can define methods common to all derived classes (default implementation) and derived classes can override these methods to do their own.
The parent class can be abstract, meaning you can't create a new instance of it. Eg. the abstract class 'Fruit' is extended to 'Banana' and 'Apple': you can create an apple but not a fruit, too abstract.
You can do:
Code:interface Anim
{
void update();
}
abstract class Base{
String name;
Base(String n){
name = n;
}
}
class Anim1 extends Base implements Anim{
Anim1(String n){
super(n);
}
void update(){
rect(random(11), random(11), random(120), random(84));
}
}
class Anim2 extends Base implements Anim{
int an;
Anim2(String n){
super(n);
an = n.length();
}
void update(){
ellipse(random(11), random(11), random(50), random(50) );
}
}
or even just drop the interface:
Code:abstract class Anim{
String name;
Anim(String n){
name = n;
}
abstract void update();
}
class Anim1 extends Anim{
Anim1(String n){
super(n);
}
void update(){
rect(random(11), random(11), random(120), random(84));
}
}
class Anim2 extends Anim{
int an;
Anim2(String n){
super(n);
an = n.length();
}
void update(){
ellipse(random(11), random(11), random(50), random(50) );
}
}