abstract class mankind
{
final String name="xuguo";
int sex;
int age;
abstract void profession(String position);
}
class student extends mankind
{
void profession(String position)
{
System.out.println(super.name);
System.out.println("I am a student."+position);
}
}
class teacher extends mankind
{
void profession(String position)
{
System.out.println(super.name);
System.out.println("Iam a teacher."+position);
}
}
interface animal
{
int handnumber=2;
void model();
}
class mankind2 implements animal
{
public void model()
{
System.out.println("Generally,one person has two hands.");
System.out.println("one head,two eyes,two ears,like monkey.");
System.out.println(animal.handnumber);
}
}
class Overloadexample extends Frame {
public Overloadexample() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
接口是抽象类的变体。接口中的所有方法都是抽象的,没有一个有程序体。接口只可以定义static final成员变量。
接口的好处是,它给出了屈从于Java技术单继承规则的假象。当类定义只能扩展出单个类时,它能实现所需的多个接口。
接口的实现与子类相似,除了该实现类不能从接口定义中继承行为。当类实现特殊接口时,它定义(即,将程序体给予)所有这种接口的方法。然后,它可以在实现了该接口的类的任何对象上调用接口的方法。由于有抽象类,它允许使用接口名作为引用变量的类型。通常的动态联编将生效。引用可以转换到接口类型或从接口类型转换,instanceof运算符可以用来决定某对象的类是否实现了接口。
接口是用关键字interface来定义的,如下所述:
public interface Transparency {
public static final int OPAQUE=1;
public static final int BITMASK=2;
public static final int TRANSLUCENT=3;
public int getTransparency();
}
类能实现许多接口。由类实现的接口出现在类声明的末尾以逗号分隔的列表中,如下所示:
public class MyApplet extends Applet implements
Runnable, MouseListener{
"..."
}