当前位置:网站首页>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
}
}边栏推荐
- A small document of JS method Encyclopedia
- Restcloud ETL data realizes incremental data synchronization through timestamp
- Optimal transport Series 1
- Is it safe to open an account online in a small securities firm? Will my money be unsafe?
- 基于Pytorch完整的训练一个神经网络并进行验证
- 如果我在北京,到哪里开户比较好?另外,手机开户安全么?
- Gartner研究:在中国,混合云的采用已成为主流趋势
- 股票开户安全吗?上海股票开户步骤。
- Here comes the share creators budding talent training program!
- Mouse over effect 10
猜你喜欢

In the industrial Internet, "small" programs have "big" effects

HTB-Lame

XXL job User Guide

Const and the secret of pointers

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
性能测试常见面试题

Restcloud ETL WebService data synchronization to local

Record a service deployment failure troubleshooting

Here comes the share creators budding talent training program!
![[linear DP] shortest editing distance](/img/2f/9a6f661bf478fdd5d53e5a03d7297d.jpg)
[linear DP] shortest editing distance
随机推荐
How to determine the progress bar loaded in the loading interface when opening the game
UE4 rendering pipeline learning notes
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
大橙子疯博客搬家通知
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
kubernetes资源对象介绍及常用命令(二)
How to buy Hong Kong shares in China? What platform is safer?
Sampling Area Lights
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
性能测试常见面试题
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
使用ipmitool配置X86服务器的BMC网络和用户信息
Visual effects, picture to cartoon function
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
JS to find duplicate elements in two arrays
鼠标悬停效果七
Mnasnet learning notes
POI导出excel,按照父子节点进行分级显示
In the industrial Internet, "small" programs have "big" effects
C language a little bit (may increase in the future)