继承需要使用extends关键字。
/**
* 父类
*/
public class Father {
}
被继承类可称为超类、基类、父类。
/**
* 子类
*/
public class Son extends Father{
}
继承类成为子类、派生类、孩子类。
/**
* 父类
*/
public class Father {
// 共有属性
public String name;
// 受保护属性
protected String power;
// 私有属性
private int age;
// 默认属性
boolean tall;
// 共有方法
public String getName() {
return name;
}
public int getAge() {
return age;
}
// 私有方法
private void salary(){
}
// 保护方法
protected String getPower(){
power = "父类的权益";
return power;
}
// 默认方法
void testDefaultMethod(){
}
}
/**
* 子类
*/
public class Son extends Father{
/**
* 调用父类的一些成员变量或方法
*/
public void test01(){
// 调用父类共有的属性和方法
name = "父类" + getName();
// 调用父类受保护的属性和方法
String powerStr = power + getPower();
// 调用父类私有的属性和方法
// 'age' has private access in 'Father'
// age;
// 'salary()' has private access in 'Father'
// salary();
// 调用父类默认权限的属性和方法
boolean tall = super.tall;
testDefaultMethod();
}
}
当子类重写父类的方法时会使用 @Override关键字标记。
@Override
public String getName() {
// todosomething
return super.getName();
}
一般重写父类方法是为了自定义属性。去掉关键字其实就是子类的普通方法,若是还想在该方法中方位父类方法,需要使用super关键字,否则无法区分调用的是哪一个类中的方法,可能出现死循环问题。
public String getName() {
return "子类" + super.getName();
}
方法重写有一定的规则:
public Father() {
System.out.println("这是父类的构造");
}
public Son() {
System.out.println("这是子类的构造");
}
public static void main(String[] args) {
Son son = new Son();
}
S:\JDK16\bin\java.exe "-javaagent:S:\IntelliJIDEA\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar=49229:S:\IntelliJIDEA\IntelliJ IDEA 2021.3.3\bin" -Dfile.encoding=UTF-8 -classpath F:\java\extend\out\production\extend Test
这是父类的构造
这是子类的构造
如果子类构造没有显式调用父类的构造方法,则会自动调用父类的无参构造。
public class Father {
// public Father() {
// System.out.println("这是父类的构造");
// }
public Father(String name) {
System.out.println("这是父类的有参构造");
}
}
public class Son extends Father{
public Son() {
Father father = new Father("father");
System.out.println("这是子类的构造");
}
}

public Son() {
// Father father = new Father("father");
super("father");
System.out.println("这是子类的构造");
}
S:\JDK16\bin\java.exe "-javaagent:S:\IntelliJIDEA\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar=52386:S:\IntelliJIDEA\IntelliJ IDEA 2021.3.3\bin" -Dfile.encoding=UTF-8 -classpath F:\java\extend\out\production\extend Test
这是父类的有参构造
这是子类的构造
Java中支持多层继承,但不支持多继承。
public class Son extends Father{}
public class Grandson extends Son{
}
Grandson继承Son,Son继承Father。若是多继承则会报错。

Java类不支持多继承,但接口可以多继承。
public interface Interface01 {
}
...
public interface Interface02 {
}
...
// 接口多继承
public class Test implements Interface01, Interface02 {
...
}
...
父类引用指向子类对象称为多态。
// 多态
Father father = new Son();
置换法则:程序中任何出现父类对象的地方都可以用子类对象替换。
Father[] fathers = new Father[10];
fathers[0] = new Son();
fathers = new Son[10];
即使父类数组被全部置换,但申明的还是父类,所以获取的对象还是父类,无法访问子类对象特有的属性和方法,也不能赋值给子类变量。
// 置换法则
Father[] fathers = new Father[10];
fathers[0] = new Son();
// 无需强转
fathers = new Son[10];
// 得到的只能是父类,不是子类
Father father1 = fathers[0];
// 非法
Son son1 = fathers[0];
// 强转
Son son2 = (Son) fathers[0];
强转会忽略对象实际类型,使用对象的全部功能,所以强转之前最好判断一下类型。
Father[] fathers = new Father[10];
fathers[0] = new Son();
// 类型判断
if(fathers[0] instanceof Son){
Son son1 = (Son) fathers[0];
}
重载就是在一个类中,方法名相同,参数不同,返回值可以不同,也可以相同,多用于构造函数。
public Father() {
System.out.println("这是父类的构造");
}
public Father(String name) {
System.out.println("这是父类的有一个参的构造");
}
public Father(String name, String power) {
this.name = name;
this.power = power;
System.out.println("这是父类的有两个参的构造");
}
public Father(String name, String power, int age) {
this.name = name;
this.power = power;
this.age = age;
System.out.println("这是父类的有三个参的构造");
}
// 无参
private void test(){
}
// int 参
private int test(int a){
return -1;
}
// 参数名相同,类型不同
private int test(boolean a){
return -1;
}
private String test(String str,int a){
return str;
}
// 改变参数位置
private String test(int a,String str){
return str;
}
// 只改变返回值类型,不能算重载
// private int test(String str,int a){
// return a;
// }
使用final类和方法,可以阻止继承。
public final class FinalTest {
void test() {
}
}
尝试继承final类。

final方法也是不能被重写的。
public class FinalTest {
final void test() {
}
}
尝试重写final方法。

Java中基本数据类型和String都是final类,为了防止被继承。

