Java 三大特性之继承
继承可以不用重新编写已有的特性,同时又可以根据需求来覆写方法
父类:
- public class Father {
- protected String getText(){
- return "I am Father";
- }
- }
子类:
- public class Son extends Father{
- protected String getText(){
- return "I am Son";
- }
- }
客户端:
- public class Main {
- public static void main(String[] args) {
- Father f=new Father();
- System.out.println(f.getText());
- Father f2=new Son();
- System.out.println(f2.getText());
- }
- }
运行结果:
- I am Father
- I am Son