当前位置:网站首页>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
}
}边栏推荐
- js 找出两个数组中的重复元素
- C language a little bit (may increase in the future)
- Xception learning notes
- js中的原型和原型链
- The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
- Catch 222222
- Od modify DLL and exe pop-up contents [OllyDbg]
- Visual effects, picture to cartoon function
- 【小程序项目开发-- 京东商城】uni-app之首页商品楼层
- Mouse over effect 7
猜你喜欢

XXL job User Guide

Share Creators萌芽人才培養計劃來了!

STM32——一线协议之DS18B20温度采样
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

Voici le programme de formation des talents de SHARE Creators!

【机器学习】向量化计算 -- 机器学习路上必经路

Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and

RestCloud ETL WebService数据同步到本地

通信协议——分类及其特征介绍

【EXSI】主机间传输文件
随机推荐
【微信小程序开发】样式汇总
Visual effects, picture to cartoon function
Share Creators萌芽人才培養計劃來了!
Record a service deployment failure troubleshooting
IEDA 右键源码文件菜单简介
An article explaining the publisher subscriber model and the observer model
【EXSI】主机间传输文件
Prototype and prototype chain in JS
DenseNet网络论文学习笔记
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
鼠标悬停效果五
Servlet [first introduction]
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
Magnetic manometer and measurement of foreign coins
Mouse over effect III
鼠标悬停效果二
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
鼠标悬停效果三
Completely solve the lost connection to MySQL server at 'reading initial communication packet
[PR # 5 A] two way running (state pressure DP)