举例:
public class Test {
public static void main(String[] args) {
People people = new Student();
people.say();
//people.say2();//会报错:The method say2() is undefined for the type People
System.out.println("-----");
Student s = new Student();
s.say();
s.say2();
}
}
abstract class People{
abstract void say();
}
class Student extends People{
@Override
void say() {
System.out.println("我是人");
}
void say2() {
System.out.println("我是学生");
}
}
输出结果:
我是人
-----
我是人
我是学生
当子类继承父类的时候,如果子类有新的方法并且要使用,不要发生多态,否则就只能用父类的方法,子类的新方法没法用。