[Java]Points in Invoking Constructors in Super Class - Java Programming Language

本文详细解析了Java中使用super关键字调用父类构造器的机制,包括默认构造器的调用规则及如何显式调用特定参数的父类构造器。通过实例演示,帮助开发者理解构造器调用的细节。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

• To invoke a parent constructor, you must place a call to super in the first line of the constructor.
• You can call a specific parent constructor by the arguments that you use in the call to super.
• If no this or super call is used in a constructor, then the compiler adds an implicit call to super() that calls the parent no argument constructor (which could be the default constructor).
  If the parent class defines constructors, but does not provide a no-argument constructor, then a compiler error message is issued.

Supposed that there is a super class called Employee

For example:

public class Manager extends Employee {
private String department;
public Manager(String name, double salary, String dept) {
super(name, salary);
department = dept;
}
public Manager(String name, String dept) {
super(name);
department = dept;
}
public Manager(String dept) { // This code fails: no super()
department = dept;
}
//more Manager code...
}


Now you can see what happened here.

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值