- 子类通过extends关键字继承父类
- 继承只是单继承(Java doesn’t support multiple inheritance),
继承只是单继承原因:目的是为了让类变得更加的安全,如果继承了两个的话,调用父类方法,如果两个类都有这个方法,不知道继承的哪一个。可以通过"传递"的方式实现多继承的效果,后续还有多实现的方式 - 每一个类都有它继承的类,如果没写默认继承Object,如果写了extends就继承extend后面的类,每个类都会直接或间接的继承Object
- We can’t extend Final classes in java
-
子类可以调用父类中的成员
(1)public protected权限修饰符修饰的属性和方法可以当做自己的来使用。
(2)private members of superclass are not directly accessible to subclass.but it can be indirectly accessible via getter and setter methods.
(3)构造方法严格意义来说,不算作子类继承过来的,只是子类在调用构造方法时默认调用父类的构造方法
(4)程序块严格意义来说,不算作子类继承过来的,程序块没有名字,子类不能直接调。子类执行构造方法之,默认调用了父类的构造方法,父类调用构造方法之前自动执行父类的块 -
子类继承的父类的方法如果不能满足子类的需求,可以在子类中覆盖父类该方法
We can override the method of Superclass in the Subclass. However we should always annotate overridden method with @Override annotation. -
子类可以添加自己独有的属性和方法
-
We can create an instance of subclass and then assign it to superclass variable, this is called upcasting
-
When an instance of Superclass is assigned to a Subclass variable, then it’s called downcasting
-
We can use instanceof instruction to check the inheritance between objects.