来源于百度:
在Java中,通过关键字extends继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类)。在Java中不允许多继承。
例子1:
public class Client2 {
public static void main(String[] args) {
Son son = new Son();
son.print();
}
}
class Father {
public Father() {
System.out.println("Father");
}
public void print() {
System.out.println("Father class");
}
}
class Son extends Father {
public Son() {
System.out.println("Son");
}
public void print() {
System.out.println("Son class");
}
}
输出结果:
Father
Son
Son class
class Animal{
void eat(){
System.out.println("Animal eat");
}
void sleep(){
System.out.println("Animal sleep");
}
void breathe(){
System.out.println("Animal breathe");
}
}
class Fish extends Animal{
void breathe(){
System.out.println("Fish breathe");
}
}
public class TestNew {
public static void main(String[] args) {
Animal an = new Animal();
Fish fn = new Fish();
an.breathe();
fn.breathe();
}
}
执行结果:
Animal breathe
Fish breathe