方法的重写指在子类中重新定义父类中已有的方法。
重写方法要注意下面的三点:
1、重写的方法和被重写的方法必须具有相同方法名称、参数列表和返回类型;
2、子类中不允许出现与父类同名同参但不同返回值的方法;
3、重写方法不能使用比被重写的方法更严格的访问权限。
class Human { private int age; private String name; public Human() { } public Human(int age, String name) { this.age = age; this.name = name; } public String getInformation() { String infor = name + ":" + age; return infor; } } class Student extends Human { private String school; public Student(int age, String name, String school) { super(age, name); this.school = school; } public String getInformation() { String infor = super.getInformation() + ":" + school; return infor; } } public class TestOverride { public static void main(String[] args) { Student st1 = new Student(22, "李四", "重庆"); System.out.printf("%s ", st1.getInformation()); } }