当前位置:网站首页>If a parent class defines a parameterless constructor, is it necessary to call super ()?
If a parent class defines a parameterless constructor, is it necessary to call super ()?
2022-07-01 02:57:00 【A wild man on the verge of success】
Is it necessary to call super() if a Parent class defines a no-argument constructor?
on any condition , The first line of the constructor Must be a call to the constructor of the parent class . in other words , Every time you create an instance of a subclass , Will create a new instance of the parent class in memory .
Default constructor - Is a parameterless constructor , If the class does not provide any constructors , The compiler generates it automatically . There is no difference between the default constructor and the parameterless constructor that you can add to your class yourself .
If a parent class has a parameterless constructor ( Whether it is declared manually or provided by the compiler ), The compiler will be able to add super() a line .
If the parent class Only parameterization Constructor for , You must be in the constructor of the subclass Add calls manually super(name, salary) As the first line , Otherwise the code will not compile .
for example :
Person There is no claim to extend anything , But by default it extends Object class , There is one in this category Parameterless constructor . therefore , If you decide to create it manually Person Constructor for , you There is no need to add super();, The compiler will handle it .
stay Employee Class in the constructor ,super(); This line is optional .
But for Manager Category , To compile , Its first line Must call super(name, salary);. You must call the parameterized constructor of the parent class with the appropriate parameters .
public class Person {
public Person() {
super(); // optional, can be added by the compiler
}
}
public class Employee extends Person {
protected String name;
protected int salary;
public Employee(String name, int salary) {
super(); // optional, can be added by the compiler
this.name = name;
this.salary = salary;
}
}
public class Manager extends Employee {
public Manager(String name, int salary) {
super(name, salary); // this line is mandatory
}
}边栏推荐
- HTB-Lame
- 单片机 MCU 固件打包脚本软件
- Voici le programme de formation des talents de SHARE Creators!
- Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
- PTA 1016
- Sampling Area Lights
- UE4 rendering pipeline learning notes
- MnasNet学习笔记
- PHP batch Excel to word
- Mouse over effect 9
猜你喜欢

基于OPENCV和图像减法的PCB缺陷检测

Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?

xxl-job使用指南

Introduction and basic knowledge of machine learning

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】

Gartner研究:在中国,混合云的采用已成为主流趋势

HTB-Lame

MnasNet学习笔记

记一次服务部署失败问题排查

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
随机推荐
基于OPENCV和图像减法的PCB缺陷检测
xxl-job使用指南
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
STM32 - DS18B20 temperature sampling of first-line protocol
Borrowing constructor inheritance and composite inheritance
Gartner研究:在中国,混合云的采用已成为主流趋势
最新接口自动化面试题
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
调试定位导航遇到的问题总结
Xception learning notes
Lavaweb [first understanding the solution of subsequent problems]
Restcloud ETL practice data row column conversion
旷世轻量化网络ShuffulNetV2学习笔记
Summary of problems encountered in debugging positioning and navigation
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
Nacos configuration center tutorial
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
Mouse over effect 10
lavaweb【初识后续问题的解决】
鼠标悬停效果二